Commit 88ffe246 authored by Tobias Tebbi's avatar Tobias Tebbi Committed by Commit Bot

[torque] handle souce position information in a global context

Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I20e30f0c19c887b1e093b02e39c7bd3d53d15182
Reviewed-on: https://chromium-review.googlesource.com/1054073
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53221}
parent 0b6ad251
......@@ -3098,6 +3098,7 @@ if (current_toolchain == v8_snapshot_toolchain) {
"src/torque/ast-generator.cc",
"src/torque/ast-generator.h",
"src/torque/ast.h",
"src/torque/contextual.h",
"src/torque/declarable.cc",
"src/torque/declarable.h",
"src/torque/declaration-visitor.cc",
......
......@@ -747,7 +747,7 @@ antlrcpp::Any AstGenerator::visitDiagnosticStatement(
void AstGenerator::visitSourceFile(SourceFileContext* context) {
source_file_context_ = context;
current_source_file_ = ast_.AddSource(context->name);
current_source_file_ = SourceFileMap::Get().AddSource(context->name);
for (auto* declaration : context->file->children) {
ast_.declarations().push_back(declaration->accept(this).as<Declaration*>());
}
......
......@@ -11,6 +11,7 @@
#include <vector>
#include "src/base/optional.h"
#include "src/torque/contextual.h"
namespace v8 {
namespace internal {
......@@ -24,6 +25,8 @@ struct SourcePosition {
int column;
};
DECLARE_CONTEXTUAL_VARIABLE(CurrentSourcePosition, SourcePosition)
#define AST_EXPRESSION_NODE_KIND_LIST(V) \
V(CallExpression) \
V(LogicalOrExpression) \
......@@ -185,7 +188,7 @@ struct ExplicitModuleDeclaration : ModuleDeclaration {
std::string name;
};
class SourceFileMap {
class SourceFileMap : public ContextualClass<SourceFileMap> {
public:
SourceFileMap() {}
const std::string& GetSource(SourceId id) const {
......@@ -197,20 +200,22 @@ class SourceFileMap {
std::to_string(pos.column);
}
private:
friend class Ast;
SourceId AddSource(std::string path) {
sources_.push_back(std::move(path));
return static_cast<SourceId>(sources_.size() - 1);
}
private:
std::vector<std::string> sources_;
};
inline std::string PositionAsString(SourcePosition pos) {
return SourceFileMap::Get().PositionAsString(pos);
}
class Ast {
public:
Ast()
: default_module_{SourcePosition(), {}},
source_file_map_(new SourceFileMap()) {}
Ast() : default_module_{SourcePosition(), {}} {}
std::vector<Declaration*>& declarations() {
return default_module_.declarations;
......@@ -221,15 +226,10 @@ class Ast {
void AddNode(std::unique_ptr<AstNode> node) {
nodes_.emplace_back(std::move(node));
}
SourceId AddSource(std::string path) {
return source_file_map_->AddSource(path);
}
DefaultModuleDeclaration* default_module() { return &default_module_; }
SourceFileMap* source_file_map() { return &*source_file_map_; }
private:
DefaultModuleDeclaration default_module_;
std::unique_ptr<SourceFileMap> source_file_map_;
std::vector<std::unique_ptr<AstNode>> nodes_;
};
......
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_TORQUE_CONTEXTUAL_H_
#define V8_TORQUE_CONTEXTUAL_H_
#include <type_traits>
namespace v8 {
namespace internal {
namespace torque {
// Contextual variables store a value in one or more stack-allocated scopes and
// allow global access to the most recent active scope on the current
// call-stack.
template <class Derived, class VarType>
class ContextualVariable {
public:
// A {Scope} contains a new object of type {T} and gives
// ContextualVariable::Get() access to it. Upon destruction, the contextual
// variable is restored to the state before the {Scope} was created. Scopes
// have to follow a stack discipline: A {Scope} has to be destructed before
// any older scope is destructed.
class Scope {
public:
explicit Scope(VarType x = VarType())
: current_(std::move(x)), previous_(top_) {
top_ = &current_;
}
~Scope() {
// Ensure stack discipline.
DCHECK_EQ(&current_, top_);
top_ = previous_;
}
private:
VarType current_;
VarType* previous_;
static_assert(std::is_base_of<ContextualVariable, Derived>::value,
"Curiously Recurring Template Pattern");
};
// Access the most recent active {Scope}. There has to be an active {Scope}
// for this contextual variable.
static VarType& Get() {
DCHECK_NOT_NULL(top_);
return *top_;
}
private:
static thread_local VarType* top_;
};
template <class Derived, class VarType>
thread_local VarType* ContextualVariable<Derived, VarType>::top_ = nullptr;
// Usage: DECLARE_CONTEXTUAL_VARIABLE(VarName, VarType)
#define DECLARE_CONTEXTUAL_VARIABLE(VarName, ...) \
struct VarName \
: v8::internal::torque::ContextualVariable<VarName, __VA_ARGS__> {};
// By inheriting from {ContextualClass} a class can become a contextual variable
// of itself.
template <class T>
using ContextualClass = ContextualVariable<T, T>;
} // namespace torque
} // namespace internal
} // namespace v8
#endif // V8_TORQUE_CONTEXTUAL_H_
......@@ -9,6 +9,7 @@ namespace internal {
namespace torque {
void DeclarationVisitor::Visit(Expression* expr) {
CurrentSourcePosition::Scope scope(expr->pos);
switch (expr->kind) {
#define ENUM_ITEM(name) \
case AstNode::Kind::k##name: \
......@@ -21,6 +22,7 @@ void DeclarationVisitor::Visit(Expression* expr) {
}
void DeclarationVisitor::Visit(Statement* stmt) {
CurrentSourcePosition::Scope scope(stmt->pos);
switch (stmt->kind) {
#define ENUM_ITEM(name) \
case AstNode::Kind::k##name: \
......@@ -33,6 +35,7 @@ void DeclarationVisitor::Visit(Statement* stmt) {
}
void DeclarationVisitor::Visit(Declaration* decl) {
CurrentSourcePosition::Scope scope(decl->pos);
switch (decl->kind) {
#define ENUM_ITEM(name) \
case AstNode::Kind::k##name: \
......@@ -67,38 +70,35 @@ Builtin* DeclarationVisitor::BuiltinDeclarationCommon(
if (signature.types().size() == 0 ||
!(signature.types()[0] ==
declarations()->LookupGlobalType(decl->pos, CONTEXT_TYPE_STRING))) {
declarations()->LookupGlobalType(CONTEXT_TYPE_STRING))) {
std::stringstream stream;
stream << "first parameter to builtin " << decl->name
<< " is not a context but should be at "
<< PositionAsString(decl->pos);
<< " is not a context but should be";
ReportError(stream.str());
}
if (varargs && !javascript) {
std::stringstream stream;
stream << "builtin " << decl->name
<< " with rest parameters must be a JavaScript builtin at "
<< PositionAsString(decl->pos);
<< " with rest parameters must be a JavaScript builtin";
ReportError(stream.str());
}
if (javascript) {
if (signature.types().size() < 2 ||
!(signature.types()[1] ==
declarations()->LookupGlobalType(decl->pos, OBJECT_TYPE_STRING))) {
declarations()->LookupGlobalType(OBJECT_TYPE_STRING))) {
std::stringstream stream;
stream << "second parameter to javascript builtin " << decl->name
<< " is " << signature.types()[1] << " but should be Object at "
<< PositionAsString(decl->pos);
<< " is " << signature.types()[1] << " but should be Object";
ReportError(stream.str());
}
}
std::string generated_name = GetGeneratedCallableName(
decl->name, declarations()->GetCurrentSpecializationTypeNamesVector());
return declarations()->DeclareBuiltin(decl->pos, generated_name, kind,
external, signature);
return declarations()->DeclareBuiltin(generated_name, kind, external,
signature);
}
void DeclarationVisitor::Visit(ExternalRuntimeDeclaration* decl,
......@@ -113,12 +113,11 @@ void DeclarationVisitor::Visit(ExternalRuntimeDeclaration* decl,
declarations()->LookupGlobalType(CONTEXT_TYPE_STRING))) {
std::stringstream stream;
stream << "first parameter to runtime " << decl->name
<< " is not a context but should be at "
<< PositionAsString(decl->pos);
<< " is not a context but should be";
ReportError(stream.str());
}
declarations()->DeclareRuntimeFunction(decl->pos, decl->name, signature);
declarations()->DeclareRuntimeFunction(decl->name, signature);
}
void DeclarationVisitor::Visit(ExternalMacroDeclaration* decl,
......@@ -130,7 +129,7 @@ void DeclarationVisitor::Visit(ExternalMacroDeclaration* decl,
std::string generated_name = GetGeneratedCallableName(
decl->name, declarations()->GetCurrentSpecializationTypeNamesVector());
declarations()->DeclareMacro(decl->pos, generated_name, signature);
declarations()->DeclareMacro(generated_name, signature);
if (decl->op) {
OperationHandler handler(
{decl->name, signature.parameter_types, signature.return_type});
......@@ -144,18 +143,13 @@ void DeclarationVisitor::Visit(ExternalMacroDeclaration* decl,
if (decl->implicit) {
if (!decl->op || *decl->op != "convert<>") {
std::stringstream s;
s << "implicit can only be used with cast<> operator at "
<< PositionAsString(decl->pos) << "\n";
ReportError(s.str());
ReportError("implicit can only be used with cast<> operator");
}
const TypeVector& parameter_types = signature.parameter_types.types;
if (parameter_types.size() != 1 || signature.parameter_types.var_args) {
std::stringstream s;
s << "implicit cast operators doesn't only have a single parameter at "
<< PositionAsString(decl->pos) << "\n";
ReportError(s.str());
ReportError(
"implicit cast operators doesn't only have a single parameter");
}
GetTypeOracle().RegisterImplicitConversion(signature.return_type,
parameter_types[0]);
......@@ -166,10 +160,10 @@ void DeclarationVisitor::Visit(TorqueBuiltinDeclaration* decl,
const Signature& signature, Statement* body) {
Builtin* builtin = BuiltinDeclarationCommon(decl, false, signature);
CurrentCallableActivator activator(global_context_, builtin, decl);
DeclareSignature(decl->pos, builtin->signature());
DeclareSignature(builtin->signature());
if (builtin->signature().parameter_types.var_args) {
declarations()->DeclareConstant(
decl->pos, decl->signature->parameters.arguments_variable,
decl->signature->parameters.arguments_variable,
GetTypeOracle().GetArgumentsType(), "arguments");
}
torque_builtins_.push_back(builtin);
......@@ -180,14 +174,13 @@ void DeclarationVisitor::Visit(TorqueMacroDeclaration* decl,
const Signature& signature, Statement* body) {
std::string generated_name = GetGeneratedCallableName(
decl->name, declarations()->GetCurrentSpecializationTypeNamesVector());
Macro* macro =
declarations()->DeclareMacro(decl->pos, generated_name, signature);
Macro* macro = declarations()->DeclareMacro(generated_name, signature);
CurrentCallableActivator activator(global_context_, macro, decl);
DeclareSignature(decl->pos, signature);
DeclareSignature(signature);
if (!signature.return_type->IsVoidOrNever()) {
declarations()->DeclareVariable(decl->pos, kReturnValueVariable,
declarations()->DeclareVariable(kReturnValueVariable,
signature.return_type);
}
......@@ -206,14 +199,12 @@ void DeclarationVisitor::Visit(StandardDeclaration* decl) {
}
void DeclarationVisitor::Visit(GenericDeclaration* decl) {
declarations()->DeclareGeneric(decl->pos, decl->callable->name,
CurrentModule(), decl);
declarations()->DeclareGeneric(decl->callable->name, CurrentModule(), decl);
}
void DeclarationVisitor::Visit(SpecializationDeclaration* decl) {
Generic* generic = declarations()->LookupGeneric(decl->pos, decl->name);
SpecializationKey key = {generic,
GetTypeVector(decl->pos, decl->generic_parameters)};
Generic* generic = declarations()->LookupGeneric(decl->name);
SpecializationKey key = {generic, GetTypeVector(decl->generic_parameters)};
CallableNode* callable = generic->declaration()->callable;
// Ensure that the specialized signature matches the generic signature.
if (callable->signature->parameters.names.size() !=
......@@ -237,8 +228,8 @@ void DeclarationVisitor::Visit(SpecializationDeclaration* decl) {
void DeclarationVisitor::Visit(ReturnStatement* stmt) {
const Callable* callable = global_context_.GetCurrentCallable();
if (callable->IsMacro() && callable->HasReturnValue()) {
MarkVariableModified(Variable::cast(
declarations()->LookupValue(stmt->pos, kReturnValueVariable)));
MarkVariableModified(
Variable::cast(declarations()->LookupValue(kReturnValueVariable)));
}
if (stmt->value) {
Visit(*stmt->value);
......@@ -268,22 +259,20 @@ void DeclarationVisitor::Visit(TryCatchStatement* stmt) {
// Declare catch labels
for (LabelBlock* block : stmt->label_blocks) {
Label* shared_label =
declarations()->DeclareLabel(stmt->pos, block->label);
CurrentSourcePosition::Scope scope(block->pos);
Label* shared_label = declarations()->DeclareLabel(block->label);
{
Declarations::NodeScopeActivator scope(declarations(), block->body);
if (block->parameters.has_varargs) {
std::stringstream stream;
stream << "cannot use ... for label parameters at "
<< PositionAsString(stmt->pos);
stream << "cannot use ... for label parameters";
ReportError(stream.str());
}
size_t i = 0;
for (auto p : block->parameters.names) {
shared_label->AddVariable(declarations()->DeclareVariable(
stmt->pos, p,
declarations()->GetType(stmt->pos, block->parameters.types[i])));
p, declarations()->GetType(block->parameters.types[i])));
++i;
}
}
......@@ -309,11 +298,10 @@ void DeclarationVisitor::Visit(TryCatchStatement* stmt) {
void DeclarationVisitor::Visit(CallExpression* expr) {
if (expr->generic_arguments.size() != 0) {
Generic* generic =
declarations()->LookupGeneric(expr->pos, expr->callee.name);
Generic* generic = declarations()->LookupGeneric(expr->callee.name);
TypeVector specialization_types;
for (auto t : expr->generic_arguments) {
specialization_types.push_back(declarations()->GetType(expr->pos, t));
specialization_types.push_back(declarations()->GetType(t));
}
CallableNode* callable = generic->declaration()->callable;
QueueGenericSpecialization({generic, specialization_types}, callable,
......@@ -330,7 +318,9 @@ void DeclarationVisitor::Specialize(const SpecializationKey& key,
Statement* body) {
Generic* generic = key.first;
SourcePosition pos = generic->declaration()->pos;
// TODO(tebbi): The error should point to the source position where the
// instantiation was requested.
CurrentSourcePosition::Scope scope(generic->declaration()->pos);
size_t generic_parameter_count =
generic->declaration()->generic_parameters.size();
if (generic_parameter_count != key.second.size()) {
......@@ -339,8 +329,7 @@ void DeclarationVisitor::Specialize(const SpecializationKey& key,
<< std::to_string(key.second.size())
<< ") to intantiation of generic " << callable->name
<< " doesnt match the generic's declaration ("
<< std::to_string(generic_parameter_count) << ") at "
<< PositionAsString(pos);
<< std::to_string(generic_parameter_count) << ")";
ReportError(stream.str());
}
......@@ -352,7 +341,7 @@ void DeclarationVisitor::Specialize(const SpecializationKey& key,
for (auto type : key.second) {
std::string generic_type_name =
generic->declaration()->generic_parameters[i++];
declarations()->DeclareTypeAlias(pos, generic_type_name, type);
declarations()->DeclareTypeAlias(generic_type_name, type);
}
}
......
......@@ -74,14 +74,13 @@ class DeclarationVisitor : public FileVisitor {
std::string generates =
decl->generates ? *decl->generates : std::string("");
declarations()->DeclareAbstractType(decl->pos, decl->name, generates,
extends_ptr);
declarations()->DeclareAbstractType(decl->name, generates, extends_ptr);
if (decl->constexpr_generates) {
std::string constexpr_name =
std::string(CONSTEXPR_TYPE_PREFIX) + decl->name;
declarations()->DeclareAbstractType(
decl->pos, constexpr_name, *decl->constexpr_generates, &(decl->name));
constexpr_name, *decl->constexpr_generates, &(decl->name));
}
}
......@@ -118,14 +117,11 @@ class DeclarationVisitor : public FileVisitor {
void Visit(VarDeclarationStatement* stmt) {
std::string variable_name = stmt->name;
const Type* type = declarations()->GetType(stmt->pos, stmt->type);
const Type* type = declarations()->GetType(stmt->type);
if (type->IsConstexpr()) {
std::stringstream stream;
stream << "cannot declare variable with constexpr type at "
<< PositionAsString(stmt->pos);
ReportError(stream.str());
ReportError("cannot declare variable with constexpr type");
}
declarations()->DeclareVariable(stmt->pos, variable_name, type);
declarations()->DeclareVariable(variable_name, type);
if (global_context_.verbose()) {
std::cout << "declared variable " << variable_name << " with type "
<< type << "\n";
......@@ -134,21 +130,20 @@ class DeclarationVisitor : public FileVisitor {
Visit(*stmt->initializer);
if (global_context_.verbose()) {
std::cout << "variable has initialization expression at "
<< PositionAsString(stmt->pos) << "\n";
<< CurrentPositionAsString() << "\n";
}
}
}
void Visit(ConstDeclaration* decl) {
declarations()->DeclareConstant(
decl->pos, decl->name, declarations()->GetType(decl->pos, decl->type),
decl->literal);
decl->name, declarations()->GetType(decl->type), decl->literal);
}
void Visit(LogicalOrExpression* expr) {
{
Declarations::NodeScopeActivator scope(declarations(), expr->left);
declarations()->DeclareLabel(expr->pos, kFalseLabelName);
declarations()->DeclareLabel(kFalseLabelName);
Visit(expr->left);
}
Visit(expr->right);
......@@ -157,7 +152,7 @@ class DeclarationVisitor : public FileVisitor {
void Visit(LogicalAndExpression* expr) {
{
Declarations::NodeScopeActivator scope(declarations(), expr->left);
declarations()->DeclareLabel(expr->pos, kTrueLabelName);
declarations()->DeclareLabel(kTrueLabelName);
Visit(expr->left);
}
Visit(expr->right);
......@@ -171,8 +166,8 @@ class DeclarationVisitor : public FileVisitor {
// visiting the conditional expression, those label-based
// macro conditionals will be able to find them through normal
// label lookups.
declarations()->DeclareLabel(node->pos, kTrueLabelName);
declarations()->DeclareLabel(node->pos, kFalseLabelName);
declarations()->DeclareLabel(kTrueLabelName);
declarations()->DeclareLabel(kFalseLabelName);
Visit(node);
}
......@@ -314,7 +309,7 @@ class DeclarationVisitor : public FileVisitor {
void MarkLocationModified(Expression* location) {
if (IdentifierExpression* id = IdentifierExpression::cast(location)) {
const Value* value = declarations()->LookupValue(id->pos, id->name);
const Value* value = declarations()->LookupValue(id->name);
if (value->IsVariable()) {
const Variable* variable = Variable::cast(value);
bool was_live = MarkVariableModified(variable);
......@@ -340,21 +335,21 @@ class DeclarationVisitor : public FileVisitor {
return was_live_in_preceeding_split;
}
void DeclareSignature(SourcePosition pos, const Signature& signature) {
void DeclareSignature(const Signature& signature) {
auto name_iterator = signature.parameter_names.begin();
for (auto t : signature.types()) {
const std::string& name(*name_iterator++);
declarations()->DeclareParameter(pos, name,
GetParameterVariableFromName(name), t);
declarations()->DeclareParameter(name, GetParameterVariableFromName(name),
t);
}
for (auto& label : signature.labels) {
auto label_params = label.types;
Label* new_label = declarations()->DeclareLabel(pos, label.name);
Label* new_label = declarations()->DeclareLabel(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(
declarations()->DeclareVariable(pos, var_name, var_type));
declarations()->DeclareVariable(var_name, var_type));
}
}
}
......
......@@ -32,71 +32,59 @@ Scope* Declarations::GetGenericScope(Generic* generic,
return result;
}
void Declarations::CheckAlreadyDeclared(SourcePosition pos,
const std::string& name,
void Declarations::CheckAlreadyDeclared(const std::string& name,
const char* new_type) {
auto i = chain_.ShallowLookup(name);
if (i != nullptr) {
std::stringstream s;
s << "cannot redeclare " << name << " (type " << new_type << ") at "
<< PositionAsString(pos) << std::endl;
s << "cannot redeclare " << name << " (type " << new_type << ")";
ReportError(s.str());
}
}
const Type* Declarations::LookupType(SourcePosition pos,
const std::string& name) {
Declarable* raw = Lookup(pos, name);
const Type* Declarations::LookupType(const std::string& name) {
Declarable* raw = Lookup(name);
if (raw->IsType()) {
return Type::cast(raw);
} else if (raw->IsTypeAlias()) {
return TypeAlias::cast(raw)->type();
}
std::stringstream s;
s << "declaration \"" << name << "\" is not a Type at "
<< PositionAsString(pos);
s << "declaration \"" << name << "\" is not a Type";
ReportError(s.str());
return nullptr;
}
const Type* Declarations::LookupGlobalType(const std::string& name) {
return Type::cast(LookupGlobalScope(name));
}
const Type* Declarations::LookupGlobalType(SourcePosition pos,
const std::string& name) {
Declarable* raw = LookupGlobalScope(pos, name);
Declarable* raw = LookupGlobalScope(name);
if (!raw->IsType()) {
std::stringstream s;
s << "declaration \"" << name << "\" is not a Type at "
<< PositionAsString(pos);
s << "declaration \"" << name << "\" is not a Type";
ReportError(s.str());
}
return Type::cast(raw);
}
const Type* Declarations::GetFunctionPointerType(SourcePosition pos,
TypeVector argument_types,
const Type* Declarations::GetFunctionPointerType(TypeVector argument_types,
const Type* return_type) {
const Type* code_type = LookupGlobalType(pos, CODE_TYPE_STRING);
const Type* code_type = LookupGlobalType(CODE_TYPE_STRING);
return function_pointer_types_.Add(
FunctionPointerType(code_type, argument_types, return_type));
}
const Type* Declarations::GetType(SourcePosition pos,
TypeExpression* type_expression) {
const Type* Declarations::GetType(TypeExpression* type_expression) {
if (auto* basic = BasicTypeExpression::DynamicCast(type_expression)) {
std::string name =
(basic->is_constexpr ? CONSTEXPR_TYPE_PREFIX : "") + basic->name;
return LookupType(pos, name);
return LookupType(name);
} else {
auto* function_type_exp = FunctionTypeExpression::cast(type_expression);
TypeVector argument_types;
for (TypeExpression* type_exp : function_type_exp->parameters.types) {
argument_types.push_back(GetType(pos, type_exp));
argument_types.push_back(GetType(type_exp));
}
return GetFunctionPointerType(pos, argument_types,
GetType(pos, function_type_exp->return_type));
return GetFunctionPointerType(argument_types,
GetType(function_type_exp->return_type));
}
}
......@@ -115,29 +103,27 @@ Builtin* Declarations::FindSomeInternalBuiltinWithType(
return nullptr;
}
Value* Declarations::LookupValue(SourcePosition pos, const std::string& name) {
Declarable* d = Lookup(pos, name);
Value* Declarations::LookupValue(const std::string& name) {
Declarable* d = Lookup(name);
if (!d->IsValue()) {
std::stringstream s;
s << "declaration \"" << name << "\" is not a Value at "
<< PositionAsString(pos);
s << "declaration \"" << name << "\" is not a Value";
ReportError(s.str());
}
return Value::cast(d);
}
Label* Declarations::LookupLabel(SourcePosition pos, const std::string& name) {
Declarable* d = Lookup(pos, name);
Label* Declarations::LookupLabel(const std::string& name) {
Declarable* d = Lookup(name);
if (!d->IsLabel()) {
std::stringstream s;
s << "declaration \"" << name << "\" is not a Label at "
<< PositionAsString(pos);
s << "declaration \"" << name << "\" is not a Label";
ReportError(s.str());
}
return Label::cast(d);
}
Macro* Declarations::LookupMacro(SourcePosition pos, const std::string& name,
Macro* Declarations::LookupMacro(const std::string& name,
const TypeVector& types) {
Declarable* declarable = Lookup(name);
if (declarable != nullptr) {
......@@ -152,59 +138,51 @@ Macro* Declarations::LookupMacro(SourcePosition pos, const std::string& name,
}
std::stringstream stream;
stream << "macro " << name << " with parameter types " << types
<< " referenced at " << PositionAsString(pos) << " is not defined";
<< " is not defined";
ReportError(stream.str());
return nullptr;
}
Builtin* Declarations::LookupBuiltin(SourcePosition pos,
const std::string& name) {
Builtin* Declarations::LookupBuiltin(const std::string& name) {
Declarable* declarable = Lookup(name);
if (declarable != nullptr) {
if (declarable->IsBuiltin()) {
return Builtin::cast(declarable);
}
ReportError(name + " referenced at " + PositionAsString(pos) +
" is not a builtin");
ReportError(name + " is not a builtin");
}
ReportError(std::string("builtin ") + name + " referenced at " +
PositionAsString(pos) + " is not defined");
ReportError(std::string("builtin ") + name + " is not defined");
return nullptr;
}
Generic* Declarations::LookupGeneric(const SourcePosition& pos,
const std::string& name) {
Generic* Declarations::LookupGeneric(const std::string& name) {
Declarable* declarable = Lookup(name);
if (declarable != nullptr) {
if (declarable->IsGeneric()) {
return Generic::cast(declarable);
}
ReportError(name + " referenced at " + PositionAsString(pos) +
" is not a generic");
ReportError(name + " is not a generic");
}
ReportError(std::string("generic ") + name + " referenced at " +
PositionAsString(pos) + " is not defined");
ReportError(std::string("generic ") + name + " is not defined");
return nullptr;
}
const AbstractType* Declarations::DeclareAbstractType(
SourcePosition pos, const std::string& name, const std::string& generated,
const std::string& name, const std::string& generated,
const std::string* parent) {
CheckAlreadyDeclared(pos, name, "type");
CheckAlreadyDeclared(name, "type");
const Type* parent_type = nullptr;
if (parent != nullptr) {
Declarable* maybe_parent_type = Lookup(*parent);
if (maybe_parent_type == nullptr) {
std::stringstream s;
s << "cannot find parent type \"" << *parent << "\" at "
<< PositionAsString(pos);
s << "cannot find parent type \"" << *parent << "\"";
ReportError(s.str());
}
if (!maybe_parent_type->IsType()) {
std::stringstream s;
s << "parent \"" << *parent << "\" of type \"" << name << "\""
<< " is not a type "
<< " at " << PositionAsString(pos);
<< " is not a type";
ReportError(s.str());
}
parent_type = Type::cast(maybe_parent_type);
......@@ -214,21 +192,21 @@ const AbstractType* Declarations::DeclareAbstractType(
return result;
}
void Declarations::DeclareTypeAlias(SourcePosition pos, const std::string& name,
void Declarations::DeclareTypeAlias(const std::string& name,
const Type* aliased_type) {
CheckAlreadyDeclared(pos, name, "aliased type");
CheckAlreadyDeclared(name, "aliased type");
TypeAlias* result = new TypeAlias(name, aliased_type);
Declare(name, std::unique_ptr<TypeAlias>(result));
}
Label* Declarations::DeclareLabel(SourcePosition pos, const std::string& name) {
CheckAlreadyDeclared(pos, name, "label");
Label* Declarations::DeclareLabel(const std::string& name) {
CheckAlreadyDeclared(name, "label");
Label* result = new Label(name);
Declare(name, std::unique_ptr<Declarable>(result));
return result;
}
Macro* Declarations::DeclareMacro(SourcePosition pos, const std::string& name,
Macro* Declarations::DeclareMacro(const std::string& name,
const Signature& signature) {
auto previous = chain_.Lookup(name);
MacroList* macro_list = nullptr;
......@@ -237,8 +215,7 @@ Macro* Declarations::DeclareMacro(SourcePosition pos, const std::string& name,
Declare(name, std::unique_ptr<Declarable>(macro_list));
} else if (!previous->IsMacroList()) {
std::stringstream s;
s << "cannot redeclare non-macro " << name << " as a macro at "
<< PositionAsString(pos);
s << "cannot redeclare non-macro " << name << " as a macro";
ReportError(s.str());
} else {
macro_list = MacroList::cast(previous);
......@@ -251,72 +228,67 @@ Macro* Declarations::DeclareMacro(SourcePosition pos, const std::string& name,
std::stringstream s;
s << "cannot redeclare " << name
<< " as a macro with identical parameter list "
<< signature.parameter_types << PositionAsString(pos);
<< signature.parameter_types;
ReportError(s.str());
}
}
return macro_list->AddMacro(new Macro(name, signature));
}
Builtin* Declarations::DeclareBuiltin(SourcePosition pos,
const std::string& name,
Builtin* Declarations::DeclareBuiltin(const std::string& name,
Builtin::Kind kind, bool external,
const Signature& signature) {
CheckAlreadyDeclared(pos, name, "builtin");
CheckAlreadyDeclared(name, "builtin");
Builtin* result = new Builtin(name, kind, external, signature);
Declare(name, std::unique_ptr<Declarable>(result));
return result;
}
RuntimeFunction* Declarations::DeclareRuntimeFunction(
SourcePosition pos, const std::string& name, const Signature& signature) {
CheckAlreadyDeclared(pos, name, "runtime function");
const std::string& name, const Signature& signature) {
CheckAlreadyDeclared(name, "runtime function");
RuntimeFunction* result = new RuntimeFunction(name, signature);
Declare(name, std::unique_ptr<Declarable>(result));
return result;
}
Variable* Declarations::DeclareVariable(SourcePosition pos,
const std::string& var,
Variable* Declarations::DeclareVariable(const std::string& var,
const Type* type) {
std::string name(var + std::to_string(GetNextUniqueDeclarationNumber()));
CheckAlreadyDeclared(pos, var, "variable");
CheckAlreadyDeclared(var, "variable");
Variable* result = new Variable(var, name, type);
Declare(var, std::unique_ptr<Declarable>(result));
return result;
}
Parameter* Declarations::DeclareParameter(SourcePosition pos,
const std::string& name,
Parameter* Declarations::DeclareParameter(const std::string& name,
const std::string& var_name,
const Type* type) {
CheckAlreadyDeclared(pos, name, "parameter");
CheckAlreadyDeclared(name, "parameter");
Parameter* result = new Parameter(name, type, var_name);
Declare(name, std::unique_ptr<Declarable>(result));
return result;
}
Label* Declarations::DeclarePrivateLabel(SourcePosition pos,
const std::string& raw_name) {
Label* Declarations::DeclarePrivateLabel(const std::string& raw_name) {
std::string name =
raw_name + "_" + std::to_string(GetNextUniqueDeclarationNumber());
CheckAlreadyDeclared(pos, name, "label");
CheckAlreadyDeclared(name, "label");
Label* result = new Label(name);
Declare(name, std::unique_ptr<Declarable>(result));
return result;
}
void Declarations::DeclareConstant(SourcePosition pos, const std::string& name,
const Type* type, const std::string& value) {
CheckAlreadyDeclared(pos, name, "constant, parameter or arguments");
void Declarations::DeclareConstant(const std::string& name, const Type* type,
const std::string& value) {
CheckAlreadyDeclared(name, "constant, parameter or arguments");
Constant* result = new Constant(name, type, value);
Declare(name, std::unique_ptr<Declarable>(result));
}
Generic* Declarations::DeclareGeneric(SourcePosition pos,
const std::string& name, Module* module,
Generic* Declarations::DeclareGeneric(const std::string& name, Module* module,
GenericDeclaration* generic) {
CheckAlreadyDeclared(pos, name, "generic");
CheckAlreadyDeclared(name, "generic");
Generic* result = new Generic(name, module, generic);
Declare(name, std::unique_ptr<Generic>(result));
generic_declaration_scopes_[result] = GetScopeChainSnapshot();
......
......@@ -17,95 +17,80 @@ namespace torque {
class Declarations {
public:
explicit Declarations(SourceFileMap* source_file_map)
: source_file_map_(source_file_map),
unique_declaration_number_(0),
Declarations()
: unique_declaration_number_(0),
current_generic_specialization_(nullptr) {}
Declarable* Lookup(const std::string& name) { return chain_.Lookup(name); }
Declarable* TryLookup(const std::string& name) { return chain_.Lookup(name); }
Declarable* Lookup(SourcePosition pos, const std::string& name) {
Declarable* d = Lookup(name);
Declarable* Lookup(const std::string& name) {
Declarable* d = TryLookup(name);
if (d == nullptr) {
std::stringstream s;
s << "cannot find \"" << name << "\" at " << PositionAsString(pos);
s << "cannot find \"" << name << "\"";
ReportError(s.str());
}
return d;
}
Declarable* LookupGlobalScope(const std::string& name) {
return chain_.LookupGlobalScope(name);
}
Declarable* LookupGlobalScope(SourcePosition pos, const std::string& name) {
Declarable* d = chain_.LookupGlobalScope(name);
if (d == nullptr) {
std::stringstream s;
s << "cannot find \"" << name << "\" in global scope at "
<< PositionAsString(pos);
s << "cannot find \"" << name << "\" in global scope";
ReportError(s.str());
}
return d;
}
const Type* LookupType(SourcePosition pos, const std::string& name);
const Type* LookupType(const std::string& name);
const Type* LookupGlobalType(const std::string& name);
const Type* LookupGlobalType(SourcePosition pos, const std::string& name);
const Type* GetType(SourcePosition pos, TypeExpression* type_expression);
const Type* GetType(TypeExpression* type_expression);
const Type* GetFunctionPointerType(SourcePosition pos,
TypeVector argument_types,
const Type* GetFunctionPointerType(TypeVector argument_types,
const Type* return_type);
Builtin* FindSomeInternalBuiltinWithType(const FunctionPointerType* type);
Value* LookupValue(SourcePosition pos, const std::string& name);
Value* LookupValue(const std::string& name);
Macro* LookupMacro(SourcePosition pos, const std::string& name,
const TypeVector& types);
Macro* LookupMacro(const std::string& name, const TypeVector& types);
Builtin* LookupBuiltin(SourcePosition pos, const std::string& name);
Builtin* LookupBuiltin(const std::string& name);
Label* LookupLabel(SourcePosition pos, const std::string& name);
Label* LookupLabel(const std::string& name);
Generic* LookupGeneric(const SourcePosition& pos, const std::string& name);
Generic* LookupGeneric(const std::string& name);
const AbstractType* DeclareAbstractType(SourcePosition pos,
const std::string& name,
const AbstractType* DeclareAbstractType(const std::string& name,
const std::string& generated,
const std::string* parent = nullptr);
void DeclareTypeAlias(SourcePosition pos, const std::string& name,
const Type* aliased_type);
void DeclareTypeAlias(const std::string& name, const Type* aliased_type);
Label* DeclareLabel(SourcePosition pos, const std::string& name);
Label* DeclareLabel(const std::string& name);
Macro* DeclareMacro(SourcePosition pos, const std::string& name,
const Signature& signature);
Macro* DeclareMacro(const std::string& name, const Signature& signature);
Builtin* DeclareBuiltin(SourcePosition pos, const std::string& name,
Builtin::Kind kind, bool external,
const Signature& signature);
Builtin* DeclareBuiltin(const std::string& name, Builtin::Kind kind,
bool external, const Signature& signature);
RuntimeFunction* DeclareRuntimeFunction(SourcePosition pos,
const std::string& name,
RuntimeFunction* DeclareRuntimeFunction(const std::string& name,
const Signature& signature);
Variable* DeclareVariable(SourcePosition pos, const std::string& var,
const Type* type);
Variable* DeclareVariable(const std::string& var, const Type* type);
Parameter* DeclareParameter(SourcePosition pos, const std::string& name,
Parameter* DeclareParameter(const std::string& name,
const std::string& mangled_name,
const Type* type);
Label* DeclarePrivateLabel(SourcePosition pos, const std::string& name);
Label* DeclarePrivateLabel(const std::string& name);
void DeclareConstant(SourcePosition pos, const std::string& name,
const Type* type, const std::string& value);
void DeclareConstant(const std::string& name, const Type* type,
const std::string& value);
Generic* DeclareGeneric(SourcePosition pos, const std::string& name,
Module* module, GenericDeclaration* generic);
Generic* DeclareGeneric(const std::string& name, Module* module,
GenericDeclaration* generic);
TypeVector GetCurrentSpecializationTypeNamesVector();
......@@ -115,10 +100,6 @@ class Declarations {
return chain_.GetLiveVariables();
}
std::string PositionAsString(SourcePosition pos) {
return source_file_map_->PositionAsString(pos);
}
Statement* next_body() const { return next_body_; }
void PrintScopeChain() { chain_.Print(); }
......@@ -144,10 +125,8 @@ class Declarations {
int GetNextUniqueDeclarationNumber() { return unique_declaration_number_++; }
void CheckAlreadyDeclared(SourcePosition pos, const std::string& name,
const char* new_type);
void CheckAlreadyDeclared(const std::string& name, const char* new_type);
SourceFileMap* source_file_map_;
int unique_declaration_number_;
ScopeChain chain_;
const SpecializationKey* current_generic_specialization_;
......
......@@ -15,13 +15,13 @@ Signature FileVisitor::MakeSignature(CallableNode* decl,
Declarations::NodeScopeActivator scope(declarations(), decl);
LabelDeclarationVector definition_vector;
for (auto label : signature->labels) {
LabelDeclaration def = {label.name, GetTypeVector(decl->pos, label.types)};
LabelDeclaration def = {label.name, GetTypeVector(label.types)};
definition_vector.push_back(def);
}
Signature result{signature->parameters.names,
{GetTypeVector(decl->pos, signature->parameters.types),
{GetTypeVector(signature->parameters.types),
signature->parameters.has_varargs},
declarations()->GetType(decl->pos, signature->return_type),
declarations()->GetType(signature->return_type),
definition_vector};
return result;
}
......@@ -36,10 +36,10 @@ std::string FileVisitor::GetGeneratedCallableName(
return result;
}
Callable* FileVisitor::LookupCall(SourcePosition pos, const std::string& name,
Callable* FileVisitor::LookupCall(const std::string& name,
const TypeVector& parameter_types) {
Callable* result = nullptr;
Declarable* declarable = declarations()->Lookup(pos, name);
Declarable* declarable = declarations()->Lookup(name);
if (declarable->IsBuiltin()) {
result = Builtin::cast(declarable);
} else if (declarable->IsRuntimeFunction()) {
......@@ -52,8 +52,7 @@ Callable* FileVisitor::LookupCall(SourcePosition pos, const std::string& name,
std::stringstream stream;
stream << "multiple matching matching parameter list for macro "
<< name << ": (" << parameter_types << ") and ("
<< result->signature().parameter_types << ") at "
<< PositionAsString(pos);
<< result->signature().parameter_types << ")";
ReportError(stream.str());
}
result = m;
......@@ -62,16 +61,14 @@ Callable* FileVisitor::LookupCall(SourcePosition pos, const std::string& name,
if (result == nullptr) {
std::stringstream stream;
stream << "no matching matching parameter list for macro " << name
<< ": call parameters were (" << parameter_types << ") at "
<< PositionAsString(pos);
<< ": call parameters were (" << parameter_types << ")";
ReportError(stream.str());
}
} else {
std::stringstream stream;
stream << "can't call " << declarable->type_name() << " " << name
<< " because it's not callable"
<< ": call parameters were (" << parameter_types << ") at "
<< PositionAsString(pos);
<< ": call parameters were (" << parameter_types << ")";
ReportError(stream.str());
}
......
......@@ -26,11 +26,10 @@ class FileVisitor {
declarations_(global_context.declarations()),
module_(global_context.GetDefaultModule()) {}
TypeVector GetTypeVector(SourcePosition pos,
const std::vector<TypeExpression*>& v) {
TypeVector GetTypeVector(const std::vector<TypeExpression*>& v) {
TypeVector result;
for (TypeExpression* t : v) {
result.push_back(declarations()->GetType(pos, t));
result.push_back(declarations()->GetType(t));
}
return result;
}
......@@ -70,11 +69,7 @@ class FileVisitor {
return std::string("p_") + name;
}
std::string PositionAsString(SourcePosition pos) {
return global_context_.ast()->source_file_map()->PositionAsString(pos);
}
Callable* LookupCall(SourcePosition pos, const std::string& name,
Callable* LookupCall(const std::string& name,
const TypeVector& parameter_types);
Signature MakeSignature(CallableNode* decl,
......
......@@ -56,7 +56,6 @@ class GlobalContext {
explicit GlobalContext(Ast ast)
: verbose_(false),
next_label_number_(0),
declarations_(ast.source_file_map()),
type_oracle_(&declarations_),
default_module_(GetModule("base")),
ast_(std::move(ast)) {}
......@@ -115,10 +114,6 @@ class GlobalContext {
std::map<std::string, std::vector<OperationHandler>> op_handlers_;
std::string PositionAsString(SourcePosition pos) {
return declarations()->PositionAsString(pos);
}
Declarations* declarations() { return &declarations_; }
Ast* ast() { return &ast_; }
......
......@@ -11,6 +11,7 @@ namespace internal {
namespace torque {
VisitResult ImplementationVisitor::Visit(Expression* expr) {
CurrentSourcePosition::Scope scope(expr->pos);
switch (expr->kind) {
#define ENUM_ITEM(name) \
case AstNode::Kind::k##name: \
......@@ -24,6 +25,7 @@ VisitResult ImplementationVisitor::Visit(Expression* expr) {
}
const Type* ImplementationVisitor::Visit(Statement* stmt) {
CurrentSourcePosition::Scope scope(stmt->pos);
switch (stmt->kind) {
#define ENUM_ITEM(name) \
case AstNode::Kind::k##name: \
......@@ -38,6 +40,7 @@ const Type* ImplementationVisitor::Visit(Statement* stmt) {
}
void ImplementationVisitor::Visit(Declaration* decl) {
CurrentSourcePosition::Scope scope(decl->pos);
switch (decl->kind) {
#define ENUM_ITEM(name) \
case AstNode::Kind::k##name: \
......@@ -152,17 +155,16 @@ void ImplementationVisitor::Visit(TorqueMacroDeclaration* decl,
std::string name = GetGeneratedCallableName(
decl->name, declarations()->GetCurrentSpecializationTypeNamesVector());
const TypeVector& list = signature.types();
Macro* macro = declarations()->LookupMacro(decl->pos, name, list);
Macro* macro = declarations()->LookupMacro(name, list);
CurrentCallableActivator activator(global_context_, macro, decl);
header_out() << " ";
GenerateMacroFunctionDeclaration(header_out(), decl->pos, "", macro);
GenerateMacroFunctionDeclaration(header_out(), "", macro);
header_out() << ";" << std::endl;
GenerateMacroFunctionDeclaration(
source_out(), decl->pos,
GetDSLAssemblerName(CurrentModule()) + "::", macro);
source_out(), GetDSLAssemblerName(CurrentModule()) + "::", macro);
source_out() << " {" << std::endl;
const Variable* result_var = nullptr;
......@@ -181,7 +183,7 @@ void ImplementationVisitor::Visit(TorqueMacroDeclaration* decl,
result_var =
GenerateVariableDeclaration(decl, kReturnValueVariable, {}, init);
}
Label* macro_end = declarations()->DeclareLabel(decl->pos, "macro_end");
Label* macro_end = declarations()->DeclareLabel("macro_end");
GenerateLabelDefinition(macro_end, decl);
const Type* result = Visit(body);
......@@ -189,8 +191,7 @@ void ImplementationVisitor::Visit(TorqueMacroDeclaration* decl,
if (!macro->signature().return_type->IsNever() && !macro->HasReturns()) {
std::stringstream s;
s << "macro " << decl->name
<< " that never returns must have return type never at "
<< PositionAsString(decl->pos);
<< " that never returns must have return type never";
ReportError(s.str());
}
} else {
......@@ -198,14 +199,12 @@ void ImplementationVisitor::Visit(TorqueMacroDeclaration* decl,
std::stringstream s;
s << "macro " << decl->name
<< " has implicit return at end of its declartion but return type "
"never at "
<< PositionAsString(decl->pos);
"never";
ReportError(s.str());
} else if (!macro->signature().return_type->IsVoid()) {
std::stringstream s;
s << "macro " << decl->name
<< " expects to return a value but doesn't on all paths at "
<< PositionAsString(decl->pos);
<< " expects to return a value but doesn't on all paths";
ReportError(s.str());
}
}
......@@ -229,12 +228,12 @@ void ImplementationVisitor::Visit(TorqueBuiltinDeclaration* decl,
decl->name, declarations()->GetCurrentSpecializationTypeNamesVector());
source_out() << "TF_BUILTIN(" << name << ", "
<< GetDSLAssemblerName(CurrentModule()) << ") {" << std::endl;
Builtin* builtin = declarations()->LookupBuiltin(decl->pos, name);
Builtin* builtin = declarations()->LookupBuiltin(name);
CurrentCallableActivator activator(global_context_, builtin, decl);
// Context
const Value* val = declarations()->LookupValue(
decl->pos, decl->signature->parameters.names[0]);
const Value* val =
declarations()->LookupValue(decl->signature->parameters.names[0]);
GenerateIndent();
source_out() << "TNode<Context> " << val->GetValueForDeclaration()
<< " = UncheckedCast<Context>(Parameter("
......@@ -247,7 +246,7 @@ void ImplementationVisitor::Visit(TorqueBuiltinDeclaration* decl,
if (builtin->IsVarArgsJavaScript()) {
assert(decl->signature->parameters.has_varargs);
Constant* arguments = Constant::cast(declarations()->LookupValue(
decl->pos, decl->signature->parameters.arguments_variable));
decl->signature->parameters.arguments_variable));
std::string arguments_name = arguments->GetValueForDeclaration();
GenerateIndent();
source_out()
......@@ -257,8 +256,8 @@ void ImplementationVisitor::Visit(TorqueBuiltinDeclaration* decl,
source_out() << "CodeStubArguments arguments_impl(this, "
"ChangeInt32ToIntPtr(argc));"
<< std::endl;
const Value* receiver = declarations()->LookupValue(
decl->pos, decl->signature->parameters.names[1]);
const Value* receiver =
declarations()->LookupValue(decl->signature->parameters.names[1]);
GenerateIndent();
source_out() << "TNode<Object> " << receiver->GetValueForDeclaration()
<< " = arguments_impl.GetReceiver();" << std::endl;
......@@ -272,7 +271,7 @@ void ImplementationVisitor::Visit(TorqueBuiltinDeclaration* decl,
first = 2;
}
GenerateParameterList(decl->pos, decl->signature->parameters.names, first);
GenerateParameterList(decl->signature->parameters.names, first);
Visit(body);
source_out() << "}" << std::endl << std::endl;
}
......@@ -320,7 +319,7 @@ VisitResult ImplementationVisitor::Visit(ConditionalExpression* expr) {
}
source_out() << ";" << std::endl;
const Type* common_type = GetCommonType(expr->pos, left.type(), right.type());
const Type* common_type = GetCommonType(left.type(), right.type());
const Variable* result =
GenerateVariableDeclaration(expr, kConditionValueVariable, common_type);
......@@ -328,13 +327,11 @@ VisitResult ImplementationVisitor::Visit(ConditionalExpression* expr) {
ScopedIndent indent(this);
Declarations::NodeScopeActivator scope(declarations(), expr->condition);
Label* true_label = declarations()->LookupLabel(expr->pos, kTrueLabelName);
Label* true_label = declarations()->LookupLabel(kTrueLabelName);
GenerateLabelDefinition(true_label);
Label* false_label =
declarations()->LookupLabel(expr->pos, kFalseLabelName);
Label* false_label = declarations()->LookupLabel(kFalseLabelName);
GenerateLabelDefinition(false_label);
Label* done_label =
declarations()->DeclarePrivateLabel(expr->pos, kDoneLabelName);
Label* done_label = declarations()->DeclarePrivateLabel(kDoneLabelName);
GenerateLabelDefinition(done_label, expr);
VisitResult condition_result = Visit(expr->condition);
......@@ -363,13 +360,11 @@ VisitResult ImplementationVisitor::Visit(LogicalOrExpression* expr) {
VisitResult left_result;
{
Declarations::NodeScopeActivator scope(declarations(), expr->left);
Label* false_label =
declarations()->LookupLabel(expr->pos, kFalseLabelName);
Label* false_label = declarations()->LookupLabel(kFalseLabelName);
GenerateLabelDefinition(false_label);
left_result = Visit(expr->left);
if (left_result.type()->IsBool()) {
Label* true_label =
declarations()->LookupLabel(expr->pos, kTrueLabelName);
Label* true_label = declarations()->LookupLabel(kTrueLabelName);
GenerateIndent();
source_out() << "GotoIf(" << left_result.variable() << ", "
<< true_label->generated() << ");" << std::endl;
......@@ -381,8 +376,7 @@ VisitResult ImplementationVisitor::Visit(LogicalOrExpression* expr) {
if (right_result.type() != left_result.type()) {
std::stringstream stream;
stream << "types of left and right expression of logical OR don't match (\""
<< left_result.type() << "\" vs. \"" << right_result.type()
<< "\") at " << PositionAsString(expr->pos);
<< left_result.type() << "\" vs. \"" << right_result.type() << "\")";
ReportError(stream.str());
}
if (left_result.type()->IsConstexprBool()) {
......@@ -398,12 +392,11 @@ VisitResult ImplementationVisitor::Visit(LogicalAndExpression* expr) {
VisitResult left_result;
{
Declarations::NodeScopeActivator scope(declarations(), expr->left);
Label* true_label = declarations()->LookupLabel(expr->pos, kTrueLabelName);
Label* true_label = declarations()->LookupLabel(kTrueLabelName);
GenerateLabelDefinition(true_label);
left_result = Visit(expr->left);
if (left_result.type()->IsBool()) {
Label* false_label =
declarations()->LookupLabel(expr->pos, kFalseLabelName);
Label* false_label = declarations()->LookupLabel(kFalseLabelName);
GenerateIndent();
source_out() << "GotoIfNot(" << left_result.variable() << ", "
<< false_label->generated() << ");" << std::endl;
......@@ -416,8 +409,7 @@ VisitResult ImplementationVisitor::Visit(LogicalAndExpression* expr) {
std::stringstream stream;
stream
<< "types of left and right expression of logical AND don't match (\""
<< left_result.type() << "\" vs. \"" << right_result.type() << "\") at "
<< PositionAsString(expr->pos);
<< left_result.type() << "\" vs. \"" << right_result.type() << "\")";
ReportError(stream.str());
}
if (left_result.type()->IsConstexprBool()) {
......@@ -441,8 +433,7 @@ VisitResult ImplementationVisitor::Visit(IncrementDecrementExpression* expr) {
Arguments args;
args.parameters = {current_value, one};
VisitResult assignment_value = GenerateOperation(
expr->pos, expr->op == IncrementDecrementOperator::kIncrement ? "+" : "-",
args);
expr->op == IncrementDecrementOperator::kIncrement ? "+" : "-", args);
GenerateAssignToLocation(expr->location, location_ref, assignment_value);
return expr->postfix ? value_copy : assignment_value;
}
......@@ -456,7 +447,7 @@ VisitResult ImplementationVisitor::Visit(AssignmentExpression* expr) {
assignment_value = Visit(expr->value);
Arguments args;
args.parameters = {assignment_value, assignment_value};
assignment_value = GenerateOperation(expr->pos, *expr->op, args);
assignment_value = GenerateOperation(*expr->op, args);
GenerateAssignToLocation(expr->location, location_ref, assignment_value);
} else {
assignment_value = Visit(expr->value);
......@@ -470,15 +461,13 @@ VisitResult ImplementationVisitor::Visit(NumberLiteralExpression* expr) {
double d = std::stod(expr->number.c_str());
int32_t i = static_cast<int32_t>(d);
const Type* result_type =
declarations()->LookupType(expr->pos, CONST_FLOAT64_TYPE_STRING);
declarations()->LookupType(CONST_FLOAT64_TYPE_STRING);
if (i == d) {
if (Internals::IsValidSmi(i)) {
if (sizeof(void*) == sizeof(double) && ((i >> 30) != (i >> 31))) {
result_type =
declarations()->LookupType(expr->pos, CONST_INT32_TYPE_STRING);
result_type = declarations()->LookupType(CONST_INT32_TYPE_STRING);
} else {
result_type =
declarations()->LookupType(expr->pos, CONST_INT31_TYPE_STRING);
result_type = declarations()->LookupType(CONST_INT31_TYPE_STRING);
}
}
}
......@@ -495,17 +484,14 @@ VisitResult ImplementationVisitor::Visit(StringLiteralExpression* expr) {
return VisitResult{GetTypeOracle().GetStringType(), temp};
}
VisitResult ImplementationVisitor::GetBuiltinCode(SourcePosition pos,
Builtin* builtin) {
VisitResult ImplementationVisitor::GetBuiltinCode(Builtin* builtin) {
if (builtin->IsExternal() || builtin->kind() != Builtin::kStub) {
std::stringstream s;
s << "creating function pointers is only allowed for internal builtins "
"with stub linkage at "
<< PositionAsString(pos);
ReportError(s.str());
ReportError(
"creating function pointers is only allowed for internal builtins with "
"stub linkage");
}
const Type* type = declarations()->GetFunctionPointerType(
pos, builtin->signature().parameter_types.types,
builtin->signature().parameter_types.types,
builtin->signature().return_type);
std::string code =
"HeapConstant(Builtins::CallableFor(isolate(), Builtins::k" +
......@@ -516,27 +502,25 @@ VisitResult ImplementationVisitor::GetBuiltinCode(SourcePosition pos,
VisitResult ImplementationVisitor::Visit(CastExpression* expr) {
Arguments args;
args.parameters = {Visit(expr->value)};
args.labels = LabelsFromIdentifiers(expr->pos, {expr->otherwise});
return GenerateOperation(expr->pos, "cast<>", args,
declarations()->GetType(expr->pos, expr->type));
args.labels = LabelsFromIdentifiers({expr->otherwise});
return GenerateOperation("cast<>", args, declarations()->GetType(expr->type));
}
VisitResult ImplementationVisitor::Visit(ConvertExpression* expr) {
Arguments args;
args.parameters = {Visit(expr->value)};
return GenerateOperation(expr->pos, "convert<>", args,
declarations()->GetType(expr->pos, expr->type));
return GenerateOperation("convert<>", args,
declarations()->GetType(expr->type));
}
const Type* ImplementationVisitor::Visit(GotoStatement* stmt) {
Label* label = declarations()->LookupLabel(stmt->pos, stmt->label);
Label* label = declarations()->LookupLabel(stmt->label);
if (stmt->arguments.size() != label->GetParameterCount()) {
std::stringstream stream;
stream << "goto to label has incorrect number of parameters (expected "
<< std::to_string(label->GetParameterCount()) << " found "
<< std::to_string(stmt->arguments.size()) << ") at "
<< PositionAsString(stmt->pos);
<< std::to_string(stmt->arguments.size()) << ")";
ReportError(stream.str());
}
......@@ -544,7 +528,7 @@ const Type* ImplementationVisitor::Visit(GotoStatement* stmt) {
for (Expression* e : stmt->arguments) {
VisitResult result = Visit(e);
Variable* var = label->GetParameter(i++);
GenerateAssignToVariable(e->pos, var, result);
GenerateAssignToVariable(var, result);
}
GenerateLabelGoto(label);
......@@ -562,9 +546,7 @@ const Type* ImplementationVisitor::Visit(IfStatement* stmt) {
if (!(expression_result.type() == GetTypeOracle().GetConstexprBoolType())) {
std::stringstream stream;
stream
<< "expression should return type \"constexpr bool\" but doesn't at"
<< PositionAsString(stmt->pos);
stream << "expression should return type \"constexpr bool\" but doesn't";
ReportError(stream.str());
}
......@@ -600,17 +582,16 @@ const Type* ImplementationVisitor::Visit(IfStatement* stmt) {
Label* false_label = nullptr;
{
Declarations::NodeScopeActivator scope(declarations(), &*stmt->condition);
true_label = declarations()->LookupLabel(stmt->pos, kTrueLabelName);
true_label = declarations()->LookupLabel(kTrueLabelName);
GenerateLabelDefinition(true_label);
false_label = declarations()->LookupLabel(stmt->pos, kFalseLabelName);
false_label = declarations()->LookupLabel(kFalseLabelName);
GenerateLabelDefinition(false_label, !has_else ? stmt : nullptr);
}
Label* done_label = nullptr;
bool live = false;
if (has_else) {
done_label =
declarations()->DeclarePrivateLabel(stmt->pos, "if_done_label");
done_label = declarations()->DeclarePrivateLabel("if_done_label");
GenerateLabelDefinition(done_label, stmt);
} else {
done_label = false_label;
......@@ -637,14 +618,13 @@ const Type* ImplementationVisitor::Visit(WhileStatement* stmt) {
Label* exit_label = nullptr;
{
Declarations::NodeScopeActivator scope(declarations(), stmt->condition);
body_label = declarations()->LookupLabel(stmt->pos, kTrueLabelName);
body_label = declarations()->LookupLabel(kTrueLabelName);
GenerateLabelDefinition(body_label);
exit_label = declarations()->LookupLabel(stmt->pos, kFalseLabelName);
exit_label = declarations()->LookupLabel(kFalseLabelName);
GenerateLabelDefinition(exit_label);
}
Label* header_label =
declarations()->DeclarePrivateLabel(stmt->pos, "header");
Label* header_label = declarations()->DeclarePrivateLabel("header");
GenerateLabelDefinition(header_label, stmt);
GenerateLabelGoto(header_label);
GenerateLabelBind(header_label);
......@@ -666,8 +646,7 @@ const Type* ImplementationVisitor::Visit(BlockStatement* block) {
for (Statement* s : block->statements) {
if (type->IsNever()) {
std::stringstream stream;
stream << "statement after non-returning statement at "
<< PositionAsString(s->pos);
stream << "statement after non-returning statement";
ReportError(stream.str());
}
type = Visit(s);
......@@ -706,12 +685,11 @@ const Type* ImplementationVisitor::Visit(AssertStatement* stmt) {
Label* true_label = nullptr;
Label* false_label = nullptr;
Declarations::NodeScopeActivator scope(declarations(), stmt->expression);
true_label = declarations()->LookupLabel(stmt->pos, kTrueLabelName);
true_label = declarations()->LookupLabel(kTrueLabelName);
GenerateLabelDefinition(true_label);
false_label = declarations()->LookupLabel(stmt->pos, kFalseLabelName);
false_label = declarations()->LookupLabel(kFalseLabelName);
GenerateLabelDefinition(false_label);
Expression* expression = stmt->expression;
VisitResult expression_result = Visit(stmt->expression);
if (expression_result.type() == GetTypeOracle().GetBoolType()) {
GenerateBranch(expression_result, true_label, false_label);
......@@ -719,7 +697,7 @@ const Type* ImplementationVisitor::Visit(AssertStatement* stmt) {
if (expression_result.type() != GetTypeOracle().GetNeverType()) {
std::stringstream s;
s << "unexpected return type " << expression_result.type()
<< " for branch expression at " << PositionAsString(expression->pos);
<< " for branch expression";
ReportError(s.str());
}
}
......@@ -746,29 +724,26 @@ const Type* ImplementationVisitor::Visit(ReturnStatement* stmt) {
Callable* current_callable = global_context_.GetCurrentCallable();
if (current_callable->signature().return_type->IsNever()) {
std::stringstream s;
s << "cannot return from a function with return type never at "
<< PositionAsString(stmt->pos);
s << "cannot return from a function with return type never";
ReportError(s.str());
}
Label* end = current_callable->IsMacro()
? declarations()->LookupLabel(stmt->pos, "macro_end")
? declarations()->LookupLabel("macro_end")
: nullptr;
if (current_callable->HasReturnValue()) {
if (!stmt->value) {
std::stringstream s;
s << "return expression needs to be specified for a return type of "
<< current_callable->signature().return_type << " at "
<< PositionAsString(stmt->pos);
<< current_callable->signature().return_type;
ReportError(s.str());
}
VisitResult expression_result = Visit(*stmt->value);
VisitResult return_result = GenerateImplicitConvert(
stmt->pos, current_callable->signature().return_type,
expression_result);
current_callable->signature().return_type, expression_result);
if (current_callable->IsMacro()) {
Variable* var = Variable::cast(
declarations()->LookupValue(stmt->pos, kReturnValueVariable));
GenerateAssignToVariable(stmt->pos, var, return_result);
Variable* var =
Variable::cast(declarations()->LookupValue(kReturnValueVariable));
GenerateAssignToVariable(var, return_result);
GenerateLabelGoto(end);
} else if (current_callable->IsBuiltin()) {
if (Builtin::cast(current_callable)->IsVarArgsJavaScript()) {
......@@ -787,8 +762,7 @@ const Type* ImplementationVisitor::Visit(ReturnStatement* stmt) {
if (stmt->value) {
std::stringstream s;
s << "return expression can't be specified for a void or never return "
"type at "
<< PositionAsString(stmt->pos);
"type";
ReportError(s.str());
}
GenerateLabelGoto(end);
......@@ -805,19 +779,18 @@ const Type* ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
stmt->begin ? Visit(*stmt->begin)
: VisitResult(GetTypeOracle().GetConstInt31Type(), "0");
VisitResult end = stmt->end ? Visit(*stmt->end)
: GenerateOperation(stmt->pos, ".length",
{{expression_result}, {}});
VisitResult end =
stmt->end ? Visit(*stmt->end)
: GenerateOperation(".length", {{expression_result}, {}});
Label* body_label = declarations()->DeclarePrivateLabel(stmt->pos, "body");
Label* body_label = declarations()->DeclarePrivateLabel("body");
GenerateLabelDefinition(body_label);
Label* increment_label =
declarations()->DeclarePrivateLabel(stmt->pos, "increment");
Label* increment_label = declarations()->DeclarePrivateLabel("increment");
GenerateLabelDefinition(increment_label);
Label* exit_label = declarations()->DeclarePrivateLabel(stmt->pos, "exit");
Label* exit_label = declarations()->DeclarePrivateLabel("exit");
GenerateLabelDefinition(exit_label);
const Type* common_type = GetCommonType(stmt->pos, begin.type(), end.type());
const Type* common_type = GetCommonType(begin.type(), end.type());
Variable* index_var = GenerateVariableDeclaration(
stmt, std::string(kForIndexValueVariable) + "_" + NewTempVariable(),
common_type, begin);
......@@ -825,8 +798,7 @@ const Type* ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
VisitResult index_for_read = {index_var->type(),
index_var->GetValueForRead()};
Label* header_label =
declarations()->DeclarePrivateLabel(stmt->pos, "header");
Label* header_label = declarations()->DeclarePrivateLabel("header");
GenerateLabelDefinition(header_label, stmt);
GenerateLabelGoto(header_label);
......@@ -836,13 +808,12 @@ const Type* ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
BreakContinueActivator activator(global_context_, exit_label,
increment_label);
VisitResult result =
GenerateOperation(stmt->pos, "<", {{index_for_read, end}, {}});
VisitResult result = GenerateOperation("<", {{index_for_read, end}, {}});
GenerateBranch(result, body_label, exit_label);
GenerateLabelBind(body_label);
VisitResult element_result = GenerateOperation(
stmt->pos, "[]", {{expression_result, index_for_read}, {}});
VisitResult element_result =
GenerateOperation("[]", {{expression_result, index_for_read}, {}});
GenerateVariableDeclaration(stmt->var_declaration,
stmt->var_declaration->name, {}, element_result);
Visit(stmt->body);
......@@ -852,10 +823,9 @@ const Type* ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
Arguments increment_args;
increment_args.parameters = {index_for_read,
{GetTypeOracle().GetConstInt31Type(), "1"}};
VisitResult increment_result =
GenerateOperation(stmt->pos, "+", increment_args);
VisitResult increment_result = GenerateOperation("+", increment_args);
GenerateAssignToVariable(stmt->pos, index_var, increment_result);
GenerateAssignToVariable(index_var, increment_result);
GenerateLabelGoto(header_label);
......@@ -865,7 +835,7 @@ const Type* ImplementationVisitor::Visit(ForOfLoopStatement* stmt) {
const Type* ImplementationVisitor::Visit(TryCatchStatement* stmt) {
ScopedIndent indent(this);
Label* try_done = declarations()->DeclarePrivateLabel(stmt->pos, "try_done");
Label* try_done = declarations()->DeclarePrivateLabel("try_done");
GenerateLabelDefinition(try_done);
const Type* try_result = GetTypeOracle().GetNeverType();
std::vector<Label*> labels;
......@@ -875,7 +845,8 @@ const Type* ImplementationVisitor::Visit(TryCatchStatement* stmt) {
// Activate a new scope to see handler labels
Declarations::NodeScopeActivator scope(declarations(), stmt);
for (LabelBlock* block : stmt->label_blocks) {
Label* label = declarations()->LookupLabel(block->pos, block->label);
CurrentSourcePosition::Scope scope(block->pos);
Label* label = declarations()->LookupLabel(block->label);
labels.push_back(label);
GenerateLabelDefinition(label);
}
......@@ -891,8 +862,7 @@ const Type* ImplementationVisitor::Visit(TryCatchStatement* stmt) {
++i;
}
Label* try_begin_label =
declarations()->DeclarePrivateLabel(stmt->pos, "try_begin");
Label* try_begin_label = declarations()->DeclarePrivateLabel("try_begin");
GenerateLabelDefinition(try_begin_label);
GenerateLabelGoto(try_begin_label);
......@@ -909,12 +879,12 @@ const Type* ImplementationVisitor::Visit(TryCatchStatement* stmt) {
// bound labels that are never jumped to.
auto label_iterator = stmt->label_blocks.begin();
for (auto label : labels) {
CurrentSourcePosition::Scope scope((*label_iterator)->pos);
if (!label->IsUsed()) {
std::stringstream s;
s << "label ";
s << (*label_iterator)->label;
s << " has a handler block but is never referred to in try block at "
<< PositionAsString((*label_iterator)->pos);
s << " has a handler block but is never referred to in try block";
ReportError(s.str());
}
label_iterator++;
......@@ -936,7 +906,7 @@ const Type* ImplementationVisitor::Visit(TryCatchStatement* stmt) {
const Type* ImplementationVisitor::Visit(BreakStatement* stmt) {
Label* break_label = global_context_.GetCurrentBreak();
if (break_label == nullptr) {
ReportError("break used outside of loop at " + PositionAsString(stmt->pos));
ReportError("break used outside of loop");
}
GenerateLabelGoto(break_label);
return GetTypeOracle().GetNeverType();
......@@ -945,8 +915,7 @@ const Type* ImplementationVisitor::Visit(BreakStatement* stmt) {
const Type* ImplementationVisitor::Visit(ContinueStatement* stmt) {
Label* continue_label = global_context_.GetCurrentContinue();
if (continue_label == nullptr) {
ReportError("continue used outside of loop at " +
PositionAsString(stmt->pos));
ReportError("continue used outside of loop");
}
GenerateLabelGoto(continue_label);
return GetTypeOracle().GetNeverType();
......@@ -961,20 +930,18 @@ const Type* ImplementationVisitor::Visit(ForLoopStatement* stmt) {
Label* exit_label = nullptr;
{
Declarations::NodeScopeActivator scope(declarations(), stmt->test);
body_label = declarations()->LookupLabel(stmt->pos, kTrueLabelName);
body_label = declarations()->LookupLabel(kTrueLabelName);
GenerateLabelDefinition(body_label);
exit_label = declarations()->LookupLabel(stmt->pos, kFalseLabelName);
exit_label = declarations()->LookupLabel(kFalseLabelName);
GenerateLabelDefinition(exit_label);
}
Label* header_label =
declarations()->DeclarePrivateLabel(stmt->pos, "header");
Label* header_label = declarations()->DeclarePrivateLabel("header");
GenerateLabelDefinition(header_label, stmt);
GenerateLabelGoto(header_label);
GenerateLabelBind(header_label);
Label* assignment_label =
declarations()->DeclarePrivateLabel(stmt->pos, "assignment");
Label* assignment_label = declarations()->DeclarePrivateLabel("assignment");
GenerateLabelDefinition(assignment_label);
BreakContinueActivator activator(global_context_, exit_label,
......@@ -1029,8 +996,7 @@ void ImplementationVisitor::GenerateIndent() {
}
void ImplementationVisitor::GenerateMacroFunctionDeclaration(
std::ostream& o, SourcePosition pos, const std::string& macro_prefix,
Macro* macro) {
std::ostream& o, const std::string& macro_prefix, Macro* macro) {
if (global_context_.verbose()) {
std::cout << "generating source for declaration " << *macro << ""
<< std::endl;
......@@ -1054,7 +1020,7 @@ void ImplementationVisitor::GenerateMacroFunctionDeclaration(
if (!first) {
o << ", ";
}
const Value* parameter = declarations()->LookupValue(pos, name);
const Value* parameter = declarations()->LookupValue(name);
const Type* parameter_type = *type_iterator;
const std::string& generated_type_name =
parameter_type->GetGeneratedTypeName();
......@@ -1064,7 +1030,7 @@ void ImplementationVisitor::GenerateMacroFunctionDeclaration(
}
for (const LabelDeclaration& label_info : macro->signature().labels) {
Label* label = declarations()->LookupLabel(pos, label_info.name);
Label* label = declarations()->LookupLabel(label_info.name);
if (!first) {
o << ", ";
}
......@@ -1082,7 +1048,7 @@ void ImplementationVisitor::GenerateMacroFunctionDeclaration(
}
VisitResult ImplementationVisitor::GenerateOperation(
SourcePosition pos, const std::string& operation, Arguments arguments,
const std::string& operation, Arguments arguments,
base::Optional<const Type*> return_type) {
TypeVector parameter_types(arguments.parameters.GetTypeVector());
......@@ -1095,25 +1061,23 @@ VisitResult ImplementationVisitor::GenerateOperation(
// return but have a True and False label
if (!return_type && handler.result_type->IsNever()) {
if (arguments.labels.size() == 0) {
Label* true_label =
declarations()->LookupLabel(pos, kTrueLabelName);
Label* true_label = declarations()->LookupLabel(kTrueLabelName);
arguments.labels.push_back(true_label);
Label* false_label =
declarations()->LookupLabel(pos, kFalseLabelName);
Label* false_label = declarations()->LookupLabel(kFalseLabelName);
arguments.labels.push_back(false_label);
}
}
if (!return_type || (GetTypeOracle().IsAssignableFrom(
*return_type, handler.result_type))) {
return GenerateCall(pos, handler.macro_name, arguments, false);
return GenerateCall(handler.macro_name, arguments, false);
}
}
}
}
std::stringstream s;
s << "cannot find implementation of operation \"" << operation
<< "\" with types " << parameter_types << " at " << PositionAsString(pos);
<< "\" with types " << parameter_types;
ReportError(s.str());
return VisitResult(GetTypeOracle().GetVoidType(), "");
}
......@@ -1136,8 +1100,7 @@ void ImplementationVisitor::GenerateChangedVarsFromControlSplit(AstNode* node) {
source_out() << "}";
}
const Type* ImplementationVisitor::GetCommonType(SourcePosition pos,
const Type* left,
const Type* ImplementationVisitor::GetCommonType(const Type* left,
const Type* right) {
const Type* common_type = GetTypeOracle().GetVoidType();
if (GetTypeOracle().IsAssignableFrom(left, right)) {
......@@ -1146,8 +1109,7 @@ const Type* ImplementationVisitor::GetCommonType(SourcePosition pos,
common_type = right;
} else {
std::stringstream s;
s << "illegal combination of types " << left << " and " << right << " at "
<< PositionAsString(pos);
s << "illegal combination of types " << left << " and " << right;
ReportError(s.str());
}
return common_type;
......@@ -1194,10 +1156,9 @@ VisitResult ImplementationVisitor::GenerateFetchFromLocation(
}
}
void ImplementationVisitor::GenerateAssignToVariable(SourcePosition pos,
Variable* var,
void ImplementationVisitor::GenerateAssignToVariable(Variable* var,
VisitResult value) {
VisitResult casted_value = GenerateImplicitConvert(pos, var->type(), value);
VisitResult casted_value = GenerateImplicitConvert(var->type(), value);
GenerateIndent();
source_out() << var->GetValueForWrite() << " = " << casted_value.variable()
<< ";" << std::endl;
......@@ -1212,20 +1173,17 @@ void ImplementationVisitor::GenerateAssignToLocation(
if (value->IsConst()) {
std::stringstream s;
s << "\"" << value->name()
<< "\" is declared const (maybe implicitly) and cannot be assigned to "
"at "
<< PositionAsString(location->pos);
<< "\" is declared const (maybe implicitly) and cannot be assigned to";
ReportError(s.str());
}
Variable* var = Variable::cast(value);
GenerateAssignToVariable(location->pos, var, assignment_value);
GenerateAssignToVariable(var, assignment_value);
} else if (auto access = FieldAccessExpression::cast(location)) {
GenerateOperation(access->pos, std::string(".") + access->field + "=",
GenerateOperation(std::string(".") + access->field + "=",
{{reference.base, assignment_value}, {}});
} else {
DCHECK_NOT_NULL(ElementAccessExpression::cast(location));
GenerateOperation(
location->pos,
"[]=", {{reference.base, reference.index, assignment_value}, {}});
}
}
......@@ -1234,13 +1192,12 @@ Variable* ImplementationVisitor::GenerateVariableDeclaration(
AstNode* node, const std::string& name,
const base::Optional<const Type*>& type,
const base::Optional<VisitResult>& initialization) {
SourcePosition pos = node->pos;
Variable* variable = nullptr;
if (declarations()->Lookup(name)) {
variable = Variable::cast(declarations()->LookupValue(pos, name));
if (declarations()->TryLookup(name)) {
variable = Variable::cast(declarations()->LookupValue(name));
} else {
variable = declarations()->DeclareVariable(pos, name, *type);
variable = declarations()->DeclareVariable(name, *type);
// Because the variable is being defined during code generation, it must be
// assumed that it changes along all control split paths because it's no
// longer possible to run the control-flow anlaysis in the declaration pass
......@@ -1268,14 +1225,14 @@ Variable* ImplementationVisitor::GenerateVariableDeclaration(
source_out() << "USE(" << variable->GetValueForDeclaration() << ");"
<< std::endl;
if (initialization) {
GenerateAssignToVariable(pos, variable, *initialization);
GenerateAssignToVariable(variable, *initialization);
}
return variable;
}
void ImplementationVisitor::GenerateParameter(
SourcePosition pos, const std::string& parameter_name) {
const Value* val = declarations()->LookupValue(pos, parameter_name);
const std::string& parameter_name) {
const Value* val = declarations()->LookupValue(parameter_name);
std::string var = val->GetValueForDeclaration();
GenerateIndent();
source_out() << val->type()->GetGeneratedTypeName() << " " << var << " = ";
......@@ -1287,12 +1244,11 @@ void ImplementationVisitor::GenerateParameter(
source_out() << "USE(" << var << ");" << std::endl;
}
void ImplementationVisitor::GenerateParameterList(SourcePosition pos,
const NameVector& list,
void ImplementationVisitor::GenerateParameterList(const NameVector& list,
size_t first) {
for (auto p : list) {
if (first == 0) {
GenerateParameter(pos, p);
GenerateParameter(p);
} else {
first--;
}
......@@ -1300,14 +1256,13 @@ void ImplementationVisitor::GenerateParameterList(SourcePosition pos,
}
VisitResult ImplementationVisitor::GeneratePointerCall(
SourcePosition pos, Expression* callee, const Arguments& arguments,
bool is_tailcall) {
Expression* callee, const Arguments& arguments, bool is_tailcall) {
TypeVector parameter_types(arguments.parameters.GetTypeVector());
VisitResult callee_result = Visit(callee);
if (!callee_result.type()->IsFunctionPointerType()) {
std::stringstream stream;
stream << "Expected a function pointer type but found "
<< callee_result.type() << " at " << PositionAsString(pos);
<< callee_result.type();
ReportError(stream.str());
}
const FunctionPointerType* type =
......@@ -1317,7 +1272,7 @@ VisitResult ImplementationVisitor::GeneratePointerCall(
for (size_t current = 0; current < arguments.parameters.size(); ++current) {
const Type* to_type = type->parameter_types()[current];
VisitResult result =
GenerateImplicitConvert(pos, to_type, arguments.parameters[current]);
GenerateImplicitConvert(to_type, arguments.parameters[current]);
variables.push_back(result.variable());
}
......@@ -1359,10 +1314,10 @@ VisitResult ImplementationVisitor::GeneratePointerCall(
}
VisitResult ImplementationVisitor::GenerateCall(
SourcePosition pos, const std::string& callable_name,
const Arguments& arguments, bool is_tailcall) {
const std::string& callable_name, const Arguments& arguments,
bool is_tailcall) {
TypeVector parameter_types(arguments.parameters.GetTypeVector());
Callable* callable = LookupCall(pos, callable_name, parameter_types);
Callable* callable = LookupCall(callable_name, parameter_types);
const Type* result_type = callable->signature().return_type;
std::vector<std::string> variables;
......@@ -1371,7 +1326,7 @@ VisitResult ImplementationVisitor::GenerateCall(
? GetTypeOracle().GetObjectType()
: callable->signature().types()[current];
VisitResult result =
GenerateImplicitConvert(pos, to_type, arguments.parameters[current]);
GenerateImplicitConvert(to_type, arguments.parameters[current]);
variables.push_back(result.variable());
}
......@@ -1395,7 +1350,7 @@ VisitResult ImplementationVisitor::GenerateCall(
} else if (callable->IsMacro()) {
if (is_tailcall) {
std::stringstream stream;
stream << "can't tail call a macro at " << PositionAsString(pos);
stream << "can't tail call a macro";
ReportError(stream.str());
}
source_out() << callable->name() << "(";
......@@ -1409,8 +1364,7 @@ VisitResult ImplementationVisitor::GenerateCall(
UNREACHABLE();
}
if (global_context_.verbose()) {
std::cout << "generating code for call to " << callable_name << " at "
<< PositionAsString(pos) << "" << std::endl;
std::cout << "generating code for call to " << callable_name << "\n";
}
size_t total_parameters = 0;
......@@ -1426,8 +1380,7 @@ VisitResult ImplementationVisitor::GenerateCall(
std::stringstream s;
s << "unexpected number of otherwise labels for " << callable->name()
<< " (expected " << std::to_string(label_count) << " found "
<< std::to_string(arguments.labels.size()) << ") at "
<< PositionAsString(pos);
<< std::to_string(arguments.labels.size()) << ")";
ReportError(s.str());
}
for (size_t i = 0; i < label_count; ++i) {
......@@ -1442,8 +1395,7 @@ VisitResult ImplementationVisitor::GenerateCall(
s << "label " << label->name()
<< " doesn't have the right number of parameters (found "
<< std::to_string(label->GetParameterCount()) << " expected "
<< std::to_string(callee_label_parameters) << ") at "
<< PositionAsString(pos);
<< std::to_string(callee_label_parameters) << ")";
ReportError(s.str());
}
source_out() << label->generated();
......@@ -1455,7 +1407,7 @@ VisitResult ImplementationVisitor::GenerateCall(
std::stringstream s;
s << "mismatch of label parameters (expected " << t << " got "
<< label->GetParameter(j)->type() << " for parameter "
<< std::to_string(i + 1) << ") at " << PositionAsString(pos);
<< std::to_string(i + 1) << ")";
ReportError(s.str());
}
j++;
......@@ -1466,7 +1418,7 @@ VisitResult ImplementationVisitor::GenerateCall(
if (global_context_.verbose()) {
std::cout << "finished generating code for call to " << callable_name
<< " at " << PositionAsString(pos) << "" << std::endl;
<< "\n";
}
if (!result_type->IsVoidOrNever() && !is_tailcall &&
!result_type->IsConstexpr()) {
......@@ -1481,9 +1433,8 @@ void ImplementationVisitor::Visit(StandardDeclaration* decl) {
}
void ImplementationVisitor::Visit(SpecializationDeclaration* decl) {
Generic* generic = declarations()->LookupGeneric(decl->pos, decl->name);
TypeVector specialization_types =
GetTypeVector(decl->pos, decl->generic_parameters);
Generic* generic = declarations()->LookupGeneric(decl->name);
TypeVector specialization_types = GetTypeVector(decl->generic_parameters);
CallableNode* callable = generic->declaration()->callable;
QueueGenericSpecialization({generic, specialization_types}, callable,
decl->signature.get(), decl->body);
......@@ -1495,10 +1446,8 @@ VisitResult ImplementationVisitor::Visit(CallExpression* expr,
std::string name = expr->callee.name;
bool has_template_arguments = expr->generic_arguments.size() != 0;
if (has_template_arguments) {
Generic* generic =
declarations()->LookupGeneric(expr->pos, expr->callee.name);
TypeVector specialization_types =
GetTypeVector(expr->pos, expr->generic_arguments);
Generic* generic = declarations()->LookupGeneric(expr->callee.name);
TypeVector specialization_types = GetTypeVector(expr->generic_arguments);
name = GetGeneratedCallableName(name, specialization_types);
CallableNode* callable = generic->declaration()->callable;
QueueGenericSpecialization({generic, specialization_types}, callable,
......@@ -1507,22 +1456,21 @@ VisitResult ImplementationVisitor::Visit(CallExpression* expr,
}
for (Expression* arg : expr->arguments)
arguments.parameters.push_back(Visit(arg));
arguments.labels = LabelsFromIdentifiers(expr->pos, expr->labels);
arguments.labels = LabelsFromIdentifiers(expr->labels);
if (expr->is_operator) {
if (is_tailcall) {
std::stringstream s;
s << "can't tail call an operator" << PositionAsString(expr->pos);
s << "can't tail call an operator";
ReportError(s.str());
}
return GenerateOperation(expr->pos, name, arguments);
return GenerateOperation(name, arguments);
}
VisitResult result;
if (!has_template_arguments &&
declarations()->Lookup(expr->pos, expr->callee.name)->IsValue()) {
result =
GeneratePointerCall(expr->pos, &expr->callee, arguments, is_tailcall);
declarations()->Lookup(expr->callee.name)->IsValue()) {
result = GeneratePointerCall(&expr->callee, arguments, is_tailcall);
} else {
result = GenerateCall(expr->pos, name, arguments, is_tailcall);
result = GenerateCall(name, arguments, is_tailcall);
}
if (!result.type()->IsVoidOrNever()) {
GenerateIndent();
......@@ -1576,7 +1524,7 @@ bool ImplementationVisitor::GenerateExpressionBranch(
if (expression_result.type() != GetTypeOracle().GetNeverType()) {
std::stringstream s;
s << "unexpected return type " << expression_result.type()
<< " for branch expression at " << PositionAsString(expression->pos);
<< " for branch expression";
ReportError(s.str());
}
}
......@@ -1586,7 +1534,7 @@ bool ImplementationVisitor::GenerateExpressionBranch(
}
VisitResult ImplementationVisitor::GenerateImplicitConvert(
SourcePosition pos, const Type* destination_type, VisitResult source) {
const Type* destination_type, VisitResult source) {
if (destination_type == source.type()) {
return source;
}
......@@ -1595,15 +1543,14 @@ VisitResult ImplementationVisitor::GenerateImplicitConvert(
VisitResult result(source.type(), source.variable());
Arguments args;
args.parameters = {result};
return GenerateOperation(pos, "convert<>", args, destination_type);
return GenerateOperation("convert<>", args, destination_type);
} else if (GetTypeOracle().IsAssignableFrom(destination_type,
source.type())) {
return VisitResult(destination_type, source.variable());
} else {
std::stringstream s;
s << "cannot use expression of type " << source.type()
<< " as a value of type " << destination_type << " at "
<< PositionAsString(pos);
<< " as a value of type " << destination_type;
ReportError(s.str());
}
return VisitResult(GetTypeOracle().GetVoidType(), "");
......@@ -1651,10 +1598,10 @@ void ImplementationVisitor::GenerateLabelGoto(Label* label) {
}
std::vector<Label*> ImplementationVisitor::LabelsFromIdentifiers(
SourcePosition pos, const std::vector<std::string>& names) {
const std::vector<std::string>& names) {
std::vector<Label*> result;
for (auto name : names) {
result.push_back(declarations()->LookupLabel(pos, name));
result.push_back(declarations()->LookupLabel(name));
}
return result;
}
......
......@@ -39,8 +39,7 @@ class ImplementationVisitor : public FileVisitor {
LocationReference GetLocationReference(LocationExpression* location);
LocationReference GetLocationReference(IdentifierExpression* expr) {
return LocationReference(declarations()->LookupValue(expr->pos, expr->name),
{}, {});
return LocationReference(declarations()->LookupValue(expr->name), {}, {});
}
LocationReference GetLocationReference(FieldAccessExpression* expr) {
return LocationReference({}, Visit(expr->object), {});
......@@ -56,8 +55,7 @@ class ImplementationVisitor : public FileVisitor {
Value* value = reference.value;
if (value->IsVariable() && !Variable::cast(value)->IsDefined()) {
std::stringstream s;
s << "\"" << value->name() << "\" is used before it is defined at "
<< PositionAsString(expr->pos);
s << "\"" << value->name() << "\" is used before it is defined";
ReportError(s.str());
}
return VisitResult({value->type(), value->GetValueForRead()});
......@@ -66,22 +64,21 @@ class ImplementationVisitor : public FileVisitor {
LocationReference reference) {
Arguments arguments;
arguments.parameters = {reference.base};
return GenerateOperation(expr->pos, std::string(".") + expr->field,
arguments);
return GenerateOperation(std::string(".") + expr->field, arguments);
}
VisitResult GenerateFetchFromLocation(ElementAccessExpression* expr,
LocationReference reference) {
Arguments arguments;
arguments.parameters = {reference.base, reference.index};
return GenerateOperation(expr->pos, "[]", arguments);
return GenerateOperation("[]", arguments);
}
VisitResult GetBuiltinCode(SourcePosition pos, Builtin* builtin);
VisitResult GetBuiltinCode(Builtin* builtin);
VisitResult Visit(IdentifierExpression* expr) {
if (Builtin* builtin =
Builtin::DynamicCast(declarations()->Lookup(expr->name))) {
return GetBuiltinCode(expr->pos, builtin);
return GetBuiltinCode(builtin);
}
return GenerateFetchFromLocation(expr, GetLocationReference(expr));
}
......@@ -183,13 +180,11 @@ class ImplementationVisitor : public FileVisitor {
void GenerateChangedVarsFromControlSplit(AstNode* node);
const Type* GetCommonType(SourcePosition pos, const Type* left,
const Type* right);
const Type* GetCommonType(const Type* left, const Type* right);
VisitResult GenerateCopy(const VisitResult& to_copy);
void GenerateAssignToVariable(SourcePosition pos, Variable* var,
VisitResult value);
void GenerateAssignToVariable(Variable* var, VisitResult value);
void GenerateAssignToLocation(LocationExpression* location,
const LocationReference& reference,
......@@ -200,14 +195,13 @@ class ImplementationVisitor : public FileVisitor {
const base::Optional<const Type*>& type,
const base::Optional<VisitResult>& initialization = {});
void GenerateParameter(SourcePosition pos, const std::string& parameter_name);
void GenerateParameter(const std::string& parameter_name);
void GenerateParameterList(SourcePosition pos, const NameVector& list,
size_t first = 0);
void GenerateParameterList(const NameVector& list, size_t first = 0);
VisitResult GenerateCall(SourcePosition pos, const std::string& callable_name,
VisitResult GenerateCall(const std::string& callable_name,
const Arguments& parameters, bool tail_call);
VisitResult GeneratePointerCall(SourcePosition pos, Expression* callee,
VisitResult GeneratePointerCall(Expression* callee,
const Arguments& parameters, bool tail_call);
bool GenerateLabeledStatementBlocks(
......@@ -222,17 +216,15 @@ class ImplementationVisitor : public FileVisitor {
const std::vector<Statement*>& statement_blocks,
Label* merge_label);
void GenerateMacroFunctionDeclaration(std::ostream& o, SourcePosition pos,
void GenerateMacroFunctionDeclaration(std::ostream& o,
const std::string& macro_prefix,
Macro* macro);
VisitResult GenerateOperation(SourcePosition pos,
const std::string& operation,
VisitResult GenerateOperation(const std::string& operation,
Arguments arguments,
base::Optional<const Type*> return_type = {});
VisitResult GenerateImplicitConvert(SourcePosition pos,
const Type* destination_type,
VisitResult GenerateImplicitConvert(const Type* destination_type,
VisitResult source);
void Specialize(const SpecializationKey& key, CallableNode* callable,
......@@ -253,7 +245,7 @@ class ImplementationVisitor : public FileVisitor {
void GenerateLabelGoto(Label* label);
std::vector<Label*> LabelsFromIdentifiers(
SourcePosition pos, const std::vector<std::string>& names);
const std::vector<std::string>& names);
std::ostream& source_out() { return module_->source_stream(); }
......
......@@ -46,6 +46,7 @@ int WrappedMain(int argc, const char** argv) {
size_t lexer_errors = 0;
auto error_strategy = std::make_shared<FailedParseErrorStrategy>();
bool verbose = false;
SourceFileMap::Scope scope;
for (int i = 1; i < argc; ++i) {
// Check for options
if (!strcmp("-o", argv[i])) {
......
......@@ -7,14 +7,19 @@
#include <iostream>
#include <string>
#include "src/torque/ast.h"
#include "src/torque/utils.h"
namespace v8 {
namespace internal {
namespace torque {
std::string CurrentPositionAsString() {
return PositionAsString(CurrentSourcePosition::Get());
}
void ReportError(const std::string& error) {
std::cerr << error << std::endl;
std::cerr << CurrentPositionAsString() << ": Torque error: " << error << "\n";
throw(-1);
}
......
......@@ -25,6 +25,8 @@ std::string DashifyString(const std::string& underscore_string);
void ReplaceFileContentsIfDifferent(const std::string& file_path,
const std::string& contents);
std::string CurrentPositionAsString();
template <class T>
class Deduplicator {
public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment