Commit f2ceaf90 authored by Tobias Tebbi's avatar Tobias Tebbi Committed by V8 LUCI CQ

[torque] allow @if/@ifnot annotations in more places

Conditional compilation with @if/@ifnot is now allowed for
- statements
- typeswitch cases
- enum constants
- bitfield struct fields
- struct fields and methods

Bug: v8:7793
Change-Id: I701e8b1f4fb5c5494eaf0af6d0b540bc9166b5ca
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3296283Reviewed-by: 's avatarNico Hartmann <nicohartmann@chromium.org>
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78060}
parent 666dcfd9
...@@ -2179,6 +2179,49 @@ struct TorqueGrammar : Grammar { ...@@ -2179,6 +2179,49 @@ struct TorqueGrammar : Grammar {
return true; return true;
} }
template <class T, bool first>
static base::Optional<ParseResult> MakeExtendedVectorIfAnnotation(
ParseResultIterator* child_results) {
std::vector<T> l = {};
if (!first) l = child_results->NextAs<std::vector<T>>();
AnnotationSet annotations(child_results, {},
{ANNOTATION_IF, ANNOTATION_IFNOT});
bool skipped = false;
if (base::Optional<std::string> condition =
annotations.GetStringParam(ANNOTATION_IF)) {
if (!BuildFlags::GetFlag(*condition, ANNOTATION_IF)) skipped = true;
}
if (base::Optional<std::string> condition =
annotations.GetStringParam(ANNOTATION_IFNOT)) {
if (BuildFlags::GetFlag(*condition, ANNOTATION_IFNOT)) skipped = true;
}
T x = child_results->NextAs<T>();
if (skipped) return ParseResult{std::move(l)};
l.push_back(std::move(x));
return ParseResult{std::move(l)};
}
template <class T>
Symbol* NonemptyListAllowIfAnnotation(
Symbol* element, base::Optional<Symbol*> separator = {}) {
Symbol* list = NewSymbol();
*list = {
Rule({annotations, element}, MakeExtendedVectorIfAnnotation<T, true>),
separator ? Rule({list, annotations, *separator, element},
MakeExtendedVectorIfAnnotation<T, false>)
: Rule({list, annotations, element},
MakeExtendedVectorIfAnnotation<T, false>)};
return list;
}
template <class T>
Symbol* ListAllowIfAnnotation(Symbol* element,
base::Optional<Symbol*> separator = {}) {
return TryOrDefault<std::vector<T>>(
NonemptyListAllowIfAnnotation<T>(element, separator));
}
TorqueGrammar() : Grammar(&file) { SetWhitespace(MatchWhitespace); } TorqueGrammar() : Grammar(&file) { SetWhitespace(MatchWhitespace); }
// Result: Expression* // Result: Expression*
...@@ -2515,9 +2558,10 @@ struct TorqueGrammar : Grammar { ...@@ -2515,9 +2558,10 @@ struct TorqueGrammar : Grammar {
MakeAssignmentExpression)}; MakeAssignmentExpression)};
// Result: Statement* // Result: Statement*
Symbol block = {Rule({CheckIf(Token("deferred")), Token("{"), Symbol block = {
List<Statement*>(&statement), Token("}")}, Rule({CheckIf(Token("deferred")), Token("{"),
MakeBlockStatement)}; ListAllowIfAnnotation<Statement*>(&statement), Token("}")},
MakeBlockStatement)};
// Result: TryHandler* // Result: TryHandler*
Symbol tryHandler = { Symbol tryHandler = {
...@@ -2577,7 +2621,7 @@ struct TorqueGrammar : Grammar { ...@@ -2577,7 +2621,7 @@ struct TorqueGrammar : Grammar {
expression, expression,
Token(")"), Token(")"),
Token("{"), Token("{"),
NonemptyList<TypeswitchCase>(&typeswitchCase), NonemptyListAllowIfAnnotation<TypeswitchCase>(&typeswitchCase),
Token("}"), Token("}"),
}, },
MakeTypeswitchStatement), MakeTypeswitchStatement),
...@@ -2637,11 +2681,13 @@ struct TorqueGrammar : Grammar { ...@@ -2637,11 +2681,13 @@ struct TorqueGrammar : Grammar {
MakeClassDeclaration), MakeClassDeclaration),
Rule({annotations, Token("struct"), &name, Rule({annotations, Token("struct"), &name,
TryOrDefault<GenericParameters>(&genericParameters), Token("{"), TryOrDefault<GenericParameters>(&genericParameters), Token("{"),
List<Declaration*>(&method), ListAllowIfAnnotation<Declaration*>(&method),
List<StructFieldExpression>(&structField), Token("}")}, ListAllowIfAnnotation<StructFieldExpression>(&structField),
Token("}")},
AsSingletonVector<Declaration*, MakeStructDeclaration>()), AsSingletonVector<Declaration*, MakeStructDeclaration>()),
Rule({Token("bitfield"), Token("struct"), &name, Token("extends"), &type, Rule({Token("bitfield"), Token("struct"), &name, Token("extends"), &type,
Token("{"), List<BitFieldDeclaration>(&bitFieldDeclaration), Token("{"),
ListAllowIfAnnotation<BitFieldDeclaration>(&bitFieldDeclaration),
Token("}")}, Token("}")},
AsSingletonVector<Declaration*, MakeBitFieldStructDeclaration>()), AsSingletonVector<Declaration*, MakeBitFieldStructDeclaration>()),
Rule({annotations, CheckIf(Token("transient")), Token("type"), &name, Rule({annotations, CheckIf(Token("transient")), Token("type"), &name,
...@@ -2698,7 +2744,8 @@ struct TorqueGrammar : Grammar { ...@@ -2698,7 +2744,8 @@ struct TorqueGrammar : Grammar {
Optional<TypeExpression*>(Sequence({Token("extends"), &type})), Optional<TypeExpression*>(Sequence({Token("extends"), &type})),
Optional<std::string>( Optional<std::string>(
Sequence({Token("constexpr"), &externalString})), Sequence({Token("constexpr"), &externalString})),
Token("{"), NonemptyList<EnumEntry>(&enumEntry, Token(",")), Token("{"),
NonemptyListAllowIfAnnotation<EnumEntry>(&enumEntry, Token(",")),
CheckIf(Sequence({Token(","), Token("...")})), Token("}")}, CheckIf(Sequence({Token(","), Token("...")})), Token("}")},
MakeEnumDeclaration), MakeEnumDeclaration),
Rule({Token("namespace"), &identifier, Token("{"), &declarationList, Rule({Token("namespace"), &identifier, Token("{"), &declarationList,
......
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