mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Change Declaration passing to use Ptr (#758)
This commit is contained in:
@@ -19,6 +19,7 @@ cc_library(
|
||||
":pattern",
|
||||
":struct_definition",
|
||||
"//common:ostream",
|
||||
"//executable_semantics/common:ptr",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -8,9 +8,10 @@
|
||||
#include <variant>
|
||||
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
|
||||
namespace Carbon {
|
||||
using AST = std::list<const Carbon::Declaration*>;
|
||||
using AST = std::list<Ptr<const Declaration>>;
|
||||
}
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_ABSTRACT_SYNTAX_TREE_H_
|
||||
|
||||
@@ -20,9 +20,7 @@ class Ptr {
|
||||
|
||||
template <typename OtherT,
|
||||
std::enable_if_t<std::is_convertible_v<OtherT*, T*>>* = nullptr>
|
||||
Ptr(Ptr<OtherT> other) {
|
||||
return Ptr<OtherT>(other.ptr);
|
||||
}
|
||||
Ptr(Ptr<OtherT> other) : ptr(other.Get()) {}
|
||||
|
||||
Ptr(std::nullptr_t) = delete;
|
||||
|
||||
|
||||
@@ -183,8 +183,8 @@ void InitEnv(const Declaration& d, Env* env) {
|
||||
}
|
||||
}
|
||||
|
||||
static void InitGlobals(const std::list<const Declaration*>& fs) {
|
||||
for (const auto* d : fs) {
|
||||
static void InitGlobals(const std::list<Ptr<const Declaration>>& fs) {
|
||||
for (const auto d : fs) {
|
||||
InitEnv(*d, &globals);
|
||||
}
|
||||
}
|
||||
@@ -1236,7 +1236,7 @@ void Step() {
|
||||
}
|
||||
|
||||
// Interpret the whole porogram.
|
||||
auto InterpProgram(const std::list<const Declaration*>& fs) -> int {
|
||||
auto InterpProgram(const std::list<Ptr<const Declaration>>& fs) -> int {
|
||||
state = global_arena->RawNew<State>(); // Runtime state.
|
||||
if (tracing_output) {
|
||||
llvm::outs() << "********** initializing globals **********\n";
|
||||
|
||||
@@ -40,7 +40,7 @@ void PrintEnv(Env values, llvm::raw_ostream& out);
|
||||
auto PatternMatch(const Value* p, const Value* v, int line_num)
|
||||
-> std::optional<Env>;
|
||||
|
||||
auto InterpProgram(const std::list<const Declaration*>& fs) -> int;
|
||||
auto InterpProgram(const std::list<Ptr<const Declaration>>& fs) -> int;
|
||||
auto InterpExp(Env values, const Expression* e) -> const Value*;
|
||||
auto InterpPattern(Env values, const Pattern* p) -> const Value*;
|
||||
|
||||
|
||||
@@ -989,16 +989,16 @@ static auto GetName(const Declaration& d) -> const std::string& {
|
||||
}
|
||||
}
|
||||
|
||||
auto MakeTypeChecked(const Declaration& d, const TypeEnv& types,
|
||||
const Env& values) -> const Declaration* {
|
||||
switch (d.Tag()) {
|
||||
auto MakeTypeChecked(const Ptr<const Declaration> d, const TypeEnv& types,
|
||||
const Env& values) -> Ptr<const Declaration> {
|
||||
switch (d->Tag()) {
|
||||
case Declaration::Kind::FunctionDeclaration:
|
||||
return global_arena->RawNew<FunctionDeclaration>(TypeCheckFunDef(
|
||||
&cast<FunctionDeclaration>(d).Definition(), types, values));
|
||||
return global_arena->New<FunctionDeclaration>(TypeCheckFunDef(
|
||||
&cast<FunctionDeclaration>(*d).Definition(), types, values));
|
||||
|
||||
case Declaration::Kind::StructDeclaration: {
|
||||
const StructDefinition& struct_def =
|
||||
cast<StructDeclaration>(d).Definition();
|
||||
cast<StructDeclaration>(*d).Definition();
|
||||
std::list<Member*> fields;
|
||||
for (Member* m : struct_def.members) {
|
||||
switch (m->Tag()) {
|
||||
@@ -1008,16 +1008,16 @@ auto MakeTypeChecked(const Declaration& d, const TypeEnv& types,
|
||||
break;
|
||||
}
|
||||
}
|
||||
return global_arena->RawNew<StructDeclaration>(
|
||||
return global_arena->New<StructDeclaration>(
|
||||
struct_def.line_num, struct_def.name, std::move(fields));
|
||||
}
|
||||
|
||||
case Declaration::Kind::ChoiceDeclaration:
|
||||
// TODO
|
||||
return &d;
|
||||
return d;
|
||||
|
||||
case Declaration::Kind::VariableDeclaration: {
|
||||
const auto& var = cast<VariableDeclaration>(d);
|
||||
const auto& var = cast<VariableDeclaration>(*d);
|
||||
// Signals a type error if the initializing expression does not have
|
||||
// the declared type of the variable, otherwise returns this
|
||||
// declaration with annotated types.
|
||||
@@ -1033,7 +1033,7 @@ auto MakeTypeChecked(const Declaration& d, const TypeEnv& types,
|
||||
const Value* declared_type = InterpExp(values, type);
|
||||
ExpectType(var.LineNumber(), "initializer of variable", declared_type,
|
||||
type_checked_initializer.type);
|
||||
return &d;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1095,7 +1095,7 @@ static void TopLevel(const Declaration& d, TypeCheckContext* tops) {
|
||||
}
|
||||
}
|
||||
|
||||
auto TopLevel(const std::list<const Declaration*>& fs) -> TypeCheckContext {
|
||||
auto TopLevel(const std::list<Ptr<const Declaration>>& fs) -> TypeCheckContext {
|
||||
TypeCheckContext tops;
|
||||
bool found_main = false;
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
#include "executable_semantics/interpreter/dictionary.h"
|
||||
#include "executable_semantics/interpreter/interpreter.h"
|
||||
|
||||
@@ -55,9 +56,9 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
const Value*& ret_type, bool is_omitted_ret_type)
|
||||
-> TCStatement;
|
||||
|
||||
auto MakeTypeChecked(const Declaration& decl, const TypeEnv& types,
|
||||
const Env& values) -> const Declaration*;
|
||||
auto TopLevel(const std::list<const Declaration*>& fs) -> TypeCheckContext;
|
||||
auto MakeTypeChecked(const Ptr<const Declaration> d, const TypeEnv& types,
|
||||
const Env& values) -> Ptr<const Declaration>;
|
||||
auto TopLevel(const std::list<Ptr<const Declaration>>& fs) -> TypeCheckContext;
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
#include "executable_semantics/ast/paren_contents.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -95,7 +96,7 @@ void Carbon::Parser::error(const location_type&, const std::string& message) {
|
||||
%type <const Declaration*> declaration
|
||||
%type <const FunctionDefinition*> function_declaration
|
||||
%type <const FunctionDefinition*> function_definition
|
||||
%type <std::list<const Declaration*>> declaration_list
|
||||
%type <std::list<Ptr<const Declaration>>> declaration_list
|
||||
%type <const Statement*> statement
|
||||
%type <const Statement*> if_statement
|
||||
%type <const Statement*> optional_else
|
||||
@@ -584,11 +585,11 @@ declaration:
|
||||
;
|
||||
declaration_list:
|
||||
// Empty
|
||||
{ $$ = std::list<const Declaration*>(); }
|
||||
{ $$ = std::list<Ptr<const Declaration>>(); }
|
||||
| declaration declaration_list
|
||||
{
|
||||
$$ = $2;
|
||||
$$.push_front($1);
|
||||
$$.push_front(Ptr<const Declaration>($1));
|
||||
}
|
||||
;
|
||||
%%
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Carbon {
|
||||
|
||||
// Adds builtins, currently only Print(). Note Print() is experimental, not
|
||||
// standardized, but is made available for printing state in tests.
|
||||
static void AddIntrinsics(std::list<const Declaration*>* fs) {
|
||||
static void AddIntrinsics(std::list<Ptr<const Declaration>>* fs) {
|
||||
std::vector<TuplePattern::Field> print_fields = {TuplePattern::Field(
|
||||
"0", global_arena->RawNew<BindingPattern>(
|
||||
-1, "format_str",
|
||||
@@ -26,7 +26,7 @@ static void AddIntrinsics(std::list<const Declaration*>* fs) {
|
||||
global_arena->RawNew<IntrinsicExpression>(
|
||||
IntrinsicExpression::IntrinsicKind::Print),
|
||||
false);
|
||||
auto* print = global_arena->RawNew<FunctionDeclaration>(
|
||||
auto print = global_arena->New<FunctionDeclaration>(
|
||||
global_arena->RawNew<FunctionDefinition>(
|
||||
-1, "Print", std::vector<GenericBinding>(),
|
||||
global_arena->RawNew<TuplePattern>(-1, print_fields),
|
||||
@@ -36,11 +36,11 @@ static void AddIntrinsics(std::list<const Declaration*>* fs) {
|
||||
fs->insert(fs->begin(), print);
|
||||
}
|
||||
|
||||
void ExecProgram(std::list<const Declaration*> fs) {
|
||||
void ExecProgram(std::list<Ptr<const Declaration>> fs) {
|
||||
AddIntrinsics(&fs);
|
||||
if (tracing_output) {
|
||||
llvm::outs() << "********** source program **********\n";
|
||||
for (const auto* decl : fs) {
|
||||
for (const auto decl : fs) {
|
||||
llvm::outs() << *decl;
|
||||
}
|
||||
llvm::outs() << "********** type checking **********\n";
|
||||
@@ -49,14 +49,14 @@ void ExecProgram(std::list<const Declaration*> fs) {
|
||||
TypeCheckContext p = TopLevel(fs);
|
||||
TypeEnv top = p.types;
|
||||
Env ct_top = p.values;
|
||||
std::list<const Declaration*> new_decls;
|
||||
for (const auto* decl : fs) {
|
||||
new_decls.push_back(MakeTypeChecked(*decl, top, ct_top));
|
||||
std::list<Ptr<const Declaration>> new_decls;
|
||||
for (const auto decl : fs) {
|
||||
new_decls.push_back(MakeTypeChecked(decl, top, ct_top));
|
||||
}
|
||||
if (tracing_output) {
|
||||
llvm::outs() << "\n";
|
||||
llvm::outs() << "********** type checking complete **********\n";
|
||||
for (const auto* decl : new_decls) {
|
||||
for (const auto decl : new_decls) {
|
||||
llvm::outs() << *decl;
|
||||
}
|
||||
llvm::outs() << "********** starting execution **********\n";
|
||||
|
||||
@@ -12,11 +12,12 @@
|
||||
#include <list>
|
||||
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// Runs the top-level declaration list.
|
||||
void ExecProgram(std::list<const Declaration*> fs);
|
||||
void ExecProgram(std::list<Ptr<const Declaration>> fs);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
|
||||
Reference in New Issue
Block a user