mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 18:31:08 +01:00
Move all new's to global_arena, and remove ASAN disabling (#690)
Note this changes identifiers from char* to string to avoid malloc. Fixes #580
This commit is contained in:
@@ -33,6 +33,7 @@ cc_library(
|
||||
"//common:ostream",
|
||||
"//executable_semantics/ast:declaration",
|
||||
"//executable_semantics/ast:expression",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:error",
|
||||
"//executable_semantics/common:tracing_flag",
|
||||
"//executable_semantics/interpreter",
|
||||
|
||||
@@ -178,10 +178,7 @@ operator and its operand, leading to three more cases:
|
||||
|
||||
{identifier} {
|
||||
BEGIN(AFTER_OPERAND);
|
||||
int n = strlen(yytext);
|
||||
auto r = reinterpret_cast<char*>(malloc((n + 1) * sizeof(char)));
|
||||
strncpy(r, yytext, n + 1);
|
||||
return yy::parser::make_identifier(r, context.current_token_position);
|
||||
return yy::parser::make_identifier(yytext, context.current_token_position);
|
||||
}
|
||||
|
||||
{integer_literal} {
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/syntax/paren_contents.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -82,8 +83,8 @@ void yy::parser::error(const location_type&, const std::string& message) {
|
||||
} // %code
|
||||
|
||||
%token <int> integer_literal
|
||||
%token <char*> identifier
|
||||
%type <char*> designator
|
||||
%token <std::string> identifier
|
||||
%type <std::string> designator
|
||||
%type <Carbon::Declaration> declaration
|
||||
%type <Carbon::FunctionDefinition> function_declaration
|
||||
%type <Carbon::FunctionDefinition> function_definition
|
||||
@@ -310,17 +311,17 @@ pattern:
|
||||
non_expression_pattern
|
||||
{ $$ = $1; }
|
||||
| expression
|
||||
{ $$ = new Carbon::ExpressionPattern($1); }
|
||||
{ $$ = Carbon::global_arena->New<Carbon::ExpressionPattern>($1); }
|
||||
;
|
||||
non_expression_pattern:
|
||||
AUTO
|
||||
{ $$ = new Carbon::AutoPattern(yylineno); }
|
||||
{ $$ = Carbon::global_arena->New<Carbon::AutoPattern>(yylineno); }
|
||||
| binding_lhs ":" pattern
|
||||
{ $$ = new Carbon::BindingPattern(yylineno, $1, $3); }
|
||||
{ $$ = Carbon::global_arena->New<Carbon::BindingPattern>(yylineno, $1, $3); }
|
||||
| paren_pattern
|
||||
{ $$ = $1; }
|
||||
| expression tuple_pattern
|
||||
{ $$ = new Carbon::AlternativePattern(yylineno, $1, $2); }
|
||||
{ $$ = Carbon::global_arena->New<Carbon::AlternativePattern>(yylineno, $1, $2); }
|
||||
;
|
||||
binding_lhs:
|
||||
identifier { $$ = $1; }
|
||||
@@ -354,7 +355,7 @@ paren_pattern_contents:
|
||||
| paren_pattern_contents "," paren_expression_element
|
||||
{
|
||||
$$ = $1;
|
||||
$$.elements.push_back({.name = $3.name, .term = new Carbon::ExpressionPattern($3.term)});
|
||||
$$.elements.push_back({.name = $3.name, .term = Carbon::global_arena->New<Carbon::ExpressionPattern>($3.term)});
|
||||
}
|
||||
| paren_pattern_contents "," paren_pattern_element
|
||||
{
|
||||
@@ -376,24 +377,25 @@ tuple_pattern: paren_pattern_base
|
||||
// rules out the possibility of an `expression` at this point.
|
||||
maybe_empty_tuple_pattern:
|
||||
"(" ")"
|
||||
{ $$ = new Carbon::TuplePattern(yylineno, {}); }
|
||||
{ $$ = Carbon::global_arena->New<Carbon::TuplePattern>(yylineno, std::vector<Carbon::TuplePattern::Field>()); }
|
||||
| tuple_pattern
|
||||
{ $$ = $1; }
|
||||
;
|
||||
clause:
|
||||
CASE pattern DBLARROW statement
|
||||
{ $$ = new std::pair<const Carbon::Pattern*, const Carbon::Statement*>($2, $4); }
|
||||
{ $$ = Carbon::global_arena->New<std::pair<const Carbon::Pattern*, const Carbon::Statement*>>($2, $4); }
|
||||
| DEFAULT DBLARROW statement
|
||||
{
|
||||
auto vp = new Carbon::BindingPattern(
|
||||
yylineno, std::nullopt, new Carbon::AutoPattern(yylineno));
|
||||
$$ = new std::pair<const Carbon::Pattern*, const Carbon::Statement*>(vp, $3);
|
||||
auto vp = Carbon::global_arena->New<Carbon::BindingPattern>(
|
||||
yylineno, std::nullopt, Carbon::global_arena->New<Carbon::AutoPattern>(yylineno));
|
||||
$$ = Carbon::global_arena->New<std::pair<const Carbon::Pattern*, const Carbon::Statement*>>(vp, $3);
|
||||
}
|
||||
;
|
||||
clause_list:
|
||||
// Empty
|
||||
{
|
||||
$$ = new std::list<std::pair<const Carbon::Pattern*, const Carbon::Statement*>>();
|
||||
$$ = Carbon::global_arena->New<std::list<
|
||||
std::pair<const Carbon::Pattern*, const Carbon::Statement*>>>();
|
||||
}
|
||||
| clause clause_list
|
||||
{ $$ = $2; $$->push_front(*$1); }
|
||||
@@ -484,13 +486,13 @@ function_definition:
|
||||
FN identifier deduced_params maybe_empty_tuple_pattern return_type block
|
||||
{
|
||||
$$ = Carbon::FunctionDefinition(
|
||||
yylineno, $2, $3, $4, new Carbon::ExpressionPattern($5), $6);
|
||||
yylineno, $2, $3, $4, Carbon::global_arena->New<Carbon::ExpressionPattern>($5), $6);
|
||||
}
|
||||
| FN identifier deduced_params maybe_empty_tuple_pattern DBLARROW expression ";"
|
||||
{
|
||||
$$ = Carbon::FunctionDefinition(
|
||||
yylineno, $2, $3, $4,
|
||||
new Carbon::AutoPattern(yylineno),
|
||||
Carbon::global_arena->New<Carbon::AutoPattern>(yylineno),
|
||||
Carbon::Statement::MakeReturn(yylineno, $6));
|
||||
}
|
||||
;
|
||||
@@ -498,10 +500,10 @@ function_declaration:
|
||||
FN identifier deduced_params maybe_empty_tuple_pattern return_type ";"
|
||||
{
|
||||
$$ = Carbon::FunctionDefinition(
|
||||
yylineno, $2, $3, $4, new Carbon::ExpressionPattern($5), 0); }
|
||||
yylineno, $2, $3, $4, Carbon::global_arena->New<Carbon::ExpressionPattern>($5), 0); }
|
||||
;
|
||||
variable_declaration: identifier ":" pattern
|
||||
{ $$ = new Carbon::BindingPattern(yylineno, $1, $3); }
|
||||
{ $$ = Carbon::global_arena->New<Carbon::BindingPattern>(yylineno, $1, $3); }
|
||||
;
|
||||
member: VAR variable_declaration ";"
|
||||
{ $$ = Carbon::Member::MakeFieldMember(yylineno, $2); }
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "executable_semantics/syntax/syntax_helpers.h"
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/tracing_flag.h"
|
||||
#include "executable_semantics/interpreter/interpreter.h"
|
||||
#include "executable_semantics/interpreter/typecheck.h"
|
||||
@@ -19,7 +20,7 @@ void ExecProgram(std::list<Declaration>* fs) {
|
||||
}
|
||||
llvm::outs() << "********** type checking **********\n";
|
||||
}
|
||||
state = new State(); // Compile-time state.
|
||||
state = global_arena->New<State>(); // Compile-time state.
|
||||
TypeCheckContext p = TopLevel(fs);
|
||||
TypeEnv top = p.types;
|
||||
Env ct_top = p.values;
|
||||
|
||||
Reference in New Issue
Block a user