Commit 9f4e7484 authored by Tobias Tebbi's avatar Tobias Tebbi Committed by Commit Bot

[torque] cleanup: make TypeOracle contextual

In Torque, we have several global singleton classes. Using the contextual
variable pattern instead of passing around pointers in random places
makes the code more readable.
This CL does this for TypeOracle, we plan to do it for more classes in
the future.

Bug: v8:7754
Change-Id: Ib744b476ce51a4932c52274b2210149515f1663d
Reviewed-on: https://chromium-review.googlesource.com/1078729
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53444}
parent 38ae84f4
...@@ -34,8 +34,9 @@ class ContextualVariable { ...@@ -34,8 +34,9 @@ class ContextualVariable {
// any older scope is destructed. // any older scope is destructed.
class Scope { class Scope {
public: public:
explicit Scope(VarType x = VarType()) template <class... Args>
: current_(std::move(x)), previous_(top_) { explicit Scope(Args&&... args)
: current_(std::forward<Args>(args)...), previous_(top_) {
top_ = &current_; top_ = &current_;
} }
~Scope() { ~Scope() {
......
...@@ -151,8 +151,8 @@ void DeclarationVisitor::Visit(ExternalMacroDeclaration* decl, ...@@ -151,8 +151,8 @@ void DeclarationVisitor::Visit(ExternalMacroDeclaration* decl,
ReportError( ReportError(
"implicit cast operators doesn't only have a single parameter"); "implicit cast operators doesn't only have a single parameter");
} }
GetTypeOracle().RegisterImplicitConversion(signature.return_type, TypeOracle::RegisterImplicitConversion(signature.return_type,
parameter_types[0]); parameter_types[0]);
} }
} }
...@@ -164,7 +164,7 @@ void DeclarationVisitor::Visit(TorqueBuiltinDeclaration* decl, ...@@ -164,7 +164,7 @@ void DeclarationVisitor::Visit(TorqueBuiltinDeclaration* decl,
if (signature.parameter_types.var_args) { if (signature.parameter_types.var_args) {
declarations()->DeclareConstant( declarations()->DeclareConstant(
decl->signature->parameters.arguments_variable, decl->signature->parameters.arguments_variable,
GetTypeOracle().GetArgumentsType(), "arguments"); TypeOracle::GetArgumentsType(), "arguments");
} }
torque_builtins_.push_back(builtin); torque_builtins_.push_back(builtin);
Visit(body); Visit(body);
......
...@@ -46,8 +46,8 @@ Callable* FileVisitor::LookupCall(const std::string& name, ...@@ -46,8 +46,8 @@ Callable* FileVisitor::LookupCall(const std::string& name,
result = RuntimeFunction::cast(declarable); result = RuntimeFunction::cast(declarable);
} else if (declarable->IsMacroList()) { } 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, if (IsCompatibleSignature(m->signature().parameter_types,
parameter_types)) { parameter_types)) {
if (result != nullptr) { if (result != nullptr) {
std::stringstream stream; std::stringstream stream;
stream << "multiple matching matching parameter list for macro " stream << "multiple matching matching parameter list for macro "
......
...@@ -62,7 +62,6 @@ class FileVisitor { ...@@ -62,7 +62,6 @@ class FileVisitor {
Module* CurrentModule() const { return module_; } Module* CurrentModule() const { return module_; }
friend class ScopedModuleActivator; friend class ScopedModuleActivator;
TypeOracle& GetTypeOracle() { return global_context_.GetTypeOracle(); }
std::string GetParameterVariableFromName(const std::string& name) { std::string GetParameterVariableFromName(const std::string& name) {
return std::string("p_") + name; return std::string("p_") + name;
......
...@@ -56,7 +56,6 @@ class GlobalContext { ...@@ -56,7 +56,6 @@ class GlobalContext {
explicit GlobalContext(Ast ast) explicit GlobalContext(Ast ast)
: verbose_(false), : verbose_(false),
next_label_number_(0), next_label_number_(0),
type_oracle_(&declarations_),
default_module_(GetModule("base")), default_module_(GetModule("base")),
ast_(std::move(ast)) {} ast_(std::move(ast)) {}
Module* GetDefaultModule() { return default_module_; } Module* GetDefaultModule() { return default_module_; }
...@@ -104,8 +103,6 @@ class GlobalContext { ...@@ -104,8 +103,6 @@ class GlobalContext {
friend class CurrentCallableActivator; friend class CurrentCallableActivator;
friend class BreakContinueActivator; friend class BreakContinueActivator;
TypeOracle& GetTypeOracle() { return type_oracle_; }
Callable* GetCurrentCallable() const { return current_callable_; } Callable* GetCurrentCallable() const { return current_callable_; }
Label* GetCurrentBreak() const { return break_continue_stack_.back().first; } Label* GetCurrentBreak() const { return break_continue_stack_.back().first; }
Label* GetCurrentContinue() const { Label* GetCurrentContinue() const {
...@@ -123,7 +120,6 @@ class GlobalContext { ...@@ -123,7 +120,6 @@ class GlobalContext {
Declarations declarations_; Declarations declarations_;
Callable* current_callable_; Callable* current_callable_;
std::vector<std::pair<Label*, Label*>> break_continue_stack_; std::vector<std::pair<Label*, Label*>> break_continue_stack_;
TypeOracle type_oracle_;
std::map<std::string, std::unique_ptr<Module>> modules_; std::map<std::string, std::unique_ptr<Module>> modules_;
Module* default_module_; Module* default_module_;
std::map<std::pair<const AstNode*, TypeVector>, std::set<const Variable*>> std::map<std::pair<const AstNode*, TypeVector>, std::set<const Variable*>>
......
This diff is collapsed.
...@@ -101,6 +101,7 @@ int WrappedMain(int argc, const char** argv) { ...@@ -101,6 +101,7 @@ int WrappedMain(int argc, const char** argv) {
GlobalContext global_context(std::move(ast_generator).GetAst()); GlobalContext global_context(std::move(ast_generator).GetAst());
if (verbose) global_context.SetVerbose(); if (verbose) global_context.SetVerbose();
TypeOracle::Scope type_oracle(global_context.declarations());
if (output_directory.length() != 0) { if (output_directory.length() != 0) {
{ {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef V8_TORQUE_TYPE_ORACLE_H_ #ifndef V8_TORQUE_TYPE_ORACLE_H_
#define V8_TORQUE_TYPE_ORACLE_H_ #define V8_TORQUE_TYPE_ORACLE_H_
#include "src/torque/contextual.h"
#include "src/torque/declarable.h" #include "src/torque/declarable.h"
#include "src/torque/declarations.h" #include "src/torque/declarations.h"
#include "src/torque/types.h" #include "src/torque/types.h"
...@@ -14,47 +15,53 @@ namespace v8 { ...@@ -14,47 +15,53 @@ namespace v8 {
namespace internal { namespace internal {
namespace torque { namespace torque {
class TypeOracle { class TypeOracle : public ContextualClass<TypeOracle> {
public: public:
explicit TypeOracle(Declarations* declarations) explicit TypeOracle(Declarations* declarations)
: declarations_(declarations) {} : declarations_(declarations) {}
void RegisterImplicitConversion(const Type* to, const Type* from) { static void RegisterImplicitConversion(const Type* to, const Type* from) {
implicit_conversions_.push_back(std::make_pair(to, from)); Get().implicit_conversions_.push_back(std::make_pair(to, from));
} }
const Type* GetArgumentsType() { static const Type* GetArgumentsType() {
return GetBuiltinType(ARGUMENTS_TYPE_STRING); return Get().GetBuiltinType(ARGUMENTS_TYPE_STRING);
} }
const Type* GetBoolType() { return GetBuiltinType(BOOL_TYPE_STRING); } static const Type* GetBoolType() {
return Get().GetBuiltinType(BOOL_TYPE_STRING);
const Type* GetConstexprBoolType() {
return GetBuiltinType(CONSTEXPR_BOOL_TYPE_STRING);
} }
const Type* GetVoidType() { return GetBuiltinType(VOID_TYPE_STRING); } static const Type* GetConstexprBoolType() {
return Get().GetBuiltinType(CONSTEXPR_BOOL_TYPE_STRING);
}
const Type* GetObjectType() { return GetBuiltinType(OBJECT_TYPE_STRING); } static const Type* GetVoidType() {
return Get().GetBuiltinType(VOID_TYPE_STRING);
}
const Type* GetStringType() { return GetBuiltinType(STRING_TYPE_STRING); } static const Type* GetObjectType() {
return Get().GetBuiltinType(OBJECT_TYPE_STRING);
}
const Type* GetIntPtrType() { return GetBuiltinType(INTPTR_TYPE_STRING); } static const Type* GetStringType() {
return Get().GetBuiltinType(STRING_TYPE_STRING);
}
const Type* GetNeverType() { return GetBuiltinType(NEVER_TYPE_STRING); } static const Type* GetIntPtrType() {
return Get().GetBuiltinType(INTPTR_TYPE_STRING);
}
const Type* GetConstInt31Type() { static const Type* GetNeverType() {
return GetBuiltinType(CONST_INT31_TYPE_STRING); return Get().GetBuiltinType(NEVER_TYPE_STRING);
} }
bool IsAssignableFrom(const Type* to, const Type* from) { static const Type* GetConstInt31Type() {
if (to == from) return true; return Get().GetBuiltinType(CONST_INT31_TYPE_STRING);
if (from->IsSubtypeOf(to) && !from->IsConstexpr()) return true;
return IsImplicitlyConverableFrom(to, from);
} }
bool IsImplicitlyConverableFrom(const Type* to, const Type* from) { static bool IsImplicitlyConverableFrom(const Type* to, const Type* from) {
for (auto& conversion : implicit_conversions_) { for (auto& conversion : Get().implicit_conversions_) {
if (conversion.first == to && conversion.second == from) { if (conversion.first == to && conversion.second == from) {
return true; return true;
} }
...@@ -62,19 +69,6 @@ class TypeOracle { ...@@ -62,19 +69,6 @@ class TypeOracle {
return false; return false;
} }
bool IsCompatibleSignature(const ParameterTypes& to, const TypeVector& from) {
auto i = to.types.begin();
for (auto current : from) {
if (i == to.types.end()) {
if (!to.var_args) return false;
if (!IsAssignableFrom(GetObjectType(), current)) return false;
} else {
if (!IsAssignableFrom(*i++, current)) return false;
}
}
return true;
}
private: private:
const Type* GetBuiltinType(const std::string& name) { const Type* GetBuiltinType(const std::string& name) {
return declarations_->LookupGlobalType(name); return declarations_->LookupGlobalType(name);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <iostream> #include <iostream>
#include "src/torque/declarable.h" #include "src/torque/declarable.h"
#include "src/torque/type-oracle.h"
#include "src/torque/types.h" #include "src/torque/types.h"
namespace v8 { namespace v8 {
...@@ -203,6 +204,25 @@ bool Signature::HasSameTypesAs(const Signature& other) const { ...@@ -203,6 +204,25 @@ bool Signature::HasSameTypesAs(const Signature& other) const {
return true; return true;
} }
bool IsAssignableFrom(const Type* to, const Type* from) {
if (to == from) return true;
if (from->IsSubtypeOf(to) && !from->IsConstexpr()) return true;
return TypeOracle::IsImplicitlyConverableFrom(to, from);
}
bool IsCompatibleSignature(const ParameterTypes& to, const TypeVector& from) {
auto i = to.types.begin();
for (auto current : from) {
if (i == to.types.end()) {
if (!to.var_args) return false;
if (!IsAssignableFrom(TypeOracle::GetObjectType(), current)) return false;
} else {
if (!IsAssignableFrom(*i++, current)) return false;
}
}
return true;
}
} // namespace torque } // namespace torque
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -338,6 +338,9 @@ struct Arguments { ...@@ -338,6 +338,9 @@ struct Arguments {
std::ostream& operator<<(std::ostream& os, const Signature& sig); std::ostream& operator<<(std::ostream& os, const Signature& sig);
bool IsAssignableFrom(const Type* to, const Type* from);
bool IsCompatibleSignature(const ParameterTypes& to, const TypeVector& from);
} // namespace torque } // namespace torque
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
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