Fix Torque memory leaks identified by ASAN
Bug: v8:7666 Change-Id: Ida9b6f964261bad75a4eb5d567ad37ec82569bcc Reviewed-on: https://chromium-review.googlesource.com/1023061 Commit-Queue: Daniel Clifford <danno@chromium.org> Reviewed-by: Tobias Tebbi <tebbi@chromium.org> Cr-Commit-Position: refs/heads/master@{#52751}
This commit is contained in:
parent
ba40d7c2b6
commit
480cc98950
@ -104,8 +104,8 @@ antlrcpp::Any AstGenerator::visitTypeListMaybeVarArgs(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitModuleDeclaration(
|
||||
TorqueParser::ModuleDeclarationContext* context) {
|
||||
ModuleDeclaration* result = new ExplicitModuleDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(), {}};
|
||||
ModuleDeclaration* result = RegisterNode(new ExplicitModuleDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(), {}});
|
||||
for (auto* declaration : context->declaration()) {
|
||||
result->declarations.push_back(
|
||||
declaration->accept(this).as<Declaration*>());
|
||||
@ -115,27 +115,27 @@ antlrcpp::Any AstGenerator::visitModuleDeclaration(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitMacroDeclaration(
|
||||
TorqueParser::MacroDeclarationContext* context) {
|
||||
return base::implicit_cast<Declaration*>(new MacroDeclaration{
|
||||
return base::implicit_cast<Declaration*>(RegisterNode(new MacroDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(),
|
||||
std::move(context->parameterList()->accept(this).as<ParameterList>()),
|
||||
GetOptionalType(context->optionalType()),
|
||||
GetOptionalLabelAndTypeList(context->optionalLabelList()),
|
||||
context->helperBody()->accept(this).as<Statement*>()});
|
||||
context->helperBody()->accept(this).as<Statement*>()}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitBuiltinDeclaration(
|
||||
TorqueParser::BuiltinDeclarationContext* context) {
|
||||
return base::implicit_cast<Declaration*>(new BuiltinDeclaration{
|
||||
return base::implicit_cast<Declaration*>(RegisterNode(new BuiltinDeclaration{
|
||||
Pos(context), context->JAVASCRIPT() != nullptr,
|
||||
context->IDENTIFIER()->getSymbol()->getText(),
|
||||
std::move(context->parameterList()->accept(this).as<ParameterList>()),
|
||||
GetOptionalType(context->optionalType()),
|
||||
context->helperBody()->accept(this).as<Statement*>()});
|
||||
context->helperBody()->accept(this).as<Statement*>()}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitExternalMacro(
|
||||
TorqueParser::ExternalMacroContext* context) {
|
||||
ExternalMacroDeclaration* result = new ExternalMacroDeclaration{
|
||||
ExternalMacroDeclaration* result = RegisterNode(new ExternalMacroDeclaration{
|
||||
Pos(context),
|
||||
context->IDENTIFIER()->getSymbol()->getText(),
|
||||
context->IMPLICIT() != nullptr,
|
||||
@ -143,7 +143,7 @@ antlrcpp::Any AstGenerator::visitExternalMacro(
|
||||
std::move(
|
||||
context->typeListMaybeVarArgs()->accept(this).as<ParameterList>()),
|
||||
GetOptionalType(context->optionalType()),
|
||||
GetOptionalLabelAndTypeList(context->optionalLabelList())};
|
||||
GetOptionalLabelAndTypeList(context->optionalLabelList())});
|
||||
if (auto* op = context->STRING_LITERAL())
|
||||
result->op = StringLiteralUnquote(op->getSymbol()->getText());
|
||||
return base::implicit_cast<Declaration*>(result);
|
||||
@ -151,34 +151,38 @@ antlrcpp::Any AstGenerator::visitExternalMacro(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitExternalBuiltin(
|
||||
TorqueParser::ExternalBuiltinContext* context) {
|
||||
return base::implicit_cast<Declaration*>(new ExternalBuiltinDeclaration{
|
||||
Pos(context), context->JAVASCRIPT() != nullptr,
|
||||
context->IDENTIFIER()->getSymbol()->getText(),
|
||||
std::move(context->typeList()->accept(this).as<ParameterList>()),
|
||||
GetOptionalType(context->optionalType())});
|
||||
return base::implicit_cast<Declaration*>(
|
||||
RegisterNode(new ExternalBuiltinDeclaration{
|
||||
Pos(context), context->JAVASCRIPT() != nullptr,
|
||||
context->IDENTIFIER()->getSymbol()->getText(),
|
||||
std::move(context->typeList()->accept(this).as<ParameterList>()),
|
||||
GetOptionalType(context->optionalType())}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitExternalRuntime(
|
||||
TorqueParser::ExternalRuntimeContext* context) {
|
||||
return base::implicit_cast<Declaration*>(new ExternalRuntimeDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(),
|
||||
std::move(
|
||||
context->typeListMaybeVarArgs()->accept(this).as<ParameterList>()),
|
||||
GetOptionalType(context->optionalType())});
|
||||
return base::implicit_cast<Declaration*>(
|
||||
RegisterNode(new ExternalRuntimeDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(),
|
||||
std::move(context->typeListMaybeVarArgs()
|
||||
->accept(this)
|
||||
.as<ParameterList>()),
|
||||
GetOptionalType(context->optionalType())}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitConstDeclaration(
|
||||
TorqueParser::ConstDeclarationContext* context) {
|
||||
return base::implicit_cast<Declaration*>(new ConstDeclaration{
|
||||
return base::implicit_cast<Declaration*>(RegisterNode(new ConstDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(),
|
||||
context->type()->IDENTIFIER()->getSymbol()->getText(),
|
||||
StringLiteralUnquote(context->STRING_LITERAL()->getSymbol()->getText())});
|
||||
StringLiteralUnquote(
|
||||
context->STRING_LITERAL()->getSymbol()->getText())}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitTypeDeclaration(
|
||||
TorqueParser::TypeDeclarationContext* context) {
|
||||
TypeDeclaration* result = new TypeDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(), {}, {}};
|
||||
TypeDeclaration* result = RegisterNode(new TypeDeclaration{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText(), {}, {}});
|
||||
if (context->extendsDeclaration())
|
||||
result->extends =
|
||||
context->extendsDeclaration()->IDENTIFIER()->getSymbol()->getText();
|
||||
@ -193,11 +197,11 @@ antlrcpp::Any AstGenerator::visitTypeDeclaration(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitVariableDeclaration(
|
||||
TorqueParser::VariableDeclarationContext* context) {
|
||||
return new VarDeclarationStatement{
|
||||
return RegisterNode(new VarDeclarationStatement{
|
||||
Pos(context),
|
||||
context->IDENTIFIER()->getSymbol()->getText(),
|
||||
context->type()->IDENTIFIER()->getSymbol()->getText(),
|
||||
{}};
|
||||
{}});
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitVariableDeclarationWithInitialization(
|
||||
@ -223,8 +227,8 @@ antlrcpp::Any AstGenerator::visitHelperCall(
|
||||
for (auto label : context->optionalOtherwise()->IDENTIFIER()) {
|
||||
labels.push_back(label->getSymbol()->getText());
|
||||
}
|
||||
CallExpression* result = new CallExpression{
|
||||
Pos(context), callee->getSymbol()->getText(), is_operator, {}, labels};
|
||||
CallExpression* result = RegisterNode(new CallExpression{
|
||||
Pos(context), callee->getSymbol()->getText(), is_operator, {}, labels});
|
||||
for (auto* arg : context->argumentList()->argument()) {
|
||||
result->arguments.push_back(arg->accept(this).as<Expression*>());
|
||||
}
|
||||
@ -235,21 +239,21 @@ antlrcpp::Any AstGenerator::visitHelperCallStatement(
|
||||
TorqueParser::HelperCallStatementContext* context) {
|
||||
Statement* result;
|
||||
if (context->TAIL()) {
|
||||
result = new TailCallStatement{
|
||||
result = RegisterNode(new TailCallStatement{
|
||||
Pos(context),
|
||||
CallExpression::cast(
|
||||
context->helperCall()->accept(this).as<Expression*>())};
|
||||
context->helperCall()->accept(this).as<Expression*>())});
|
||||
} else {
|
||||
result = new ExpressionStatement{
|
||||
Pos(context), context->helperCall()->accept(this).as<Expression*>()};
|
||||
result = RegisterNode(new ExpressionStatement{
|
||||
Pos(context), context->helperCall()->accept(this).as<Expression*>()});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitStatementScope(
|
||||
TorqueParser::StatementScopeContext* context) {
|
||||
BlockStatement* result =
|
||||
new BlockStatement{Pos(context), context->DEFERRED() != nullptr, {}};
|
||||
BlockStatement* result = RegisterNode(
|
||||
new BlockStatement{Pos(context), context->DEFERRED() != nullptr, {}});
|
||||
for (auto* child : context->statementList()->statement()) {
|
||||
result->statements.push_back(child->accept(this).as<Statement*>());
|
||||
}
|
||||
@ -258,29 +262,31 @@ antlrcpp::Any AstGenerator::visitStatementScope(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitExpressionStatement(
|
||||
TorqueParser::ExpressionStatementContext* context) {
|
||||
return base::implicit_cast<Statement*>(new ExpressionStatement{
|
||||
Pos(context), context->assignment()->accept(this).as<Expression*>()});
|
||||
return base::implicit_cast<Statement*>(RegisterNode(new ExpressionStatement{
|
||||
Pos(context), context->assignment()->accept(this).as<Expression*>()}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitReturnStatement(
|
||||
TorqueParser::ReturnStatementContext* context) {
|
||||
return base::implicit_cast<Statement*>(new ReturnStatement{
|
||||
Pos(context), context->expression()->accept(this).as<Expression*>()});
|
||||
return base::implicit_cast<Statement*>(RegisterNode(new ReturnStatement{
|
||||
Pos(context), context->expression()->accept(this).as<Expression*>()}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitBreakStatement(
|
||||
TorqueParser::BreakStatementContext* context) {
|
||||
return base::implicit_cast<Statement*>(new BreakStatement{Pos(context)});
|
||||
return base::implicit_cast<Statement*>(
|
||||
RegisterNode(new BreakStatement{Pos(context)}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitContinueStatement(
|
||||
TorqueParser::ContinueStatementContext* context) {
|
||||
return base::implicit_cast<Statement*>(new ContinueStatement{Pos(context)});
|
||||
return base::implicit_cast<Statement*>(
|
||||
RegisterNode(new ContinueStatement{Pos(context)}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitGotoStatement(
|
||||
TorqueParser::GotoStatementContext* context) {
|
||||
GotoStatement* result = new GotoStatement{Pos(context), {}, {}};
|
||||
GotoStatement* result = RegisterNode(new GotoStatement{Pos(context), {}, {}});
|
||||
if (context->labelReference())
|
||||
result->label =
|
||||
context->labelReference()->IDENTIFIER()->getSymbol()->getText();
|
||||
@ -294,11 +300,11 @@ antlrcpp::Any AstGenerator::visitGotoStatement(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitIfStatement(
|
||||
TorqueParser::IfStatementContext* context) {
|
||||
IfStatement* result = new IfStatement{
|
||||
IfStatement* result = RegisterNode(new IfStatement{
|
||||
Pos(context),
|
||||
std::move(context->expression()->accept(this).as<Expression*>()),
|
||||
std::move(context->statementBlock(0)->accept(this).as<Statement*>()),
|
||||
{}};
|
||||
{}});
|
||||
if (context->statementBlock(1))
|
||||
result->if_false =
|
||||
std::move(context->statementBlock(1)->accept(this).as<Statement*>());
|
||||
@ -307,19 +313,19 @@ antlrcpp::Any AstGenerator::visitIfStatement(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitWhileLoop(
|
||||
TorqueParser::WhileLoopContext* context) {
|
||||
return base::implicit_cast<Statement*>(new WhileStatement{
|
||||
return base::implicit_cast<Statement*>(RegisterNode(new WhileStatement{
|
||||
Pos(context), context->expression()->accept(this).as<Expression*>(),
|
||||
context->statementBlock()->accept(this).as<Statement*>()});
|
||||
context->statementBlock()->accept(this).as<Statement*>()}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitForLoop(
|
||||
TorqueParser::ForLoopContext* context) {
|
||||
ForLoopStatement* result = new ForLoopStatement{
|
||||
ForLoopStatement* result = RegisterNode(new ForLoopStatement{
|
||||
Pos(context),
|
||||
{},
|
||||
context->expression()->accept(this).as<Expression*>(),
|
||||
context->assignment()->accept(this).as<Expression*>(),
|
||||
context->statementBlock()->accept(this).as<Statement*>()};
|
||||
context->statementBlock()->accept(this).as<Statement*>()});
|
||||
if (auto* init = context->forInitialization()
|
||||
->variableDeclarationWithInitialization()) {
|
||||
result->var_declaration =
|
||||
@ -330,7 +336,7 @@ antlrcpp::Any AstGenerator::visitForLoop(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitForOfLoop(
|
||||
TorqueParser::ForOfLoopContext* context) {
|
||||
ForOfLoopStatement* result = new ForOfLoopStatement{
|
||||
ForOfLoopStatement* result = RegisterNode(new ForOfLoopStatement{
|
||||
Pos(context),
|
||||
context->variableDeclaration()
|
||||
->accept(this)
|
||||
@ -338,7 +344,7 @@ antlrcpp::Any AstGenerator::visitForOfLoop(
|
||||
context->expression()->accept(this).as<Expression*>(),
|
||||
{},
|
||||
{},
|
||||
context->statementBlock()->accept(this).as<Statement*>()};
|
||||
context->statementBlock()->accept(this).as<Statement*>()});
|
||||
if (auto* range = context->forOfRange()->rangeSpecifier()) {
|
||||
if (auto* begin = range->begin) {
|
||||
result->begin = begin->accept(this).as<Expression*>();
|
||||
@ -352,16 +358,16 @@ antlrcpp::Any AstGenerator::visitForOfLoop(
|
||||
|
||||
antlrcpp::Any AstGenerator::visitTryCatch(
|
||||
TorqueParser::TryCatchContext* context) {
|
||||
TryCatchStatement* result = new TryCatchStatement{
|
||||
TryCatchStatement* result = RegisterNode(new TryCatchStatement{
|
||||
Pos(context),
|
||||
context->statementBlock()->accept(this).as<Statement*>(),
|
||||
{}};
|
||||
{}});
|
||||
for (auto* handler : context->handlerWithStatement()) {
|
||||
if (handler->CATCH() != nullptr) {
|
||||
CatchBlock* catch_block = new CatchBlock{
|
||||
CatchBlock* catch_block = RegisterNode(new CatchBlock{
|
||||
Pos(handler->statementBlock()),
|
||||
{},
|
||||
handler->statementBlock()->accept(this).as<Statement*>()};
|
||||
handler->statementBlock()->accept(this).as<Statement*>()});
|
||||
catch_block->caught = handler->IDENTIFIER()->getSymbol()->getText();
|
||||
result->catch_blocks.push_back(catch_block);
|
||||
} else {
|
||||
@ -373,11 +379,11 @@ antlrcpp::Any AstGenerator::visitTryCatch(
|
||||
->parameterList()
|
||||
->accept(this)
|
||||
.as<ParameterList>();
|
||||
LabelBlock* label_block = new LabelBlock{
|
||||
LabelBlock* label_block = RegisterNode(new LabelBlock{
|
||||
Pos(handler->statementBlock()),
|
||||
handler->labelDeclaration()->IDENTIFIER()->getSymbol()->getText(),
|
||||
label_parameters,
|
||||
handler->statementBlock()->accept(this).as<Statement*>()};
|
||||
handler->statementBlock()->accept(this).as<Statement*>()});
|
||||
result->label_blocks.push_back(label_block);
|
||||
}
|
||||
}
|
||||
@ -388,20 +394,20 @@ antlrcpp::Any AstGenerator::visitPrimaryExpression(
|
||||
TorqueParser::PrimaryExpressionContext* context) {
|
||||
if (auto* e = context->helperCall()) return e->accept(this);
|
||||
if (auto* e = context->DECIMAL_LITERAL())
|
||||
return base::implicit_cast<Expression*>(
|
||||
new NumberLiteralExpression{Pos(context), e->getSymbol()->getText()});
|
||||
return base::implicit_cast<Expression*>(RegisterNode(
|
||||
new NumberLiteralExpression{Pos(context), e->getSymbol()->getText()}));
|
||||
if (auto* e = context->STRING_LITERAL())
|
||||
return base::implicit_cast<Expression*>(
|
||||
new StringLiteralExpression{Pos(context), e->getSymbol()->getText()});
|
||||
return base::implicit_cast<Expression*>(RegisterNode(
|
||||
new StringLiteralExpression{Pos(context), e->getSymbol()->getText()}));
|
||||
if (context->CONVERT_KEYWORD())
|
||||
return base::implicit_cast<Expression*>(new ConvertExpression{
|
||||
return base::implicit_cast<Expression*>(RegisterNode(new ConvertExpression{
|
||||
Pos(context), context->type()->IDENTIFIER()->getSymbol()->getText(),
|
||||
context->expression()->accept(this).as<Expression*>()});
|
||||
context->expression()->accept(this).as<Expression*>()}));
|
||||
if (context->CAST_KEYWORD())
|
||||
return base::implicit_cast<Expression*>(new CastExpression{
|
||||
return base::implicit_cast<Expression*>(RegisterNode(new CastExpression{
|
||||
Pos(context), context->type()->IDENTIFIER()->getSymbol()->getText(),
|
||||
context->IDENTIFIER()->getSymbol()->getText(),
|
||||
context->expression()->accept(this).as<Expression*>()});
|
||||
context->expression()->accept(this).as<Expression*>()}));
|
||||
return context->expression()->accept(this);
|
||||
}
|
||||
|
||||
@ -411,8 +417,8 @@ antlrcpp::Any AstGenerator::visitAssignment(
|
||||
LocationExpression* location = LocationExpression::cast(
|
||||
context->locationExpression()->accept(this).as<Expression*>());
|
||||
if (auto* e = context->expression()) {
|
||||
AssignmentExpression* result = new AssignmentExpression{
|
||||
Pos(context), location, {}, e->accept(this).as<Expression*>()};
|
||||
AssignmentExpression* result = RegisterNode(new AssignmentExpression{
|
||||
Pos(context), location, {}, e->accept(this).as<Expression*>()});
|
||||
if (auto* op_node = context->ASSIGNMENT_OPERATOR()) {
|
||||
std::string op = op_node->getSymbol()->getText();
|
||||
result->op = op.substr(0, op.length() - 1);
|
||||
@ -425,13 +431,14 @@ antlrcpp::Any AstGenerator::visitAssignment(
|
||||
antlrcpp::Any AstGenerator::visitIncrementDecrement(
|
||||
TorqueParser::IncrementDecrementContext* context) {
|
||||
bool postfix = context->op;
|
||||
return base::implicit_cast<Expression*>(new IncrementDecrementExpression{
|
||||
Pos(context),
|
||||
LocationExpression::cast(
|
||||
context->locationExpression()->accept(this).as<Expression*>()),
|
||||
context->INCREMENT() ? IncrementDecrementOperator::kIncrement
|
||||
: IncrementDecrementOperator::kDecrement,
|
||||
postfix});
|
||||
return base::implicit_cast<Expression*>(
|
||||
RegisterNode(new IncrementDecrementExpression{
|
||||
Pos(context),
|
||||
LocationExpression::cast(
|
||||
context->locationExpression()->accept(this).as<Expression*>()),
|
||||
context->INCREMENT() ? IncrementDecrementOperator::kIncrement
|
||||
: IncrementDecrementOperator::kDecrement,
|
||||
postfix}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitLocationExpression(
|
||||
@ -439,14 +446,17 @@ antlrcpp::Any AstGenerator::visitLocationExpression(
|
||||
if (auto* l = context->locationExpression()) {
|
||||
Expression* location = l->accept(this).as<Expression*>();
|
||||
if (auto* e = context->expression()) {
|
||||
return base::implicit_cast<Expression*>(new ElementAccessExpression{
|
||||
Pos(context), location, e->accept(this).as<Expression*>()});
|
||||
return base::implicit_cast<Expression*>(
|
||||
RegisterNode(new ElementAccessExpression{
|
||||
Pos(context), location, e->accept(this).as<Expression*>()}));
|
||||
}
|
||||
return base::implicit_cast<Expression*>(new FieldAccessExpression{
|
||||
Pos(context), location, context->IDENTIFIER()->getSymbol()->getText()});
|
||||
return base::implicit_cast<Expression*>(
|
||||
RegisterNode(new FieldAccessExpression{
|
||||
Pos(context), location,
|
||||
context->IDENTIFIER()->getSymbol()->getText()}));
|
||||
}
|
||||
return base::implicit_cast<Expression*>(new IdentifierExpression{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText()});
|
||||
return base::implicit_cast<Expression*>(RegisterNode(new IdentifierExpression{
|
||||
Pos(context), context->IDENTIFIER()->getSymbol()->getText()}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitUnaryExpression(
|
||||
@ -454,8 +464,8 @@ antlrcpp::Any AstGenerator::visitUnaryExpression(
|
||||
if (auto* e = context->assignmentExpression()) return e->accept(this);
|
||||
std::vector<Expression*> args;
|
||||
args.push_back(context->unaryExpression()->accept(this).as<Expression*>());
|
||||
return base::implicit_cast<Expression*>(new CallExpression{
|
||||
Pos(context), context->op->getText(), true, std::move(args), {}});
|
||||
return base::implicit_cast<Expression*>(RegisterNode(new CallExpression{
|
||||
Pos(context), context->op->getText(), true, std::move(args), {}}));
|
||||
}
|
||||
|
||||
antlrcpp::Any AstGenerator::visitMultiplicativeExpression(
|
||||
@ -463,12 +473,12 @@ antlrcpp::Any AstGenerator::visitMultiplicativeExpression(
|
||||
auto* right = context->unaryExpression();
|
||||
if (auto* left = context->multiplicativeExpression()) {
|
||||
return base::implicit_cast<Expression*>(
|
||||
new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}});
|
||||
RegisterNode(new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -478,12 +488,12 @@ antlrcpp::Any AstGenerator::visitAdditiveExpression(
|
||||
auto* right = context->multiplicativeExpression();
|
||||
if (auto* left = context->additiveExpression()) {
|
||||
return base::implicit_cast<Expression*>(
|
||||
new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}});
|
||||
RegisterNode(new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -493,12 +503,12 @@ antlrcpp::Any AstGenerator::visitShiftExpression(
|
||||
auto* right = context->additiveExpression();
|
||||
if (auto* left = context->shiftExpression()) {
|
||||
return base::implicit_cast<Expression*>(
|
||||
new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}});
|
||||
RegisterNode(new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -508,12 +518,12 @@ antlrcpp::Any AstGenerator::visitRelationalExpression(
|
||||
auto* right = context->shiftExpression();
|
||||
if (auto* left = context->relationalExpression()) {
|
||||
return base::implicit_cast<Expression*>(
|
||||
new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}});
|
||||
RegisterNode(new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -523,12 +533,12 @@ antlrcpp::Any AstGenerator::visitEqualityExpression(
|
||||
auto* right = context->relationalExpression();
|
||||
if (auto* left = context->equalityExpression()) {
|
||||
return base::implicit_cast<Expression*>(
|
||||
new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}});
|
||||
RegisterNode(new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -538,12 +548,12 @@ antlrcpp::Any AstGenerator::visitBitwiseExpression(
|
||||
auto* right = context->equalityExpression();
|
||||
if (auto* left = context->bitwiseExpression()) {
|
||||
return base::implicit_cast<Expression*>(
|
||||
new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}});
|
||||
RegisterNode(new CallExpression{Pos(context),
|
||||
context->op->getText(),
|
||||
true,
|
||||
{left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()},
|
||||
{}}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -552,9 +562,10 @@ antlrcpp::Any AstGenerator::visitLogicalANDExpression(
|
||||
TorqueParser::LogicalANDExpressionContext* context) {
|
||||
auto* right = context->bitwiseExpression();
|
||||
if (auto* left = context->logicalANDExpression()) {
|
||||
return base::implicit_cast<Expression*>(new LogicalAndExpression{
|
||||
Pos(context), left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()});
|
||||
return base::implicit_cast<Expression*>(
|
||||
RegisterNode(new LogicalAndExpression{
|
||||
Pos(context), left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -563,9 +574,10 @@ antlrcpp::Any AstGenerator::visitLogicalORExpression(
|
||||
TorqueParser::LogicalORExpressionContext* context) {
|
||||
auto* right = context->logicalANDExpression();
|
||||
if (auto* left = context->logicalORExpression()) {
|
||||
return base::implicit_cast<Expression*>(new LogicalOrExpression{
|
||||
Pos(context), left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()});
|
||||
return base::implicit_cast<Expression*>(
|
||||
RegisterNode(new LogicalOrExpression{
|
||||
Pos(context), left->accept(this).as<Expression*>(),
|
||||
right->accept(this).as<Expression*>()}));
|
||||
}
|
||||
return right->accept(this);
|
||||
}
|
||||
@ -573,12 +585,13 @@ antlrcpp::Any AstGenerator::visitLogicalORExpression(
|
||||
antlrcpp::Any AstGenerator::visitConditionalExpression(
|
||||
TorqueParser::ConditionalExpressionContext* context) {
|
||||
if (auto* condition = context->conditionalExpression()) {
|
||||
return base::implicit_cast<Expression*>(new ConditionalExpression{
|
||||
Pos(context), condition->accept(this).as<Expression*>(),
|
||||
return base::implicit_cast<Expression*>(
|
||||
RegisterNode(new ConditionalExpression{
|
||||
Pos(context), condition->accept(this).as<Expression*>(),
|
||||
|
||||
context->logicalORExpression(0)->accept(this).as<Expression*>(),
|
||||
context->logicalORExpression(0)->accept(this).as<Expression*>(),
|
||||
|
||||
context->logicalORExpression(1)->accept(this).as<Expression*>()});
|
||||
context->logicalORExpression(1)->accept(this).as<Expression*>()}));
|
||||
}
|
||||
return context->logicalORExpression(0)->accept(this);
|
||||
}
|
||||
|
@ -142,6 +142,12 @@ class AstGenerator : public TorqueBaseVisitor {
|
||||
return {};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T* RegisterNode(T* node) {
|
||||
ast_.AddNode(std::unique_ptr<AstNode>(node));
|
||||
return node;
|
||||
}
|
||||
|
||||
void visitSourceFile(SourceFileContext* context);
|
||||
|
||||
SourcePosition Pos(antlr4::ParserRuleContext* context);
|
||||
|
@ -81,6 +81,7 @@ struct AstNode {
|
||||
};
|
||||
|
||||
AstNode(Kind k, SourcePosition p) : kind(k), pos(p) {}
|
||||
virtual ~AstNode() {}
|
||||
|
||||
const Kind kind;
|
||||
SourcePosition pos;
|
||||
@ -167,6 +168,9 @@ class Ast {
|
||||
const std::vector<Declaration*>& declarations() const {
|
||||
return default_module_.declarations;
|
||||
}
|
||||
void AddNode(std::unique_ptr<AstNode> node) {
|
||||
nodes_.emplace_back(std::move(node));
|
||||
}
|
||||
SourceId AddSource(std::string path) {
|
||||
sources_.push_back(std::move(path));
|
||||
return static_cast<SourceId>(sources_.size() - 1);
|
||||
@ -183,6 +187,7 @@ class Ast {
|
||||
private:
|
||||
DefaultModuleDeclaration default_module_;
|
||||
std::vector<std::string> sources_;
|
||||
std::vector<std::unique_ptr<AstNode>> nodes_;
|
||||
};
|
||||
|
||||
struct CallExpression : Expression {
|
||||
|
@ -61,8 +61,6 @@ class Declarable {
|
||||
|
||||
class Value : public Declarable {
|
||||
public:
|
||||
Value(Kind kind, Type type, const std::string& name)
|
||||
: Declarable(kind), type_(type), name_(name) {}
|
||||
const std::string& name() const { return name_; }
|
||||
virtual bool IsConst() const { return true; }
|
||||
virtual std::string GetValueForDeclaration() const = 0;
|
||||
@ -73,6 +71,10 @@ class Value : public Declarable {
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Value, value);
|
||||
Type type() const { return type_; }
|
||||
|
||||
protected:
|
||||
Value(Kind kind, Type type, const std::string& name)
|
||||
: Declarable(kind), type_(type), name_(name) {}
|
||||
|
||||
private:
|
||||
Type type_;
|
||||
std::string name_;
|
||||
@ -80,21 +82,19 @@ class Value : public Declarable {
|
||||
|
||||
class Parameter : public Value {
|
||||
public:
|
||||
Parameter(const std::string& name, Type type, const std::string& var_name)
|
||||
: Value(Declarable::kParameter, type, name), var_name_(var_name) {}
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Parameter, parameter);
|
||||
std::string GetValueForDeclaration() const override { return var_name_; }
|
||||
|
||||
private:
|
||||
friend class Scope;
|
||||
Parameter(const std::string& name, Type type, const std::string& var_name)
|
||||
: Value(Declarable::kParameter, type, name), var_name_(var_name) {}
|
||||
|
||||
std::string var_name_;
|
||||
};
|
||||
|
||||
class Variable : public Value {
|
||||
public:
|
||||
Variable(const std::string& name, const std::string& value, Type type)
|
||||
: Value(Declarable::kVariable, type, name),
|
||||
value_(value),
|
||||
defined_(false) {}
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Variable, variable);
|
||||
bool IsConst() const override { return false; }
|
||||
std::string GetValueForDeclaration() const override { return value_; }
|
||||
@ -106,19 +106,19 @@ class Variable : public Value {
|
||||
bool IsDefined() const { return defined_; }
|
||||
|
||||
private:
|
||||
friend class Scope;
|
||||
Variable(const std::string& name, const std::string& value, Type type)
|
||||
: Value(Declarable::kVariable, type, name),
|
||||
value_(value),
|
||||
defined_(false) {}
|
||||
|
||||
std::string value_;
|
||||
bool defined_;
|
||||
};
|
||||
|
||||
class Label : public Value {
|
||||
public:
|
||||
explicit Label(const std::string& name) : Label(name, {}) {}
|
||||
Label(const std::string& name, const std::vector<Variable*>& parameters)
|
||||
: Value(Declarable::kLabel, Type(),
|
||||
"label_" + name + "_" + std::to_string(next_id_++)),
|
||||
source_name_(name),
|
||||
parameters_(parameters),
|
||||
used_(false) {}
|
||||
void AddVariable(Variable* var) { parameters_.push_back(var); }
|
||||
std::string GetSourceName() const { return source_name_; }
|
||||
std::string GetValueForDeclaration() const override { return name(); }
|
||||
Variable* GetParameter(size_t i) const { return parameters_[i]; }
|
||||
@ -130,6 +130,13 @@ class Label : public Value {
|
||||
bool IsUsed() const { return used_; }
|
||||
|
||||
private:
|
||||
friend class Scope;
|
||||
explicit Label(const std::string& name)
|
||||
: Value(Declarable::kLabel, Type(),
|
||||
"label_" + name + "_" + std::to_string(next_id_++)),
|
||||
source_name_(name),
|
||||
used_(false) {}
|
||||
|
||||
std::string source_name_;
|
||||
std::vector<Variable*> parameters_;
|
||||
static size_t next_id_;
|
||||
@ -138,25 +145,20 @@ class Label : public Value {
|
||||
|
||||
class Constant : public Value {
|
||||
public:
|
||||
explicit Constant(const std::string& name, Type type,
|
||||
const std::string& value)
|
||||
: Value(Declarable::kConstant, type, name), value_(value) {}
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Constant, constant);
|
||||
std::string GetValueForDeclaration() const override { return value_; }
|
||||
|
||||
private:
|
||||
friend class Scope;
|
||||
explicit Constant(const std::string& name, Type type,
|
||||
const std::string& value)
|
||||
: Value(Declarable::kConstant, type, name), value_(value) {}
|
||||
|
||||
std::string value_;
|
||||
};
|
||||
|
||||
class Callable : public Declarable {
|
||||
public:
|
||||
Callable(Declarable::Kind kind, const std::string& name, Scope* scope,
|
||||
const Signature& signature)
|
||||
: Declarable(kind),
|
||||
name_(name),
|
||||
scope_(scope),
|
||||
signature_(signature),
|
||||
returns_(0) {}
|
||||
static Callable* cast(Declarable* declarable) {
|
||||
assert(declarable->IsMacro() || declarable->IsBuiltin() ||
|
||||
declarable->IsRuntime());
|
||||
@ -179,6 +181,15 @@ class Callable : public Declarable {
|
||||
void IncrementReturns() { ++returns_; }
|
||||
bool HasReturns() const { return returns_; }
|
||||
|
||||
protected:
|
||||
Callable(Declarable::Kind kind, const std::string& name, Scope* scope,
|
||||
const Signature& signature)
|
||||
: Declarable(kind),
|
||||
name_(name),
|
||||
scope_(scope),
|
||||
signature_(signature),
|
||||
returns_(0) {}
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
Scope* scope_;
|
||||
@ -188,41 +199,39 @@ class Callable : public Declarable {
|
||||
|
||||
class Macro : public Callable {
|
||||
public:
|
||||
Macro(const std::string& name, Scope* scope, const Signature& signature)
|
||||
: Macro(Declarable::kMacro, name, scope, signature) {}
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Macro, macro);
|
||||
|
||||
void AddLabel(const LabelDeclaration& label) { labels_.push_back(label); }
|
||||
|
||||
const LabelDeclarationVector& GetLabels() { return labels_; }
|
||||
|
||||
protected:
|
||||
Macro(Declarable::Kind type, const std::string& name, Scope* scope,
|
||||
const Signature& signature)
|
||||
: Callable(type, name, scope, signature) {}
|
||||
|
||||
private:
|
||||
LabelDeclarationVector labels_;
|
||||
friend class Scope;
|
||||
Macro(const std::string& name, Scope* scope, const Signature& signature)
|
||||
: Macro(Declarable::kMacro, name, scope, signature) {}
|
||||
};
|
||||
|
||||
class MacroList : public Declarable {
|
||||
public:
|
||||
MacroList() : Declarable(Declarable::kMacroList) {}
|
||||
DECLARE_DECLARABLE_BOILERPLATE(MacroList, macro_list);
|
||||
const std::vector<Macro*>& list() { return list_; }
|
||||
void AddMacro(Macro* macro) { list_.push_back(macro); }
|
||||
const std::vector<std::unique_ptr<Macro>>& list() { return list_; }
|
||||
Macro* AddMacro(std::unique_ptr<Macro> macro) {
|
||||
Macro* result = macro.get();
|
||||
list_.emplace_back(std::move(macro));
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Macro*> list_;
|
||||
friend class Scope;
|
||||
MacroList() : Declarable(Declarable::kMacroList) {}
|
||||
|
||||
std::vector<std::unique_ptr<Macro>> list_;
|
||||
};
|
||||
|
||||
class Builtin : public Callable {
|
||||
public:
|
||||
enum Kind { kStub = 0, kFixedArgsJavaScript, kVarArgsJavaScript };
|
||||
|
||||
Builtin(const std::string& name, Kind kind, Scope* scope,
|
||||
const Signature& signature)
|
||||
: Callable(Declarable::kBuiltin, name, scope, signature), kind_(kind) {}
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Builtin, builtin);
|
||||
Kind kind() const { return kind_; }
|
||||
bool IsStub() const { return kind_ == kStub; }
|
||||
@ -230,14 +239,22 @@ class Builtin : public Callable {
|
||||
bool IsFixedArgsJavaScript() const { return kind_ == kFixedArgsJavaScript; }
|
||||
|
||||
private:
|
||||
friend class Scope;
|
||||
Builtin(const std::string& name, Builtin::Kind kind, Scope* scope,
|
||||
const Signature& signature)
|
||||
: Callable(Declarable::kBuiltin, name, scope, signature), kind_(kind) {}
|
||||
|
||||
Kind kind_;
|
||||
};
|
||||
|
||||
class Runtime : public Callable {
|
||||
public:
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Runtime, runtime);
|
||||
|
||||
private:
|
||||
friend class Scope;
|
||||
Runtime(const std::string& name, Scope* scope, const Signature& signature)
|
||||
: Callable(Declarable::kRuntime, name, scope, signature) {}
|
||||
DECLARE_DECLARABLE_BOILERPLATE(Runtime, runtime);
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Callable& m) {
|
||||
|
@ -98,7 +98,7 @@ void DeclarationVisitor::Visit(BuiltinDeclaration* decl) {
|
||||
Builtin* builtin = enclosing_scope->DeclareBuiltin(
|
||||
decl->pos, decl->name, kind, new_scope, signature);
|
||||
defined_builtins_.push_back(builtin);
|
||||
DeclareParameterList(decl->pos, signature);
|
||||
DeclareParameterList(decl->pos, signature, {});
|
||||
CurrentCallActivator activator(global_context_, builtin);
|
||||
Visit(decl->body);
|
||||
}
|
||||
@ -127,7 +127,7 @@ void DeclarationVisitor::Visit(MacroDeclaration* decl) {
|
||||
|
||||
Macro* macro = enclosing_scope->DeclareMacro(decl->pos, decl->name, new_scope,
|
||||
signature);
|
||||
DeclareParameterList(decl->pos, signature);
|
||||
DeclareParameterList(decl->pos, signature, decl->labels);
|
||||
CurrentCallActivator activator(global_context_, macro);
|
||||
Visit(decl->body);
|
||||
|
||||
@ -164,7 +164,7 @@ void DeclarationVisitor::Visit(TryCatchStatement* stmt) {
|
||||
|
||||
// Declare catch labels
|
||||
for (LabelBlock* block : stmt->label_blocks) {
|
||||
std::vector<Variable*> parameters;
|
||||
Label* shared_label = TopScope()->DeclareLabel(stmt->pos, block->label);
|
||||
{
|
||||
Scope::Activator s(global_context_, block->body);
|
||||
if (block->parameters.has_varargs) {
|
||||
@ -176,14 +176,12 @@ void DeclarationVisitor::Visit(TryCatchStatement* stmt) {
|
||||
|
||||
size_t i = 0;
|
||||
for (auto p : block->parameters.names) {
|
||||
parameters.push_back(TopScope()->DeclareVariable(
|
||||
shared_label->AddVariable(TopScope()->DeclareVariable(
|
||||
stmt->pos, p,
|
||||
GetTypeOracle().GetType(block->parameters.types[i])));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
Label* shared_label = new Label(block->label, parameters);
|
||||
TopScope()->DeclareLabel(stmt->pos, block->label, shared_label);
|
||||
if (global_context_.verbose()) {
|
||||
std::cout << " declaring catch for exception " << block->label << "\n";
|
||||
}
|
||||
|
@ -135,7 +135,6 @@ class DeclarationVisitor : public FileVisitor {
|
||||
Signature signature{{},
|
||||
{parameter_types, decl->parameters.has_varargs},
|
||||
return_type,
|
||||
{},
|
||||
{}};
|
||||
TopScope()->DeclareRuntime(decl->pos, decl->name, nullptr,
|
||||
std::move(signature));
|
||||
@ -392,16 +391,25 @@ class DeclarationVisitor : public FileVisitor {
|
||||
return was_live_in_preceeding_split;
|
||||
}
|
||||
|
||||
void DeclareParameterList(SourcePosition pos, const Signature& signature) {
|
||||
void DeclareParameterList(SourcePosition pos, const Signature& signature,
|
||||
base::Optional<LabelAndTypesVector> labels) {
|
||||
auto name_iterator = signature.parameter_names.begin();
|
||||
for (auto t : signature.types()) {
|
||||
const std::string& name(*name_iterator++);
|
||||
TopScope()->DeclareParameter(pos, name,
|
||||
GetParameterVariableFromName(name), t);
|
||||
}
|
||||
size_t i = 0;
|
||||
for (auto definition : signature.label_defintions) {
|
||||
TopScope()->DeclareLabel(pos, definition.name, signature.labels[i++]);
|
||||
if (labels) {
|
||||
for (auto label : *labels) {
|
||||
auto label_params = GetTypeVector(label.types);
|
||||
Label* new_label = TopScope()->DeclareLabel(pos, label.name);
|
||||
size_t i = 0;
|
||||
for (auto var_type : label_params) {
|
||||
std::string var_name = label.name + std::to_string(i++);
|
||||
new_label->AddVariable(
|
||||
TopScope()->DeclareVariable(pos, var_name, var_type));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,19 +21,7 @@ Signature FileVisitor::MakeSignature(const ParameterList& parameters,
|
||||
Signature result{parameters.names,
|
||||
{GetTypeVector(parameters.types), parameters.has_varargs},
|
||||
GetType(return_type),
|
||||
definition_vector,
|
||||
{}};
|
||||
for (auto label : labels) {
|
||||
auto label_params = GetTypeVector(label.types);
|
||||
std::vector<Variable*> vars;
|
||||
size_t i = 0;
|
||||
for (auto var_type : label_params) {
|
||||
std::string name = label.name + std::to_string(i++);
|
||||
Variable* var = new Variable(label.name, name, var_type);
|
||||
vars.push_back(var);
|
||||
}
|
||||
result.labels.push_back(new Label(label.name, vars));
|
||||
}
|
||||
definition_vector};
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -44,9 +44,7 @@ class FileVisitor {
|
||||
static constexpr const char* kFalseLabelName = "False";
|
||||
static constexpr const char* kReturnValueVariable = "return";
|
||||
static constexpr const char* kConditionValueVariable = "condition";
|
||||
static constexpr const char* kBreakLabelName = "break";
|
||||
static constexpr const char* kDoneLabelName = "done";
|
||||
static constexpr const char* kContinueLabelName = "continue";
|
||||
static constexpr const char* kForIndexValueVariable = "for_index";
|
||||
|
||||
Module* CurrentModule() const { return module_; }
|
||||
@ -81,10 +79,10 @@ class FileVisitor {
|
||||
} else if (declarable->IsRuntime()) {
|
||||
result = Runtime::cast(declarable);
|
||||
} else if (declarable->IsMacroList()) {
|
||||
for (auto m : MacroList::cast(declarable)->list()) {
|
||||
for (auto& m : MacroList::cast(declarable)->list()) {
|
||||
if (GetTypeOracle().IsCompatibleSignature(
|
||||
m->signature().parameter_types, parameter_types)) {
|
||||
result = m;
|
||||
result = m.get();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -115,10 +113,10 @@ class FileVisitor {
|
||||
Declarable* declarable = global_context_.Lookup(name);
|
||||
if (declarable != nullptr) {
|
||||
if (declarable->IsMacroList()) {
|
||||
for (auto m : MacroList::cast(declarable)->list()) {
|
||||
for (auto& m : MacroList::cast(declarable)->list()) {
|
||||
if (m->signature().parameter_types.types == types &&
|
||||
!m->signature().parameter_types.var_args) {
|
||||
return m;
|
||||
return m.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -154,7 +152,7 @@ class FileVisitor {
|
||||
|
||||
Signature MakeSignature(const ParameterList& parameters,
|
||||
const std::string& return_type,
|
||||
const LabelAndTypesVector& exceptions);
|
||||
const LabelAndTypesVector& labels);
|
||||
|
||||
GlobalContext& global_context_;
|
||||
Callable* current_callable_;
|
||||
|
@ -21,18 +21,19 @@ class TypeOracle;
|
||||
|
||||
class Module {
|
||||
public:
|
||||
Module(const std::string& name, GlobalContext& context)
|
||||
: name_(name), scope_(context) {}
|
||||
Module(const std::string& name, GlobalContext& context) : name_(name) {
|
||||
scope_ = new Scope(context);
|
||||
}
|
||||
const std::string& name() const { return name_; }
|
||||
std::ostream& source_stream() { return source_stream_; }
|
||||
std::ostream& header_stream() { return header_stream_; }
|
||||
std::string source() { return source_stream_.str(); }
|
||||
std::string header() { return header_stream_.str(); }
|
||||
Scope* scope() { return &scope_; }
|
||||
Scope* scope() { return scope_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
Scope scope_;
|
||||
Scope* scope_;
|
||||
std::stringstream header_stream_;
|
||||
std::stringstream source_stream_;
|
||||
};
|
||||
@ -46,10 +47,10 @@ class OperationHandler {
|
||||
|
||||
struct SourceFileContext {
|
||||
std::string name;
|
||||
antlr4::ANTLRFileStream* stream;
|
||||
TorqueLexer* lexer;
|
||||
antlr4::CommonTokenStream* tokens;
|
||||
TorqueParser* parser;
|
||||
std::unique_ptr<antlr4::ANTLRFileStream> stream;
|
||||
std::unique_ptr<TorqueLexer> lexer;
|
||||
std::unique_ptr<antlr4::CommonTokenStream> tokens;
|
||||
std::unique_ptr<TorqueParser> parser;
|
||||
TorqueParser::FileContext* file;
|
||||
|
||||
std::string sourceFileAndLineNumber(antlr4::ParserRuleContext* context) {
|
||||
@ -73,16 +74,18 @@ class GlobalContext {
|
||||
Module* GetModule(const std::string& name) {
|
||||
auto i = modules_.find(name);
|
||||
if (i != modules_.end()) {
|
||||
return i->second;
|
||||
return i->second.get();
|
||||
}
|
||||
Module* module = new Module(name, *this);
|
||||
modules_[name] = module;
|
||||
modules_[name] = std::unique_ptr<Module>(module);
|
||||
return module;
|
||||
}
|
||||
int GetNextScopeNumber() { return next_scope_number_++; }
|
||||
int GetNextLabelNumber() { return next_label_number_++; }
|
||||
|
||||
const std::map<std::string, Module*>& GetModules() const { return modules_; }
|
||||
const std::map<std::string, std::unique_ptr<Module>>& GetModules() const {
|
||||
return modules_;
|
||||
}
|
||||
|
||||
Scope* GetParserRuleContextScope(const AstNode* context) {
|
||||
auto i = context_scopes_.find(context);
|
||||
@ -92,11 +95,11 @@ class GlobalContext {
|
||||
return new_scope;
|
||||
}
|
||||
|
||||
Scope* TopScope() const { return scopes_.back(); }
|
||||
Scope* TopScope() const { return current_scopes_.back(); }
|
||||
|
||||
Declarable* Lookup(const std::string& name) const {
|
||||
auto e = scopes_.rend();
|
||||
auto c = scopes_.rbegin();
|
||||
auto e = current_scopes_.rend();
|
||||
auto c = current_scopes_.rbegin();
|
||||
while (c != e) {
|
||||
Declarable* result = (*c)->Lookup(name);
|
||||
if (result != nullptr) return result;
|
||||
@ -106,13 +109,17 @@ class GlobalContext {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void PushScope(Scope* scope) { scopes_.push_back(scope); }
|
||||
void RegisterScope(Scope* scope) {
|
||||
scopes_.emplace_back(std::unique_ptr<Scope>(scope));
|
||||
}
|
||||
|
||||
void PopScope() { scopes_.pop_back(); }
|
||||
void PushScope(Scope* scope) { current_scopes_.push_back(scope); }
|
||||
|
||||
void PopScope() { current_scopes_.pop_back(); }
|
||||
|
||||
std::set<const Variable*> GetLiveTypeVariables() {
|
||||
std::set<const Variable*> result;
|
||||
for (auto scope : scopes_) {
|
||||
for (auto scope : current_scopes_) {
|
||||
scope->AddLiveVariables(result);
|
||||
}
|
||||
return result;
|
||||
@ -138,15 +145,20 @@ class GlobalContext {
|
||||
}
|
||||
|
||||
friend class CurrentCallActivator;
|
||||
friend class BreakContinueActivator;
|
||||
|
||||
TypeOracle& GetTypeOracle() { return type_oracle_; }
|
||||
|
||||
Callable* GetCurrentCallable() const { return current_callable_; }
|
||||
Label* GetCurrentBreak() const { return break_continue_stack_.back().first; }
|
||||
Label* GetCurrentContinue() const {
|
||||
return break_continue_stack_.back().second;
|
||||
}
|
||||
|
||||
std::map<std::string, std::vector<OperationHandler>> op_handlers_;
|
||||
|
||||
void PrintScopeChain() {
|
||||
for (auto s : scopes_) {
|
||||
for (auto s : current_scopes_) {
|
||||
s->Print();
|
||||
}
|
||||
}
|
||||
@ -161,9 +173,11 @@ class GlobalContext {
|
||||
bool verbose_;
|
||||
int next_scope_number_;
|
||||
int next_label_number_;
|
||||
std::map<std::string, Module*> modules_;
|
||||
std::map<std::string, std::unique_ptr<Module>> modules_;
|
||||
std::vector<std::unique_ptr<Scope>> scopes_;
|
||||
Module* default_module_;
|
||||
std::vector<Scope*> scopes_;
|
||||
std::vector<Scope*> current_scopes_;
|
||||
std::vector<std::pair<Label*, Label*>> break_continue_stack_;
|
||||
TypeOracle type_oracle_;
|
||||
Callable* current_callable_;
|
||||
std::map<const AstNode*, std::set<const Variable*>>
|
||||
@ -184,6 +198,19 @@ class CurrentCallActivator {
|
||||
GlobalContext& context_;
|
||||
};
|
||||
|
||||
class BreakContinueActivator {
|
||||
public:
|
||||
BreakContinueActivator(GlobalContext& context, Label* break_label,
|
||||
Label* continue_label)
|
||||
: context_(context) {
|
||||
context_.break_continue_stack_.push_back({break_label, continue_label});
|
||||
}
|
||||
~BreakContinueActivator() { context_.break_continue_stack_.pop_back(); }
|
||||
|
||||
private:
|
||||
GlobalContext& context_;
|
||||
};
|
||||
|
||||
} // namespace torque
|
||||
} // namespace internal
|
||||
} // namespace v8
|
||||
|
@ -113,7 +113,7 @@ void ImplementationVisitor::Visit(ModuleDeclaration* decl) {
|
||||
Module* saved_module = module_;
|
||||
module_ = module;
|
||||
Scope::Activator activator(module_->scope());
|
||||
for (Declaration* child : decl->declarations) Visit(child);
|
||||
for (auto& child : decl->declarations) Visit(child);
|
||||
module_ = saved_module;
|
||||
|
||||
source << "} // namepsace internal" << std::endl
|
||||
@ -305,7 +305,8 @@ VisitResult ImplementationVisitor::Visit(ConditionalExpression* expr) {
|
||||
GenerateLabelDefinition(true_label);
|
||||
Label* false_label = Label::cast(LookupValue(expr->pos, kFalseLabelName));
|
||||
GenerateLabelDefinition(false_label);
|
||||
Label* done_label = new Label(kDoneLabelName);
|
||||
Label* done_label =
|
||||
TopScope()->DeclarePrivateLabel(expr->pos, kDoneLabelName);
|
||||
GenerateLabelDefinition(done_label, expr);
|
||||
|
||||
VisitResult condition_result = Visit(expr->condition);
|
||||
@ -332,7 +333,7 @@ VisitResult ImplementationVisitor::Visit(ConditionalExpression* expr) {
|
||||
|
||||
VisitResult ImplementationVisitor::Visit(LogicalOrExpression* expr) {
|
||||
{
|
||||
Scope::Activator s(global_context_, &*expr->left);
|
||||
Scope::Activator s(global_context_, expr->left);
|
||||
Label* false_label = Label::cast(LookupValue(expr->pos, kFalseLabelName));
|
||||
GenerateLabelDefinition(false_label);
|
||||
VisitResult left_result = Visit(expr->left);
|
||||
@ -350,7 +351,7 @@ VisitResult ImplementationVisitor::Visit(LogicalOrExpression* expr) {
|
||||
|
||||
VisitResult ImplementationVisitor::Visit(LogicalAndExpression* expr) {
|
||||
{
|
||||
Scope::Activator s(global_context_, &*expr->left);
|
||||
Scope::Activator s(global_context_, expr->left);
|
||||
Label* true_label = Label::cast(LookupValue(expr->pos, kTrueLabelName));
|
||||
GenerateLabelDefinition(true_label);
|
||||
VisitResult left_result = Visit(expr->left);
|
||||
@ -456,7 +457,7 @@ Type ImplementationVisitor::Visit(GotoStatement* stmt) {
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
for (auto e : stmt->arguments) {
|
||||
for (Expression* e : stmt->arguments) {
|
||||
VisitResult result = Visit(e);
|
||||
Variable* var = label->GetParameter(i++);
|
||||
GenerateAssignToVariable(e->pos, var, result);
|
||||
@ -485,7 +486,7 @@ Type ImplementationVisitor::Visit(IfStatement* stmt) {
|
||||
Label* done_label = nullptr;
|
||||
bool live = false;
|
||||
if (has_else) {
|
||||
done_label = new Label("if_done_label");
|
||||
done_label = TopScope()->DeclarePrivateLabel(stmt->pos, "if_done_label");
|
||||
GenerateLabelDefinition(done_label, stmt);
|
||||
} else {
|
||||
done_label = false_label;
|
||||
@ -509,21 +510,21 @@ Type ImplementationVisitor::Visit(WhileStatement* stmt) {
|
||||
Label* body_label = nullptr;
|
||||
Label* exit_label = nullptr;
|
||||
{
|
||||
Scope::Activator s(global_context_, &*stmt->condition);
|
||||
Scope::Activator s(global_context_, stmt->condition);
|
||||
body_label = Label::cast(LookupValue(stmt->pos, kTrueLabelName));
|
||||
GenerateLabelDefinition(body_label);
|
||||
exit_label = Label::cast(LookupValue(stmt->pos, kFalseLabelName));
|
||||
GenerateLabelDefinition(exit_label);
|
||||
}
|
||||
|
||||
Label* header_label = new Label("header");
|
||||
Label* header_label = TopScope()->DeclarePrivateLabel(stmt->pos, "header");
|
||||
GenerateLabelDefinition(header_label, stmt);
|
||||
GenerateLabelGoto(header_label);
|
||||
GenerateLabelBind(header_label);
|
||||
|
||||
Scope::Activator s(global_context_, &*stmt->body);
|
||||
TopScope()->DeclareLabel(stmt->pos, kBreakLabelName, exit_label);
|
||||
TopScope()->DeclareLabel(stmt->pos, kContinueLabelName, header_label);
|
||||
Scope::Activator s(global_context_, stmt->body);
|
||||
BreakContinueActivator activator(global_context_, exit_label, header_label);
|
||||
|
||||
GenerateExpressionBranch(stmt->condition, {body_label, exit_label},
|
||||
{stmt->body}, header_label);
|
||||
|
||||
@ -619,11 +620,12 @@ Type ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
|
||||
: GenerateOperation(stmt->pos, ".length",
|
||||
{{expression_result}, {}});
|
||||
|
||||
Label* body_label = new Label("body");
|
||||
Label* body_label = TopScope()->DeclarePrivateLabel(stmt->pos, "body");
|
||||
GenerateLabelDefinition(body_label);
|
||||
Label* increment_label = new Label("increment");
|
||||
Label* increment_label =
|
||||
TopScope()->DeclarePrivateLabel(stmt->pos, "increment");
|
||||
GenerateLabelDefinition(increment_label);
|
||||
Label* exit_label = new Label("exit");
|
||||
Label* exit_label = TopScope()->DeclarePrivateLabel(stmt->pos, "exit");
|
||||
GenerateLabelDefinition(exit_label);
|
||||
|
||||
Type common_type = GetCommonType(stmt->pos, begin.type(), end.type());
|
||||
@ -634,14 +636,15 @@ Type ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
|
||||
VisitResult index_for_read = {index_var->type(),
|
||||
index_var->GetValueForRead()};
|
||||
|
||||
Label* header_label = new Label("header");
|
||||
Label* header_label = TopScope()->DeclarePrivateLabel(stmt->pos, "header");
|
||||
GenerateLabelDefinition(header_label, stmt);
|
||||
|
||||
GenerateLabelGoto(header_label);
|
||||
|
||||
GenerateLabelBind(header_label);
|
||||
TopScope()->DeclareLabel(stmt->pos, kBreakLabelName, exit_label);
|
||||
TopScope()->DeclareLabel(stmt->pos, kContinueLabelName, increment_label);
|
||||
|
||||
BreakContinueActivator activator(global_context_, exit_label,
|
||||
increment_label);
|
||||
|
||||
VisitResult result =
|
||||
GenerateOperation(stmt->pos, "<", {{index_for_read, end}, {}});
|
||||
@ -672,7 +675,7 @@ Type ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
|
||||
|
||||
Type ImplementationVisitor::Visit(TryCatchStatement* stmt) {
|
||||
ScopedIndent indent(this);
|
||||
Label* try_done = new Label("try_done");
|
||||
Label* try_done = TopScope()->DeclarePrivateLabel(stmt->pos, "try_done");
|
||||
GenerateLabelDefinition(try_done);
|
||||
Type try_result = GetTypeOracle().GetNeverType();
|
||||
std::vector<Label*> labels;
|
||||
@ -690,14 +693,15 @@ Type ImplementationVisitor::Visit(TryCatchStatement* stmt) {
|
||||
size_t i = 0;
|
||||
for (auto label : labels) {
|
||||
Scope::Activator s(global_context_, stmt->label_blocks[i]->body);
|
||||
for (auto v : label->GetParameters()) {
|
||||
for (auto& v : label->GetParameters()) {
|
||||
GenerateVariableDeclaration(stmt, v->name(), v->type());
|
||||
v->Define();
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
Label* try_begin_label = new Label("try_begin");
|
||||
Label* try_begin_label =
|
||||
TopScope()->DeclarePrivateLabel(stmt->pos, "try_begin");
|
||||
GenerateLabelDefinition(try_begin_label);
|
||||
GenerateLabelGoto(try_begin_label);
|
||||
|
||||
@ -739,7 +743,7 @@ Type ImplementationVisitor::Visit(TryCatchStatement* stmt) {
|
||||
}
|
||||
|
||||
Type ImplementationVisitor::Visit(BreakStatement* stmt) {
|
||||
Label* break_label = Label::cast(LookupValue(stmt->pos, kBreakLabelName));
|
||||
Label* break_label = global_context_.GetCurrentBreak();
|
||||
if (break_label == nullptr) {
|
||||
ReportError("break used outside of loop at " + PositionAsString(stmt->pos));
|
||||
}
|
||||
@ -748,8 +752,7 @@ Type ImplementationVisitor::Visit(BreakStatement* stmt) {
|
||||
}
|
||||
|
||||
Type ImplementationVisitor::Visit(ContinueStatement* stmt) {
|
||||
Label* continue_label =
|
||||
Label::cast(LookupValue(stmt->pos, kContinueLabelName));
|
||||
Label* continue_label = global_context_.GetCurrentContinue();
|
||||
if (continue_label == nullptr) {
|
||||
ReportError("continue used outside of loop at " +
|
||||
PositionAsString(stmt->pos));
|
||||
@ -773,16 +776,17 @@ Type ImplementationVisitor::Visit(ForLoopStatement* stmt) {
|
||||
GenerateLabelDefinition(exit_label);
|
||||
}
|
||||
|
||||
Label* header_label = new Label("header");
|
||||
Label* header_label = TopScope()->DeclarePrivateLabel(stmt->pos, "header");
|
||||
GenerateLabelDefinition(header_label, stmt);
|
||||
GenerateLabelGoto(header_label);
|
||||
GenerateLabelBind(header_label);
|
||||
|
||||
Label* assignment_label = new Label("assignment");
|
||||
Label* assignment_label =
|
||||
TopScope()->DeclarePrivateLabel(stmt->pos, "assignment");
|
||||
GenerateLabelDefinition(assignment_label);
|
||||
|
||||
TopScope()->DeclareLabel(stmt->pos, kBreakLabelName, exit_label);
|
||||
TopScope()->DeclareLabel(stmt->pos, kContinueLabelName, assignment_label);
|
||||
BreakContinueActivator activator(global_context_, exit_label,
|
||||
assignment_label);
|
||||
|
||||
std::vector<Label*> labels = {body_label, exit_label};
|
||||
if (GenerateExpressionBranch(stmt->test, labels, {stmt->body},
|
||||
@ -878,7 +882,8 @@ void ImplementationVisitor::GenerateMacroFunctionDeclaration(
|
||||
first = false;
|
||||
}
|
||||
|
||||
for (Label* label : macro->signature().labels) {
|
||||
for (const LabelDeclaration& label_info : macro->signature().labels) {
|
||||
Label* label = GetLabel(pos, label_info.name);
|
||||
if (!first) {
|
||||
o << ", ";
|
||||
}
|
||||
@ -1159,7 +1164,7 @@ VisitResult ImplementationVisitor::GenerateCall(
|
||||
source_out() << variables[i];
|
||||
}
|
||||
|
||||
size_t label_count = callable->signature().label_defintions.size();
|
||||
size_t label_count = callable->signature().labels.size();
|
||||
if (label_count != arguments.labels.size()) {
|
||||
std::stringstream s;
|
||||
s << "unexpected number of otherwise labels for " << callable->name()
|
||||
@ -1174,7 +1179,7 @@ VisitResult ImplementationVisitor::GenerateCall(
|
||||
}
|
||||
Label* label = arguments.labels[i];
|
||||
size_t callee_label_parameters =
|
||||
callable->signature().label_defintions[i].types.size();
|
||||
callable->signature().labels[i].types.size();
|
||||
if (label->GetParameterCount() != callee_label_parameters) {
|
||||
std::stringstream s;
|
||||
s << "label " << label->GetSourceName()
|
||||
@ -1186,7 +1191,7 @@ VisitResult ImplementationVisitor::GenerateCall(
|
||||
}
|
||||
source_out() << label->GetValueForRead();
|
||||
size_t j = 0;
|
||||
for (auto t : callable->signature().label_defintions[i].types) {
|
||||
for (auto t : callable->signature().labels[i].types) {
|
||||
source_out() << ", ";
|
||||
Variable* variable = label->GetParameter(j);
|
||||
if (!variable->type().Is(t)) {
|
||||
|
@ -231,7 +231,6 @@ class ImplementationVisitor : public FileVisitor {
|
||||
|
||||
size_t indent_;
|
||||
int32_t next_temp_;
|
||||
std::ostream* current_source_out_;
|
||||
};
|
||||
|
||||
} // namespace torque
|
||||
|
@ -16,13 +16,16 @@ namespace torque {
|
||||
|
||||
Scope::Scope(GlobalContext& global_context)
|
||||
: global_context_(global_context),
|
||||
scope_number_(global_context.GetNextScopeNumber()) {}
|
||||
scope_number_(global_context.GetNextScopeNumber()),
|
||||
private_label_number_(0) {
|
||||
global_context.RegisterScope(this);
|
||||
}
|
||||
|
||||
Macro* Scope::DeclareMacro(SourcePosition pos, const std::string& name,
|
||||
Scope* scope, const Signature& signature) {
|
||||
auto i = lookup_.find(name);
|
||||
if (i == lookup_.end()) {
|
||||
lookup_[name] = new MacroList();
|
||||
lookup_[name] = std::unique_ptr<MacroList>(new MacroList());
|
||||
i = lookup_.find(name);
|
||||
} else if (i->second->kind() != Declarable::kMacroList) {
|
||||
std::stringstream s;
|
||||
@ -30,8 +33,8 @@ Macro* Scope::DeclareMacro(SourcePosition pos, const std::string& name,
|
||||
<< global_context_.PositionAsString(pos);
|
||||
ReportError(s.str());
|
||||
}
|
||||
MacroList* macro_list = MacroList::cast(i->second);
|
||||
for (auto macro : macro_list->list()) {
|
||||
MacroList* macro_list = MacroList::cast(i->second.get());
|
||||
for (auto& macro : macro_list->list()) {
|
||||
if (signature.parameter_types.types ==
|
||||
macro->signature().parameter_types.types &&
|
||||
signature.parameter_types.var_args ==
|
||||
@ -43,8 +46,8 @@ Macro* Scope::DeclareMacro(SourcePosition pos, const std::string& name,
|
||||
ReportError(s.str());
|
||||
}
|
||||
}
|
||||
Macro* result = new Macro(name, scope, signature);
|
||||
macro_list->AddMacro(result);
|
||||
Macro* result = macro_list->AddMacro(
|
||||
std::unique_ptr<Macro>(new Macro(name, scope, signature)));
|
||||
if (global_context_.verbose()) {
|
||||
std::cout << "declared " << *result << "\n";
|
||||
}
|
||||
@ -56,7 +59,7 @@ Builtin* Scope::DeclareBuiltin(SourcePosition pos, const std::string& name,
|
||||
const Signature& signature) {
|
||||
CheckAlreadyDeclared(pos, name, "builtin");
|
||||
Builtin* result = new Builtin(name, kind, scope, signature);
|
||||
lookup_[name] = result;
|
||||
lookup_[name] = std::unique_ptr<Builtin>(result);
|
||||
if (global_context_.verbose()) {
|
||||
std::cout << "declared " << *result << "\n";
|
||||
}
|
||||
@ -67,7 +70,7 @@ Runtime* Scope::DeclareRuntime(SourcePosition pos, const std::string& name,
|
||||
Scope* scope, const Signature& signature) {
|
||||
CheckAlreadyDeclared(pos, name, "runtime");
|
||||
Runtime* result = new Runtime(name, scope, signature);
|
||||
lookup_[name] = result;
|
||||
lookup_[name] = std::unique_ptr<Runtime>(result);
|
||||
if (global_context_.verbose()) {
|
||||
std::cout << "declared " << *result << "\n";
|
||||
}
|
||||
@ -80,7 +83,7 @@ Variable* Scope::DeclareVariable(SourcePosition pos, const std::string& var,
|
||||
std::to_string(scope_number_));
|
||||
CheckAlreadyDeclared(pos, var, "variable");
|
||||
Variable* result = new Variable(var, name, type);
|
||||
lookup_[var] = result;
|
||||
lookup_[var] = std::unique_ptr<Variable>(result);
|
||||
if (global_context_.verbose()) {
|
||||
std::cout << "declared " << var << " (type " << type << ")\n";
|
||||
}
|
||||
@ -91,29 +94,36 @@ Parameter* Scope::DeclareParameter(SourcePosition pos, const std::string& name,
|
||||
const std::string& var_name, Type type) {
|
||||
CheckAlreadyDeclared(pos, name, "parameter");
|
||||
Parameter* result = new Parameter(name, type, var_name);
|
||||
lookup_[name] = result;
|
||||
lookup_[name] = std::unique_ptr<Parameter>(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Label* Scope::DeclareLabel(SourcePosition pos, const std::string& name,
|
||||
Label* already_defined) {
|
||||
Label* Scope::DeclareLabel(SourcePosition pos, const std::string& name) {
|
||||
CheckAlreadyDeclared(pos, name, "label");
|
||||
Label* result =
|
||||
already_defined == nullptr ? new Label(name) : already_defined;
|
||||
lookup_[name] = result;
|
||||
Label* result = new Label(name);
|
||||
lookup_[name] = std::unique_ptr<Label>(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Label* Scope::DeclarePrivateLabel(SourcePosition pos,
|
||||
const std::string& raw_name) {
|
||||
std::string name = raw_name + "_" + std::to_string(private_label_number_++);
|
||||
CheckAlreadyDeclared(pos, name, "label");
|
||||
Label* result = new Label(name);
|
||||
lookup_[name] = std::unique_ptr<Label>(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Scope::DeclareConstant(SourcePosition pos, const std::string& name,
|
||||
Type type, const std::string& value) {
|
||||
CheckAlreadyDeclared(pos, name, "constant, parameter or arguments");
|
||||
lookup_[name] = new Constant(name, type, value);
|
||||
lookup_[name] = std::unique_ptr<Constant>(new Constant(name, type, value));
|
||||
}
|
||||
|
||||
void Scope::AddLiveVariables(std::set<const Variable*>& set) {
|
||||
for (auto current : lookup_) {
|
||||
for (auto& current : lookup_) {
|
||||
if (current.second->IsVariable()) {
|
||||
set.insert(Variable::cast(current.second));
|
||||
set.insert(Variable::cast(current.second.get()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -132,8 +142,8 @@ void Scope::CheckAlreadyDeclared(SourcePosition pos, const std::string& name,
|
||||
|
||||
void Scope::Print() {
|
||||
std::cout << "scope #" << std::to_string(scope_number_) << "\n";
|
||||
for (auto i : lookup_) {
|
||||
std::cout << i.first << ": " << i.second << "\n";
|
||||
for (auto& i : lookup_) {
|
||||
std::cout << i.first << ": " << i.second.get() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,8 +45,9 @@ class Scope {
|
||||
Parameter* DeclareParameter(SourcePosition pos, const std::string& name,
|
||||
const std::string& mangled_name, Type type);
|
||||
|
||||
Label* DeclareLabel(SourcePosition pos, const std::string& name,
|
||||
Label* already_defined = nullptr);
|
||||
Label* DeclareLabel(SourcePosition pos, const std::string& name);
|
||||
|
||||
Label* DeclarePrivateLabel(SourcePosition pos, const std::string& name);
|
||||
|
||||
void DeclareConstant(SourcePosition pos, const std::string& name, Type type,
|
||||
const std::string& value);
|
||||
@ -56,12 +57,12 @@ class Scope {
|
||||
if (i == lookup_.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return i->second;
|
||||
return i->second.get();
|
||||
}
|
||||
|
||||
void Stream(std::ostream& stream) const {
|
||||
stream << "scope " << std::to_string(scope_number_) << " {";
|
||||
for (auto c : lookup_) {
|
||||
for (auto& c : lookup_) {
|
||||
stream << c.first << ",";
|
||||
}
|
||||
stream << "}";
|
||||
@ -81,7 +82,8 @@ class Scope {
|
||||
|
||||
GlobalContext& global_context_;
|
||||
int scope_number_;
|
||||
std::map<std::string, Declarable*> lookup_;
|
||||
int private_label_number_;
|
||||
std::map<std::string, std::unique_ptr<Declarable>> lookup_;
|
||||
};
|
||||
|
||||
class Scope::Activator {
|
||||
|
@ -61,12 +61,16 @@ int WrappedMain(int argc, const char** argv) {
|
||||
// file, parse it and
|
||||
// remember the syntax tree
|
||||
context.name = argv[i];
|
||||
context.stream = new antlr4::ANTLRFileStream(context.name.c_str());
|
||||
context.lexer = new TorqueLexer(context.stream);
|
||||
context.tokens = new antlr4::CommonTokenStream(context.lexer);
|
||||
context.stream = std::unique_ptr<antlr4::ANTLRFileStream>(
|
||||
new antlr4::ANTLRFileStream(context.name.c_str()));
|
||||
context.lexer =
|
||||
std::unique_ptr<TorqueLexer>(new TorqueLexer(context.stream.get()));
|
||||
context.tokens = std::unique_ptr<antlr4::CommonTokenStream>(
|
||||
new antlr4::CommonTokenStream(context.lexer.get()));
|
||||
context.tokens->fill();
|
||||
lexer_errors += context.lexer->getNumberOfSyntaxErrors();
|
||||
context.parser = new TorqueParser(context.tokens);
|
||||
context.parser =
|
||||
std::unique_ptr<TorqueParser>(new TorqueParser(context.tokens.get()));
|
||||
context.parser->setErrorHandler(error_strategy);
|
||||
context.file = context.parser->file();
|
||||
ast_generator.visitSourceFile(&context);
|
||||
@ -93,7 +97,7 @@ int WrappedMain(int argc, const char** argv) {
|
||||
visitor.Visit(global_context.ast());
|
||||
|
||||
for (auto& module : global_context.GetModules()) {
|
||||
visitor.GenerateImplementation(output_directory, module.second);
|
||||
visitor.GenerateImplementation(output_directory, module.second.get());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@ -17,20 +17,20 @@ class TypeOracle {
|
||||
void RegisterTypeImpl(const std::string& name, const std::string& generated,
|
||||
const std::string* parent = nullptr) {
|
||||
TypeImpl* parent_class = nullptr;
|
||||
if (type_imples_.find(name) != type_imples_.end()) {
|
||||
if (type_impls_.find(name) != type_impls_.end()) {
|
||||
ReportError(std::string("cannot redefine type class ") + name);
|
||||
}
|
||||
if (parent != nullptr) {
|
||||
auto i = type_imples_.find(*parent);
|
||||
if (i == type_imples_.end()) {
|
||||
auto i = type_impls_.find(*parent);
|
||||
if (i == type_impls_.end()) {
|
||||
std::stringstream s;
|
||||
s << "cannot find parent type class " << *parent << " for " << name;
|
||||
ReportError(s.str());
|
||||
}
|
||||
parent_class = i->second;
|
||||
parent_class = i->second.get();
|
||||
}
|
||||
TypeImpl* new_class = new TypeImpl(parent_class, name, generated);
|
||||
type_imples_[name] = new_class;
|
||||
type_impls_[name] = std::unique_ptr<TypeImpl>(new_class);
|
||||
}
|
||||
|
||||
void RegisterImplicitConversion(Type to, Type from) {
|
||||
@ -38,13 +38,13 @@ class TypeOracle {
|
||||
}
|
||||
|
||||
Type GetType(const std::string& type_name) {
|
||||
auto i = type_imples_.find(type_name);
|
||||
if (i == type_imples_.end()) {
|
||||
auto i = type_impls_.find(type_name);
|
||||
if (i == type_impls_.end()) {
|
||||
std::stringstream s;
|
||||
s << "no type class found for type " << type_name;
|
||||
ReportError(s.str());
|
||||
}
|
||||
return Type(i->second);
|
||||
return Type(i->second.get());
|
||||
}
|
||||
|
||||
Type GetArgumentsType() { return GetType(ARGUMENTS_TYPE_STRING); }
|
||||
@ -99,7 +99,7 @@ class TypeOracle {
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, TypeImpl*> type_imples_;
|
||||
std::map<std::string, std::unique_ptr<TypeImpl>> type_impls_;
|
||||
std::vector<std::pair<Type, Type>> implicit_conversions_;
|
||||
};
|
||||
|
||||
|
@ -26,9 +26,6 @@ std::ostream& operator<<(std::ostream& os, const Signature& sig) {
|
||||
if (!sig.return_type.IsVoid()) {
|
||||
os << ": " << sig.return_type;
|
||||
}
|
||||
for (size_t i = 0; i < sig.labels.size(); ++i) {
|
||||
os << (i == 0 ? " labels " : ", ") << sig.labels[i];
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
|
@ -160,8 +160,7 @@ struct Signature {
|
||||
NameVector parameter_names;
|
||||
ParameterTypes parameter_types;
|
||||
Type return_type;
|
||||
LabelDeclarationVector label_defintions;
|
||||
std::vector<Label*> labels;
|
||||
LabelDeclarationVector labels;
|
||||
};
|
||||
|
||||
struct Arguments {
|
||||
|
Loading…
Reference in New Issue
Block a user