mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 20:40:15 +01:00
- Switch code to llvm::raw_ostream as part of standardizing output forms.
- Preferring llvm::raw_ostream over std::ostream because other tooling code should be expected to rely on llvm more closely, and an overall preference towards library consistency.
- There are a couple spots in syntax/ that still use std streams, but I'd prefer to take a separate PR to see how best to address those.
- std::boolalpha doesn't work with llvm, so I've implemented equivalent in a couple places (not enough that it felt like worth making a helper function).
- Implement Print(ostream) as consistently as we can, as an instance member.
- This facilitates the use of the common/ostream.h template to provide operators.
- Preferring this approach so that Print is easily accessible via gdb, per suggestion on #executable-semantics.
- Switch code currently calling `type->Print(ostream)` to instead do `ostream << *type`.
- Remove the unused `PrintTypeEnv`, nothing used it and the declaration didn't match the definition.
102 lines
3.3 KiB
C++
102 lines
3.3 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "executable_semantics/ast/declaration.h"
|
|
|
|
namespace Carbon {
|
|
|
|
auto Declaration::MakeFunctionDeclaration(FunctionDefinition definition)
|
|
-> const Declaration {
|
|
Declaration d;
|
|
d.value = FunctionDeclaration({.definition = definition});
|
|
return d;
|
|
}
|
|
|
|
auto Declaration::MakeStructDeclaration(int line_num, std::string name,
|
|
std::list<Member*> members)
|
|
-> const Declaration {
|
|
Declaration d;
|
|
d.value = StructDeclaration(
|
|
{.definition = StructDefinition({.line_num = line_num,
|
|
.name = std::move(name),
|
|
.members = std::move(members)})});
|
|
return d;
|
|
}
|
|
|
|
auto Declaration::MakeChoiceDeclaration(
|
|
int line_num, std::string name,
|
|
std::list<std::pair<std::string, const Expression*>> alternatives)
|
|
-> const Declaration {
|
|
Declaration d;
|
|
d.value = ChoiceDeclaration({.line_num = line_num,
|
|
.name = std::move(name),
|
|
.alternatives = std::move(alternatives)});
|
|
return d;
|
|
}
|
|
|
|
auto Declaration::MakeVariableDeclaration(int source_location, std::string name,
|
|
const Expression* type,
|
|
const Expression* initializer)
|
|
-> const Declaration {
|
|
Declaration d;
|
|
d.value = VariableDeclaration({.source_location = source_location,
|
|
.name = std::move(name),
|
|
.type = type,
|
|
.initializer = initializer});
|
|
return d;
|
|
}
|
|
|
|
auto Declaration::GetFunctionDeclaration() const -> const FunctionDeclaration& {
|
|
return std::get<FunctionDeclaration>(value);
|
|
}
|
|
|
|
auto Declaration::GetStructDeclaration() const -> const StructDeclaration& {
|
|
return std::get<StructDeclaration>(value);
|
|
}
|
|
|
|
auto Declaration::GetChoiceDeclaration() const -> const ChoiceDeclaration& {
|
|
return std::get<ChoiceDeclaration>(value);
|
|
}
|
|
|
|
auto Declaration::GetVariableDeclaration() const -> const VariableDeclaration& {
|
|
return std::get<VariableDeclaration>(value);
|
|
}
|
|
|
|
void Declaration::Print(llvm::raw_ostream& out) const {
|
|
switch (tag()) {
|
|
case DeclarationKind::FunctionDeclaration:
|
|
out << GetFunctionDeclaration().definition;
|
|
break;
|
|
|
|
case DeclarationKind::StructDeclaration: {
|
|
const StructDefinition& struct_def = GetStructDeclaration().definition;
|
|
out << "struct " << struct_def.name << " {\n";
|
|
for (Member* m : struct_def.members) {
|
|
out << *m;
|
|
}
|
|
out << "}\n";
|
|
break;
|
|
}
|
|
|
|
case DeclarationKind::ChoiceDeclaration: {
|
|
const auto& choice = GetChoiceDeclaration();
|
|
out << "choice " << choice.name << " {\n";
|
|
for (const auto& [name, signature] : choice.alternatives) {
|
|
out << "alt " << name << " " << *signature << ";\n";
|
|
}
|
|
out << "}\n";
|
|
break;
|
|
}
|
|
|
|
case DeclarationKind::VariableDeclaration: {
|
|
const auto& var = GetVariableDeclaration();
|
|
out << "var " << *var.type << " : " << var.name << " = "
|
|
<< *var.initializer << "\n";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|