Commit 7b3f609b authored by Tobias Tebbi's avatar Tobias Tebbi Committed by Commit Bot

[torque] allow qualified access to structs

Bug: v8:7793
Change-Id: I4ce0008f56976102bad952ef2389f40845dcc15b
Reviewed-on: https://chromium-review.googlesource.com/c/1340255Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57605}
parent b1468242
......@@ -1253,16 +1253,6 @@ extern macro TryIntPtrAdd(intptr, intptr): intptr
extern builtin ObjectToString(Context, Object): Object;
extern builtin StringRepeat(Context, String, Number): String;
// Returned from IteratorBuiltinsAssembler::GetIterator(). Struct is declared
// here to simplify use in other generated builtins.
struct IteratorRecord {
// iteratorRecord.[[Iterator]]
object: JSReceiver;
// iteratorRecord.[[NextMethod]]
next: Object;
}
struct KeyValuePair {
key: Object;
value: Object;
......
......@@ -18,6 +18,7 @@ namespace v8 {
namespace internal {
using Node = compiler::Node;
using IteratorRecord = IteratorBuiltinsFromDSLAssembler::IteratorRecord;
ArrayBuiltinsAssembler::ArrayBuiltinsAssembler(
compiler::CodeAssemblerState* state)
......
......@@ -312,7 +312,7 @@ void BaseCollectionsAssembler::AddConstructorEntriesFromIterable(
TNode<Object> add_func = GetAddFunction(variant, context, collection);
IteratorBuiltinsAssembler iterator_assembler(this->state());
BaseBuiltinsFromDSLAssembler::IteratorRecord iterator =
IteratorBuiltinsFromDSLAssembler::IteratorRecord iterator =
iterator_assembler.GetIterator(context, iterable);
CSA_ASSERT(this, Word32BinaryNot(IsUndefined(iterator.object)));
......
......@@ -16,7 +16,7 @@
namespace v8 {
namespace internal {
typedef BaseBuiltinsFromDSLAssembler::IteratorRecord IteratorRecord;
typedef IteratorBuiltinsFromDSLAssembler::IteratorRecord IteratorRecord;
using compiler::Node;
......
......@@ -7,6 +7,7 @@
#include "src/code-stub-assembler.h"
#include "torque-generated/builtins-base-from-dsl-gen.h"
#include "torque-generated/builtins-iterator-from-dsl-gen.h"
namespace v8 {
namespace internal {
......@@ -23,10 +24,10 @@ class IteratorBuiltinsAssembler : public CodeStubAssembler {
// https://tc39.github.io/ecma262/#sec-getiterator --- never used for
// @@asyncIterator.
BaseBuiltinsFromDSLAssembler::IteratorRecord GetIterator(
IteratorBuiltinsFromDSLAssembler::IteratorRecord GetIterator(
Node* context, Node* object, Label* if_exception = nullptr,
Variable* exception = nullptr);
BaseBuiltinsFromDSLAssembler::IteratorRecord GetIterator(
IteratorBuiltinsFromDSLAssembler::IteratorRecord GetIterator(
Node* context, Node* object, Node* method, Label* if_exception = nullptr,
Variable* exception = nullptr);
......@@ -37,13 +38,13 @@ class IteratorBuiltinsAssembler : public CodeStubAssembler {
// object, loaded from the native context.
TNode<Object> IteratorStep(
Node* context,
const BaseBuiltinsFromDSLAssembler::IteratorRecord& iterator,
const IteratorBuiltinsFromDSLAssembler::IteratorRecord& iterator,
Label* if_done, Node* fast_iterator_result_map = nullptr,
Label* if_exception = nullptr, Variable* exception = nullptr);
TNode<Object> IteratorStep(
Node* context,
const BaseBuiltinsFromDSLAssembler::IteratorRecord& iterator,
const IteratorBuiltinsFromDSLAssembler::IteratorRecord& iterator,
Node* fast_iterator_result_map, Label* if_done) {
return IteratorStep(context, iterator, if_done, fast_iterator_result_map);
}
......@@ -60,11 +61,11 @@ class IteratorBuiltinsAssembler : public CodeStubAssembler {
// https://tc39.github.io/ecma262/#sec-iteratorclose
void IteratorCloseOnException(
Node* context,
const BaseBuiltinsFromDSLAssembler::IteratorRecord& iterator,
const IteratorBuiltinsFromDSLAssembler::IteratorRecord& iterator,
Label* if_exception, Variable* exception);
void IteratorCloseOnException(
Node* context,
const BaseBuiltinsFromDSLAssembler::IteratorRecord& iterator,
const IteratorBuiltinsFromDSLAssembler::IteratorRecord& iterator,
TNode<Object> exception);
// #sec-iterabletolist
......
......@@ -18,7 +18,7 @@ namespace v8 {
namespace internal {
using compiler::Node;
using IteratorRecord = BaseBuiltinsFromDSLAssembler::IteratorRecord;
using IteratorRecord = IteratorBuiltinsFromDSLAssembler::IteratorRecord;
Node* PromiseBuiltinsAssembler::AllocateJSPromise(Node* context) {
Node* const native_context = LoadNativeContext(context);
......
......@@ -9,6 +9,7 @@
#include "src/contexts.h"
#include "src/objects/promise.h"
#include "torque-generated/builtins-base-from-dsl-gen.h"
#include "torque-generated/builtins-iterator-from-dsl-gen.h"
namespace v8 {
namespace internal {
......@@ -183,7 +184,7 @@ class PromiseBuiltinsAssembler : public CodeStubAssembler {
Node* PerformPromiseAll(
Node* context, Node* constructor, Node* capability,
const BaseBuiltinsFromDSLAssembler::IteratorRecord& record,
const IteratorBuiltinsFromDSLAssembler::IteratorRecord& record,
Label* if_exception, Variable* var_exception);
void SetForwardingHandlerIfTrue(Node* context, Node* condition,
......
......@@ -3,6 +3,15 @@
// found in the LICENSE file.
namespace iterator {
// Returned from IteratorBuiltinsAssembler::GetIterator().
struct IteratorRecord {
// iteratorRecord.[[Iterator]]
object: JSReceiver;
// iteratorRecord.[[NextMethod]]
next: Object;
}
extern macro IteratorBuiltinsAssembler::GetIteratorMethod(
implicit context: Context)(Object): Object;
extern macro IteratorBuiltinsAssembler::GetIterator(
......
......@@ -217,11 +217,14 @@ struct CallExpression : Expression {
struct StructExpression : Expression {
DEFINE_AST_NODE_LEAF_BOILERPLATE(StructExpression)
StructExpression(SourcePosition pos, std::string name,
std::vector<Expression*> expressions)
StructExpression(SourcePosition pos,
std::vector<std::string> namespace_qualification,
std::string name, std::vector<Expression*> expressions)
: Expression(kKind, pos),
namespace_qualification(std::move(namespace_qualification)),
name(std::move(name)),
expressions(std::move(expressions)) {}
std::vector<std::string> namespace_qualification;
std::string name;
std::vector<Expression*> expressions;
};
......@@ -350,10 +353,14 @@ struct ParameterList {
struct BasicTypeExpression : TypeExpression {
DEFINE_AST_NODE_LEAF_BOILERPLATE(BasicTypeExpression)
BasicTypeExpression(SourcePosition pos, bool is_constexpr, std::string name)
BasicTypeExpression(SourcePosition pos,
std::vector<std::string> namespace_qualification,
bool is_constexpr, std::string name)
: TypeExpression(kKind, pos),
namespace_qualification(std::move(namespace_qualification)),
is_constexpr(is_constexpr),
name(std::move(name)) {}
std::vector<std::string> namespace_qualification;
bool is_constexpr;
std::string name;
};
......
......@@ -74,7 +74,8 @@ base::Optional<const Type*> Generic::InferTypeArgument(
for (size_t i = 0; i < arguments.size() && i < parameters.size(); ++i) {
BasicTypeExpression* basic =
BasicTypeExpression::DynamicCast(parameters[i]);
if (basic && !basic->is_constexpr && basic->name == type_name) {
if (basic && basic->namespace_qualification.empty() &&
!basic->is_constexpr && basic->name == type_name) {
return arguments[i];
}
}
......
......@@ -59,12 +59,16 @@ std::vector<Declarable*> Declarations::LookupGlobalScope(
return d;
}
const Type* Declarations::LookupType(const std::string& name) {
TypeAlias* declaration = EnsureUnique(
FilterDeclarables<TypeAlias>(Lookup(QualifiedName(name))), name, "type");
const Type* Declarations::LookupType(const QualifiedName& name) {
TypeAlias* declaration =
EnsureUnique(FilterDeclarables<TypeAlias>(Lookup(name)), name, "type");
return declaration->type();
}
const Type* Declarations::LookupType(std::string name) {
return LookupType(QualifiedName(std::move(name)));
}
const Type* Declarations::LookupGlobalType(const std::string& name) {
TypeAlias* declaration = EnsureUnique(
FilterDeclarables<TypeAlias>(LookupGlobalScope(name)), name, "type");
......@@ -75,7 +79,7 @@ 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(name);
return LookupType(QualifiedName{basic->namespace_qualification, name});
} else if (auto* union_type = UnionTypeExpression::cast(type_expression)) {
return TypeOracle::GetUnionType(GetType(union_type->a),
GetType(union_type->b));
......@@ -149,7 +153,7 @@ const AbstractType* Declarations::DeclareAbstractType(
CheckAlreadyDeclared<TypeAlias>(name, "type");
const Type* parent_type = nullptr;
if (parent) {
parent_type = LookupType(*parent);
parent_type = LookupType(QualifiedName{*parent});
}
const AbstractType* type = TypeOracle::GetAbstractType(
parent_type, name, transient, generated, non_constexpr_version);
......
......@@ -56,7 +56,8 @@ class Declarations {
static std::vector<Declarable*> LookupGlobalScope(const std::string& name);
static const Type* LookupType(const std::string& name);
static const Type* LookupType(const QualifiedName& name);
static const Type* LookupType(std::string name);
static const Type* LookupGlobalType(const std::string& name);
static const Type* GetType(TypeExpression* type_expression);
......
......@@ -1423,7 +1423,8 @@ VisitResult ImplementationVisitor::GenerateCopy(const VisitResult& to_copy) {
}
VisitResult ImplementationVisitor::Visit(StructExpression* decl) {
const Type* raw_type = Declarations::LookupType(decl->name);
const Type* raw_type = Declarations::LookupType(
QualifiedName(decl->namespace_qualification, decl->name));
if (!raw_type->IsStructType()) {
std::stringstream s;
s << decl->name << " is not a struct but used like one ";
......
......@@ -351,7 +351,8 @@ base::Optional<ParseResult> MakeDebugStatement(
}
base::Optional<ParseResult> MakeVoidType(ParseResultIterator* child_results) {
TypeExpression* result = MakeNode<BasicTypeExpression>(false, "void");
TypeExpression* result =
MakeNode<BasicTypeExpression>(std::vector<std::string>{}, false, "void");
return ParseResult{result};
}
......@@ -563,10 +564,12 @@ base::Optional<ParseResult> StringLiteralUnquoteAction(
base::Optional<ParseResult> MakeBasicTypeExpression(
ParseResultIterator* child_results) {
auto namespace_qualification =
child_results->NextAs<std::vector<std::string>>();
auto is_constexpr = child_results->NextAs<bool>();
auto name = child_results->NextAs<std::string>();
TypeExpression* result =
MakeNode<BasicTypeExpression>(is_constexpr, std::move(name));
TypeExpression* result = MakeNode<BasicTypeExpression>(
std::move(namespace_qualification), is_constexpr, std::move(name));
return ParseResult{result};
}
......@@ -845,7 +848,8 @@ base::Optional<ParseResult> MakeCatchBlock(ParseResultIterator* child_results) {
}
ParameterList parameters;
parameters.names.push_back(variable);
parameters.types.push_back(MakeNode<BasicTypeExpression>(false, "Object"));
parameters.types.push_back(MakeNode<BasicTypeExpression>(
std::vector<std::string>{}, false, "Object"));
parameters.has_varargs = false;
LabelBlock* result =
MakeNode<LabelBlock>("_catch", std::move(parameters), body);
......@@ -899,10 +903,13 @@ base::Optional<ParseResult> MakeElementAccessExpression(
base::Optional<ParseResult> MakeStructExpression(
ParseResultIterator* child_results) {
auto namespace_qualification =
child_results->NextAs<std::vector<std::string>>();
auto name = child_results->NextAs<std::string>();
auto expressions = child_results->NextAs<std::vector<Expression*>>();
Expression* result =
MakeNode<StructExpression>(std::move(name), std::move(expressions));
MakeNode<StructExpression>(std::move(namespace_qualification),
std::move(name), std::move(expressions));
return ParseResult{result};
}
......@@ -1099,7 +1106,9 @@ struct TorqueGrammar : Grammar {
// Result: TypeExpression*
Symbol simpleType = {
Rule({Token("("), &type, Token(")")}),
Rule({CheckIf(Token("constexpr")), &identifier}, MakeBasicTypeExpression),
Rule({List<std::string>(Sequence({&identifier, Token("::")})),
CheckIf(Token("constexpr")), &identifier},
MakeBasicTypeExpression),
Rule({Token("builtin"), Token("("), typeList, Token(")"), Token("=>"),
&simpleType},
MakeFunctionTypeExpression)};
......@@ -1235,9 +1244,10 @@ struct TorqueGrammar : Grammar {
CastParseResult<LocationExpression*, Expression*>),
Rule({&decimalLiteral}, MakeNumberLiteralExpression),
Rule({&stringLiteral}, MakeStringLiteralExpression),
Rule({&identifier, Token("{"), List<Expression*>(expression, Token(",")),
Token("}")},
MakeStructExpression),
Rule(
{List<std::string>(Sequence({&identifier, Token("::")})), &identifier,
Token("{"), List<Expression*>(expression, Token(",")), Token("}")},
MakeStructExpression),
Rule({Token("("), expression, Token(")")})};
// Result: Expression*
......
......@@ -654,7 +654,7 @@ namespace test {
macro TestIterator(implicit context: Context)(o: Object, map: Map) {
try {
const t1: Object = iterator::GetIteratorMethod(o);
const t2: IteratorRecord = iterator::GetIterator(o);
const t2: iterator::IteratorRecord = iterator::GetIterator(o);
const t3: Object = iterator::IteratorStep(t2) otherwise Fail;
const t4: Object = iterator::IteratorStep(t2, map) otherwise Fail;
......
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