Skip to content

Commit eed8b7d

Browse files
committed
Checl type of _Generic selection
Fixes #563 Signed-off-by: Roberto Raggi <[email protected]>
1 parent 8d9f986 commit eed8b7d

File tree

13 files changed

+633
-481
lines changed

13 files changed

+633
-481
lines changed

packages/cxx-frontend/src/ASTSlot.ts

Lines changed: 102 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -156,105 +156,106 @@ export enum ASTSlot {
156156
literalOperatorId = 133,
157157
lparen2Loc = 134,
158158
lparenLoc = 135,
159-
memInitializerList = 136,
160-
minusGreaterLoc = 137,
161-
moduleDeclaration = 138,
162-
moduleLoc = 139,
163-
moduleName = 140,
164-
modulePartition = 141,
165-
moduleQualifier = 142,
166-
mutableLoc = 143,
167-
namespaceLoc = 144,
168-
nestedNameSpecifier = 145,
169-
nestedNamespaceSpecifierList = 146,
170-
newInitalizer = 147,
171-
newLoc = 148,
172-
newPlacement = 149,
173-
noexceptLoc = 150,
174-
noreturnLoc = 151,
175-
offsetofLoc = 152,
176-
op = 153,
177-
opLoc = 154,
178-
openLoc = 155,
179-
operatorFunctionId = 156,
180-
operatorLoc = 157,
181-
otherVirtualOrAccessLoc = 158,
182-
outputOperandList = 159,
183-
parameterDeclarationClause = 160,
184-
parameterDeclarationList = 161,
185-
privateLoc = 162,
186-
privateModuleFragment = 163,
187-
ptrOpList = 164,
188-
qualifier = 165,
189-
qualifierLoc = 166,
190-
questionLoc = 167,
191-
rangeDeclaration = 168,
192-
rangeInitializer = 169,
193-
rbraceLoc = 170,
194-
rbracket2Loc = 171,
195-
rbracketLoc = 172,
196-
refLoc = 173,
197-
refOp = 174,
198-
refQualifierLoc = 175,
199-
registerLoc = 176,
200-
requirementList = 177,
201-
requiresClause = 178,
202-
requiresLoc = 179,
203-
restrictLoc = 180,
204-
returnLoc = 181,
205-
rightExpression = 182,
206-
rparen2Loc = 183,
207-
rparenLoc = 184,
208-
scopeLoc = 185,
209-
secondColonLoc = 186,
210-
semicolonLoc = 187,
211-
sizeExpression = 188,
212-
sizeofLoc = 189,
213-
specifier = 190,
214-
specifierLoc = 191,
215-
splicer = 192,
216-
starLoc = 193,
217-
statement = 194,
218-
statementList = 195,
219-
staticAssertLoc = 196,
220-
staticLoc = 197,
221-
stringLiteral = 198,
222-
stringliteralLoc = 199,
223-
switchLoc = 200,
224-
symbolicName = 201,
225-
symbolicNameLoc = 202,
226-
templateArgumentList = 203,
227-
templateId = 204,
228-
templateLoc = 205,
229-
templateParameterList = 206,
230-
templateRequiresClause = 207,
231-
thisLoc = 208,
232-
threadLoc = 209,
233-
threadLocalLoc = 210,
234-
throwLoc = 211,
235-
tildeLoc = 212,
236-
trailingReturnType = 213,
237-
tryLoc = 214,
238-
typeConstraint = 215,
239-
typeId = 216,
240-
typeIdList = 217,
241-
typeLoc = 218,
242-
typeQualifierList = 219,
243-
typeSpecifier = 220,
244-
typeSpecifierList = 221,
245-
typeTraitLoc = 222,
246-
typedefLoc = 223,
247-
typeidLoc = 224,
248-
typenameLoc = 225,
249-
underlyingTypeLoc = 226,
250-
unqualifiedId = 227,
251-
usingDeclaratorList = 228,
252-
usingLoc = 229,
253-
vaArgLoc = 230,
254-
virtualLoc = 231,
255-
virtualOrAccessLoc = 232,
256-
voidLoc = 233,
257-
volatileLoc = 234,
258-
whileLoc = 235,
259-
yieldLoc = 236,
159+
matchedAssocIndex = 136,
160+
memInitializerList = 137,
161+
minusGreaterLoc = 138,
162+
moduleDeclaration = 139,
163+
moduleLoc = 140,
164+
moduleName = 141,
165+
modulePartition = 142,
166+
moduleQualifier = 143,
167+
mutableLoc = 144,
168+
namespaceLoc = 145,
169+
nestedNameSpecifier = 146,
170+
nestedNamespaceSpecifierList = 147,
171+
newInitalizer = 148,
172+
newLoc = 149,
173+
newPlacement = 150,
174+
noexceptLoc = 151,
175+
noreturnLoc = 152,
176+
offsetofLoc = 153,
177+
op = 154,
178+
opLoc = 155,
179+
openLoc = 156,
180+
operatorFunctionId = 157,
181+
operatorLoc = 158,
182+
otherVirtualOrAccessLoc = 159,
183+
outputOperandList = 160,
184+
parameterDeclarationClause = 161,
185+
parameterDeclarationList = 162,
186+
privateLoc = 163,
187+
privateModuleFragment = 164,
188+
ptrOpList = 165,
189+
qualifier = 166,
190+
qualifierLoc = 167,
191+
questionLoc = 168,
192+
rangeDeclaration = 169,
193+
rangeInitializer = 170,
194+
rbraceLoc = 171,
195+
rbracket2Loc = 172,
196+
rbracketLoc = 173,
197+
refLoc = 174,
198+
refOp = 175,
199+
refQualifierLoc = 176,
200+
registerLoc = 177,
201+
requirementList = 178,
202+
requiresClause = 179,
203+
requiresLoc = 180,
204+
restrictLoc = 181,
205+
returnLoc = 182,
206+
rightExpression = 183,
207+
rparen2Loc = 184,
208+
rparenLoc = 185,
209+
scopeLoc = 186,
210+
secondColonLoc = 187,
211+
semicolonLoc = 188,
212+
sizeExpression = 189,
213+
sizeofLoc = 190,
214+
specifier = 191,
215+
specifierLoc = 192,
216+
splicer = 193,
217+
starLoc = 194,
218+
statement = 195,
219+
statementList = 196,
220+
staticAssertLoc = 197,
221+
staticLoc = 198,
222+
stringLiteral = 199,
223+
stringliteralLoc = 200,
224+
switchLoc = 201,
225+
symbolicName = 202,
226+
symbolicNameLoc = 203,
227+
templateArgumentList = 204,
228+
templateId = 205,
229+
templateLoc = 206,
230+
templateParameterList = 207,
231+
templateRequiresClause = 208,
232+
thisLoc = 209,
233+
threadLoc = 210,
234+
threadLocalLoc = 211,
235+
throwLoc = 212,
236+
tildeLoc = 213,
237+
trailingReturnType = 214,
238+
tryLoc = 215,
239+
typeConstraint = 216,
240+
typeId = 217,
241+
typeIdList = 218,
242+
typeLoc = 219,
243+
typeQualifierList = 220,
244+
typeSpecifier = 221,
245+
typeSpecifierList = 222,
246+
typeTraitLoc = 223,
247+
typedefLoc = 224,
248+
typeidLoc = 225,
249+
typenameLoc = 226,
250+
underlyingTypeLoc = 227,
251+
unqualifiedId = 228,
252+
usingDeclaratorList = 229,
253+
usingLoc = 230,
254+
vaArgLoc = 231,
255+
virtualLoc = 232,
256+
virtualOrAccessLoc = 233,
257+
voidLoc = 234,
258+
volatileLoc = 235,
259+
whileLoc = 236,
260+
yieldLoc = 237,
260261
}

