Commit d0a1c5b3 authored by Tobias Tebbi's avatar Tobias Tebbi Committed by Commit Bot

[torque] use :: to refer to global namespace

Bug: v8:7793
Change-Id: I5eac73a2b437e5e2d4005f79b7807ae7a9ed78e8
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2214829Reviewed-by: 's avatarBill Budge <bbudge@chromium.org>
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67976}
parent a02038b3
......@@ -46,16 +46,6 @@ extern runtime BigIntBinaryOp(
Context, Numeric, Numeric, SmiTagged<Operation>): BigInt;
} // namespace runtime
// Disambiguate macros with the same name as JS builtins.
// TODO(bbudge) Clean this up with appropriate namespaces.
macro NumberIsNaNImpl(number: Number): bool {
return NumberIsNaN(number);
}
macro StrictEqualImpl(left: JSAny, right: JSAny): Boolean {
return StrictEqual(left, right);
}
namespace number {
extern macro NaNStringConstant(): String;
extern macro ZeroStringConstant(): String;
......@@ -111,7 +101,7 @@ transitioning javascript builtin NumberPrototypeToString(
if (x == -0) {
return ZeroStringConstant();
} else if (NumberIsNaNImpl(x)) {
} else if (::NumberIsNaN(x)) {
return NaNStringConstant();
} else if (x == V8_INFINITY) {
return InfinityStringConstant();
......@@ -639,7 +629,7 @@ builtin Equal(implicit context: Context)(left: JSAny, right: JSAny): Object {
builtin StrictEqual(implicit context: Context)(
left: JSAny, right: JSAny): Object {
return StrictEqualImpl(left, right);
return ::StrictEqual(left, right);
}
} // namespace number
......@@ -77,6 +77,22 @@ SpecializationRequester::SpecializationRequester(SourcePosition position,
this->scope = scope;
}
std::vector<Declarable*> Scope::Lookup(const QualifiedName& name) {
if (name.namespace_qualification.size() >= 1 &&
name.namespace_qualification[0] == "") {
return GlobalContext::GetDefaultNamespace()->Lookup(
name.DropFirstNamespaceQualification());
}
std::vector<Declarable*> result;
if (ParentScope()) {
result = ParentScope()->Lookup(name);
}
for (Declarable* declarable : LookupShallow(name)) {
result.push_back(declarable);
}
return result;
}
base::Optional<std::string> TypeConstraint::IsViolated(const Type* type) const {
if (upper_bound && !type->IsSubtypeOf(*upper_bound)) {
return {ToString("expected ", *type, " to be a subtype of ", *upper_bound)};
......
......@@ -36,6 +36,17 @@ struct QualifiedName {
explicit QualifiedName(std::string name)
: QualifiedName({}, std::move(name)) {}
bool HasNamespaceQualification() const {
return !namespace_qualification.empty();
}
QualifiedName DropFirstNamespaceQualification() const {
return QualifiedName{
std::vector<std::string>(namespace_qualification.begin() + 1,
namespace_qualification.end()),
name};
}
friend std::ostream& operator<<(std::ostream& os, const QualifiedName& name);
};
......@@ -163,7 +174,7 @@ class Scope : public Declarable {
explicit Scope(Declarable::Kind kind) : Declarable(kind) {}
std::vector<Declarable*> LookupShallow(const QualifiedName& name) {
if (name.namespace_qualification.empty()) return declarations_[name.name];
if (!name.HasNamespaceQualification()) return declarations_[name.name];
Scope* child = nullptr;
for (Declarable* declarable :
declarations_[name.namespace_qualification.front()]) {
......@@ -176,22 +187,10 @@ class Scope : public Declarable {
}
}
if (child == nullptr) return {};
return child->LookupShallow(
QualifiedName({name.namespace_qualification.begin() + 1,
name.namespace_qualification.end()},
name.name));
return child->LookupShallow(name.DropFirstNamespaceQualification());
}
std::vector<Declarable*> Lookup(const QualifiedName& name) {
std::vector<Declarable*> result;
if (ParentScope()) {
result = ParentScope()->Lookup(name);
}
for (Declarable* declarable : LookupShallow(name)) {
result.push_back(declarable);
}
return result;
}
std::vector<Declarable*> Lookup(const QualifiedName& name);
template <class T>
T* AddDeclarable(const std::string& name, T* declarable) {
declarations_[name].push_back(declarable);
......
......@@ -1602,6 +1602,17 @@ base::Optional<ParseResult> MakeRightShiftIdentifier(
return ParseResult{MakeNode<Identifier>(str)};
}
base::Optional<ParseResult> MakeNamespaceQualification(
ParseResultIterator* child_results) {
bool global_namespace = child_results->NextAs<bool>();
auto namespace_qualification =
child_results->NextAs<std::vector<std::string>>();
if (global_namespace) {
namespace_qualification.insert(namespace_qualification.begin(), "");
}
return ParseResult(std::move(namespace_qualification));
}
base::Optional<ParseResult> MakeIdentifierExpression(
ParseResultIterator* child_results) {
auto namespace_qualification =
......@@ -1982,14 +1993,19 @@ struct TorqueGrammar : Grammar {
// Result: std::vector<Annotation>
Symbol* annotations = List<Annotation>(&annotation);
// Result: std::vector<std::string>
Symbol namespaceQualification = {
Rule({CheckIf(Token("::")),
List<std::string>(Sequence({&identifier, Token("::")}))},
MakeNamespaceQualification)};
// Result: TypeList
Symbol* typeList = List<TypeExpression*>(&type, Token(","));
// Result: TypeExpression*
Symbol simpleType = {
Rule({Token("("), &type, Token(")")}),
Rule({List<std::string>(Sequence({&identifier, Token("::")})),
CheckIf(Token("constexpr")), &identifier,
Rule({&namespaceQualification, CheckIf(Token("constexpr")), &identifier,
TryOrDefault<std::vector<TypeExpression*>>(
&genericSpecializationTypeList)},
MakeBasicTypeExpression),
......@@ -2123,7 +2139,7 @@ struct TorqueGrammar : Grammar {
// Result: Expression*
Symbol identifierExpression = {
Rule({List<std::string>(Sequence({&identifier, Token("::")})), &name,
Rule({&namespaceQualification, &name,
TryOrDefault<TypeList>(&genericSpecializationTypeList)},
MakeIdentifierExpression),
};
......
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