Switch Declaration to use inheritance+cast (#714)

This commit is contained in:
Jon Meow
2021-08-06 14:57:44 -07:00
committed by GitHub
parent 7c03858b55
commit c1148caf7d
11 changed files with 206 additions and 202 deletions
+8 -8
View File
@@ -91,10 +91,10 @@ void Carbon::Parser::error(const location_type&, const std::string& message) {
%token <std::string> identifier
%token <std::string> sized_type_literal
%type <std::string> designator
%type <Declaration> declaration
%type <const Declaration*> declaration
%type <FunctionDefinition> function_declaration
%type <FunctionDefinition> function_definition
%type <std::list<Declaration>> declaration_list
%type <std::list<const Declaration*>> declaration_list
%type <const Statement*> statement
%type <const Statement*> if_statement
%type <const Statement*> optional_else
@@ -558,25 +558,25 @@ alternative_list:
;
declaration:
function_definition
{ $$ = Declaration::MakeFunctionDeclaration(std::move($1)); }
{ $$ = global_arena->New<FunctionDeclaration>(std::move($1)); }
| function_declaration
{ $$ = Declaration::MakeFunctionDeclaration(std::move($1)); }
{ $$ = global_arena->New<FunctionDeclaration>(std::move($1)); }
| STRUCT identifier "{" member_list "}"
{
$$ = Declaration::MakeStructDeclaration(yylineno, $2, $4);
$$ = global_arena->New<StructDeclaration>(yylineno, $2, $4);
}
| CHOICE identifier "{" alternative_list "}"
{
$$ = Declaration::MakeChoiceDeclaration(yylineno, $2, $4);
$$ = global_arena->New<ChoiceDeclaration>(yylineno, $2, $4);
}
| VAR variable_declaration "=" expression ";"
{
$$ = Declaration::MakeVariableDeclaration(yylineno, $2, $4);
$$ = global_arena->New<VariableDeclaration>(yylineno, $2, $4);
}
;
declaration_list:
// Empty
{ $$ = std::list<Declaration>(); }
{ $$ = std::list<const Declaration*>(); }
| declaration declaration_list
{
$$ = $2;
@@ -12,11 +12,11 @@
namespace Carbon {
void ExecProgram(std::list<Declaration>* fs) {
void ExecProgram(const std::list<const Declaration*>& fs) {
if (tracing_output) {
llvm::outs() << "********** source program **********\n";
for (const auto& decl : *fs) {
llvm::outs() << decl;
for (const auto* decl : fs) {
llvm::outs() << *decl;
}
llvm::outs() << "********** type checking **********\n";
}
@@ -24,19 +24,19 @@ void ExecProgram(std::list<Declaration>* fs) {
TypeCheckContext p = TopLevel(fs);
TypeEnv top = p.types;
Env ct_top = p.values;
std::list<Declaration> new_decls;
for (const auto& decl : *fs) {
new_decls.push_back(MakeTypeChecked(decl, top, ct_top));
std::list<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) {
llvm::outs() << decl;
for (const auto* decl : new_decls) {
llvm::outs() << *decl;
}
llvm::outs() << "********** starting execution **********\n";
}
int result = InterpProgram(&new_decls);
int result = InterpProgram(new_decls);
llvm::outs() << "result: " << result << "\n";
}
+1 -1
View File
@@ -16,7 +16,7 @@
namespace Carbon {
// Runs the top-level declaration list.
void ExecProgram(std::list<Declaration>* fs);
void ExecProgram(const std::list<const Declaration*>& fs);
} // namespace Carbon