packages/cxx-gen-ast/src/tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export const CXX_KEYWORDS: string[] = [
197197
"__underlying_type",
198198
"_Atomic",
199199
"_Complex",
200+
"_Generic",
200201
];
201202

202203
export const BUILTIN_TYPE_TRAITS: string[] = [

src/parser/cxx/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ class GenericSelectionExpressionAST final : public ExpressionAST {
13441344
SourceLocation commaLoc;
13451345
List<GenericAssociationAST*>* genericAssociationList = nullptr;
13461346
SourceLocation rparenLoc;
1347+
int matchedAssocIndex = -1;
13471348

13481349
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
13491350

src/parser/cxx/ast_interpreter.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,26 @@ auto ASTInterpreter::ExpressionVisitor::operator()(ThisExpressionAST* ast)
16461646

16471647
auto ASTInterpreter::ExpressionVisitor::operator()(
16481648
GenericSelectionExpressionAST* ast) -> ExpressionResult {
1649+
if (ast->matchedAssocIndex == -1) return std::nullopt;
1650+
1651+
GenericAssociationAST* assoc = nullptr;
1652+
int index = 0;
1653+
for (auto assocNode : ListView{ast->genericAssociationList}) {
1654+
if (index == ast->matchedAssocIndex) {
1655+
assoc = assocNode;
1656+
break;
1657+
}
1658+
++index;
1659+
}
1660+
1661+
if (auto def = ast_cast<DefaultGenericAssociationAST>(assoc)) {
1662+
return accept(def->expression);
1663+
}
1664+
1665+
if (auto entry = ast_cast<TypeGenericAssociationAST>(assoc)) {
1666+
return accept(entry->expression);
1667+
}
1668+
16491669
return std::nullopt;
16501670
}
16511671

src/parser/cxx/ast_printer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,10 @@ void ASTPrinter::visit(GenericSelectionExpressionAST* ast) {
854854
to_string(ast->type));
855855
}
856856
out_ << "\n";
857+
++indent_;
858+
out_ << std::format("{:{}}", "", indent_ * 2);
859+
out_ << std::format("matched-assoc-index: {}\n", ast->matchedAssocIndex);
860+
--indent_;
857861
accept(ast->expression, "expression");
858862
if (ast->genericAssociationList) {
859863
++indent_;

src/parser/cxx/ast_rewriter.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,7 @@ auto ASTRewriter::ExpressionVisitor::operator()(
24892489
}
24902490

24912491
copy->rparenLoc = ast->rparenLoc;
2492+
copy->matchedAssocIndex = ast->matchedAssocIndex;
24922493

24932494
return copy;
24942495
}

0 commit comments

Comments
 (0)