inspect.go (17335B)
1 // Copyright 2020 The CC Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cc // import "modernc.org/cc/v3" 6 7 // Inspect inspects AST node trees. 8 // 9 // If n is a non-terminal node, f(n, true) is called first. Next, f is called 10 // recursively for each of n's non-nil non-terminal children nodes, if any, in 11 // alphabetical order. Next, all n's terminal nodes, if any, are visited in 12 // the numeric order of their suffixes (Token, Token2, Token3, ...). Finally, 13 // f(n, false) is invoked. 14 // 15 // If n a terminal node, of type *Token, f(n, <unspecified boolean value> is 16 // called once. 17 // 18 // Inspect stops when any invocation of f returns false. 19 func Inspect(n Node, f func(Node, bool) bool) { 20 see(n, f) 21 } 22 23 func see(n Node, f func(Node, bool) bool) bool { 24 switch x := n.(type) { 25 case *AbstractDeclarator: 26 return x == nil || f(x, true) && 27 see(x.DirectAbstractDeclarator, f) && 28 see(x.Pointer, f) && 29 f(x, false) 30 case *AdditiveExpression: 31 return x == nil || f(x, true) && 32 see(x.AdditiveExpression, f) && 33 see(x.MultiplicativeExpression, f) && 34 see(&x.Token, f) && 35 f(x, false) 36 case *AlignmentSpecifier: 37 return x == nil || f(x, true) && 38 see(x.ConstantExpression, f) && 39 see(x.TypeName, f) && 40 see(&x.Token, f) && 41 see(&x.Token2, f) && 42 see(&x.Token2, f) && 43 see(&x.Token3, f) && 44 f(x, false) 45 case *AndExpression: 46 return x == nil || f(x, true) && 47 see(x.AndExpression, f) && 48 see(x.EqualityExpression, f) && 49 see(&x.Token, f) && 50 f(x, false) 51 case *ArgumentExpressionList: 52 return x == nil || f(x, true) && 53 see(x.ArgumentExpressionList, f) && 54 see(x.AssignmentExpression, f) && 55 see(&x.Token, f) && 56 f(x, false) 57 case *Asm: 58 return x == nil || f(x, true) && 59 see(x.AsmArgList, f) && 60 see(x.AsmQualifierList, f) && 61 see(&x.Token, f) && 62 see(&x.Token2, f) && 63 see(&x.Token2, f) && 64 see(&x.Token3, f) && 65 see(&x.Token4, f) && 66 f(x, false) 67 case *AsmArgList: 68 return x == nil || f(x, true) && 69 see(x.AsmArgList, f) && 70 see(x.AsmExpressionList, f) && 71 see(&x.Token, f) && 72 f(x, false) 73 case *AsmExpressionList: 74 return x == nil || f(x, true) && 75 see(x.AsmExpressionList, f) && 76 see(x.AsmIndex, f) && 77 see(x.AssignmentExpression, f) && 78 see(&x.Token, f) && 79 f(x, false) 80 case *AsmFunctionDefinition: 81 return x == nil || f(x, true) && 82 see(x.AsmStatement, f) && 83 see(x.DeclarationSpecifiers, f) && 84 see(x.Declarator, f) && 85 f(x, false) 86 case *AsmIndex: 87 return x == nil || f(x, true) && 88 see(x.Expression, f) && 89 see(&x.Token, f) && 90 see(&x.Token2, f) && 91 see(&x.Token2, f) && 92 f(x, false) 93 case *AsmQualifier: 94 return x == nil || f(x, true) && 95 see(&x.Token, f) && 96 f(x, false) 97 case *AsmQualifierList: 98 return x == nil || f(x, true) && 99 see(x.AsmQualifier, f) && 100 see(x.AsmQualifierList, f) && 101 f(x, false) 102 case *AsmStatement: 103 return x == nil || f(x, true) && 104 see(x.Asm, f) && 105 see(x.AttributeSpecifierList, f) && 106 see(&x.Token, f) && 107 f(x, false) 108 case *AssignmentExpression: 109 return x == nil || f(x, true) && 110 see(x.AssignmentExpression, f) && 111 see(x.ConditionalExpression, f) && 112 see(x.UnaryExpression, f) && 113 see(&x.Token, f) && 114 f(x, false) 115 case *AtomicTypeSpecifier: 116 return x == nil || f(x, true) && 117 see(x.TypeName, f) && 118 see(&x.Token, f) && 119 see(&x.Token2, f) && 120 see(&x.Token2, f) && 121 see(&x.Token3, f) && 122 f(x, false) 123 case *AttributeSpecifier: 124 return x == nil || f(x, true) && 125 see(x.AttributeValueList, f) && 126 see(&x.Token, f) && 127 see(&x.Token2, f) && 128 see(&x.Token2, f) && 129 see(&x.Token3, f) && 130 see(&x.Token4, f) && 131 see(&x.Token5, f) && 132 f(x, false) 133 case *AttributeSpecifierList: 134 return x == nil || f(x, true) && 135 see(x.AttributeSpecifier, f) && 136 see(x.AttributeSpecifierList, f) && 137 f(x, false) 138 case *AttributeValue: 139 return x == nil || f(x, true) && 140 see(x.ExpressionList, f) && 141 see(&x.Token, f) && 142 see(&x.Token2, f) && 143 see(&x.Token2, f) && 144 see(&x.Token3, f) && 145 f(x, false) 146 case *AttributeValueList: 147 return x == nil || f(x, true) && 148 see(x.AttributeValue, f) && 149 see(x.AttributeValueList, f) && 150 see(&x.Token, f) && 151 f(x, false) 152 case *BlockItem: 153 return x == nil || f(x, true) && 154 see(x.CompoundStatement, f) && 155 see(x.Declaration, f) && 156 see(x.DeclarationSpecifiers, f) && 157 see(x.Declarator, f) && 158 see(x.LabelDeclaration, f) && 159 see(x.PragmaSTDC, f) && 160 see(x.Statement, f) && 161 f(x, false) 162 case *BlockItemList: 163 return x == nil || f(x, true) && 164 see(x.BlockItem, f) && 165 see(x.BlockItemList, f) && 166 f(x, false) 167 case *CastExpression: 168 return x == nil || f(x, true) && 169 see(x.CastExpression, f) && 170 see(x.TypeName, f) && 171 see(x.UnaryExpression, f) && 172 see(&x.Token, f) && 173 see(&x.Token2, f) && 174 see(&x.Token2, f) && 175 f(x, false) 176 case *CompoundStatement: 177 return x == nil || f(x, true) && 178 see(x.BlockItemList, f) && 179 see(&x.Token, f) && 180 see(&x.Token2, f) && 181 see(&x.Token2, f) && 182 f(x, false) 183 case *ConditionalExpression: 184 return x == nil || f(x, true) && 185 see(x.ConditionalExpression, f) && 186 see(x.Expression, f) && 187 see(x.LogicalOrExpression, f) && 188 see(&x.Token, f) && 189 see(&x.Token2, f) && 190 see(&x.Token2, f) && 191 f(x, false) 192 case *ConstantExpression: 193 return x == nil || f(x, true) && 194 see(x.ConditionalExpression, f) && 195 f(x, false) 196 case *Declaration: 197 return x == nil || f(x, true) && 198 see(x.DeclarationSpecifiers, f) && 199 see(x.InitDeclaratorList, f) && 200 see(&x.Token, f) && 201 f(x, false) 202 case *DeclarationList: 203 return x == nil || f(x, true) && 204 see(x.Declaration, f) && 205 see(x.DeclarationList, f) && 206 f(x, false) 207 case *DeclarationSpecifiers: 208 return x == nil || f(x, true) && 209 see(x.AlignmentSpecifier, f) && 210 see(x.AttributeSpecifier, f) && 211 see(x.DeclarationSpecifiers, f) && 212 see(x.FunctionSpecifier, f) && 213 see(x.StorageClassSpecifier, f) && 214 see(x.TypeQualifier, f) && 215 see(x.TypeSpecifier, f) && 216 f(x, false) 217 case *Declarator: 218 return x == nil || f(x, true) && 219 see(x.AttributeSpecifierList, f) && 220 see(x.DirectDeclarator, f) && 221 see(x.Pointer, f) && 222 f(x, false) 223 case *Designation: 224 return x == nil || f(x, true) && 225 see(x.DesignatorList, f) && 226 see(&x.Token, f) && 227 f(x, false) 228 case *Designator: 229 return x == nil || f(x, true) && 230 see(x.ConstantExpression, f) && 231 see(&x.Token, f) && 232 see(&x.Token2, f) && 233 see(&x.Token2, f) && 234 f(x, false) 235 case *DesignatorList: 236 return x == nil || f(x, true) && 237 see(x.Designator, f) && 238 see(x.DesignatorList, f) && 239 f(x, false) 240 case *DirectAbstractDeclarator: 241 return x == nil || f(x, true) && 242 see(x.AbstractDeclarator, f) && 243 see(x.AssignmentExpression, f) && 244 see(x.DirectAbstractDeclarator, f) && 245 see(x.ParameterTypeList, f) && 246 see(x.TypeQualifiers, f) && 247 see(&x.Token, f) && 248 see(&x.Token2, f) && 249 see(&x.Token2, f) && 250 see(&x.Token3, f) && 251 f(x, false) 252 case *DirectDeclarator: 253 return x == nil || f(x, true) && 254 see(x.Asm, f) && 255 see(x.AssignmentExpression, f) && 256 see(x.AttributeSpecifierList, f) && 257 see(x.Declarator, f) && 258 see(x.DirectDeclarator, f) && 259 see(x.IdentifierList, f) && 260 see(x.ParameterTypeList, f) && 261 see(x.TypeQualifiers, f) && 262 see(&x.Token, f) && 263 see(&x.Token2, f) && 264 see(&x.Token2, f) && 265 see(&x.Token3, f) && 266 f(x, false) 267 case *EnumSpecifier: 268 return x == nil || f(x, true) && 269 see(x.AttributeSpecifierList, f) && 270 see(x.EnumeratorList, f) && 271 see(&x.Token, f) && 272 see(&x.Token2, f) && 273 see(&x.Token2, f) && 274 see(&x.Token3, f) && 275 see(&x.Token4, f) && 276 see(&x.Token5, f) && 277 f(x, false) 278 case *Enumerator: 279 return x == nil || f(x, true) && 280 see(x.AttributeSpecifierList, f) && 281 see(x.ConstantExpression, f) && 282 see(&x.Token, f) && 283 see(&x.Token2, f) && 284 see(&x.Token2, f) && 285 f(x, false) 286 case *EnumeratorList: 287 return x == nil || f(x, true) && 288 see(x.Enumerator, f) && 289 see(x.EnumeratorList, f) && 290 see(&x.Token, f) && 291 f(x, false) 292 case *EqualityExpression: 293 return x == nil || f(x, true) && 294 see(x.EqualityExpression, f) && 295 see(x.RelationalExpression, f) && 296 see(&x.Token, f) && 297 f(x, false) 298 case *ExclusiveOrExpression: 299 return x == nil || f(x, true) && 300 see(x.AndExpression, f) && 301 see(x.ExclusiveOrExpression, f) && 302 see(&x.Token, f) && 303 f(x, false) 304 case *Expression: 305 return x == nil || f(x, true) && 306 see(x.AssignmentExpression, f) && 307 see(x.Expression, f) && 308 see(&x.Token, f) && 309 f(x, false) 310 case *ExpressionList: 311 return x == nil || f(x, true) && 312 see(x.AssignmentExpression, f) && 313 see(x.ExpressionList, f) && 314 see(&x.Token, f) && 315 f(x, false) 316 case *ExpressionStatement: 317 return x == nil || f(x, true) && 318 see(x.AttributeSpecifierList, f) && 319 see(x.Expression, f) && 320 see(&x.Token, f) && 321 f(x, false) 322 case *ExternalDeclaration: 323 return x == nil || f(x, true) && 324 see(x.AsmFunctionDefinition, f) && 325 see(x.AsmStatement, f) && 326 see(x.Declaration, f) && 327 see(x.FunctionDefinition, f) && 328 see(x.PragmaSTDC, f) && 329 see(&x.Token, f) && 330 f(x, false) 331 case *FunctionDefinition: 332 return x == nil || f(x, true) && 333 see(x.CompoundStatement, f) && 334 see(x.DeclarationList, f) && 335 see(x.DeclarationSpecifiers, f) && 336 see(x.Declarator, f) && 337 f(x, false) 338 case *FunctionSpecifier: 339 return x == nil || f(x, true) && 340 see(&x.Token, f) && 341 f(x, false) 342 case *IdentifierList: 343 return x == nil || f(x, true) && 344 see(x.IdentifierList, f) && 345 see(&x.Token, f) && 346 see(&x.Token2, f) && 347 see(&x.Token2, f) && 348 f(x, false) 349 case *InclusiveOrExpression: 350 return x == nil || f(x, true) && 351 see(x.ExclusiveOrExpression, f) && 352 see(x.InclusiveOrExpression, f) && 353 see(&x.Token, f) && 354 f(x, false) 355 case *InitDeclarator: 356 return x == nil || f(x, true) && 357 see(x.AttributeSpecifierList, f) && 358 see(x.Declarator, f) && 359 see(x.Initializer, f) && 360 see(&x.Token, f) && 361 f(x, false) 362 case *InitDeclaratorList: 363 return x == nil || f(x, true) && 364 see(x.AttributeSpecifierList, f) && 365 see(x.InitDeclarator, f) && 366 see(x.InitDeclaratorList, f) && 367 see(&x.Token, f) && 368 f(x, false) 369 case *Initializer: 370 return x == nil || f(x, true) && 371 see(x.AssignmentExpression, f) && 372 see(x.InitializerList, f) && 373 see(&x.Token, f) && 374 see(&x.Token2, f) && 375 see(&x.Token2, f) && 376 see(&x.Token3, f) && 377 f(x, false) 378 case *InitializerList: 379 return x == nil || f(x, true) && 380 see(x.Designation, f) && 381 see(x.Initializer, f) && 382 see(x.InitializerList, f) && 383 see(&x.Token, f) && 384 f(x, false) 385 case *IterationStatement: 386 return x == nil || f(x, true) && 387 see(x.Declaration, f) && 388 see(x.Expression, f) && 389 see(x.Expression2, f) && 390 see(x.Expression3, f) && 391 see(x.Statement, f) && 392 see(&x.Token, f) && 393 see(&x.Token2, f) && 394 see(&x.Token2, f) && 395 see(&x.Token3, f) && 396 see(&x.Token4, f) && 397 see(&x.Token5, f) && 398 f(x, false) 399 case *JumpStatement: 400 return x == nil || f(x, true) && 401 see(x.Expression, f) && 402 see(&x.Token, f) && 403 see(&x.Token2, f) && 404 see(&x.Token2, f) && 405 see(&x.Token3, f) && 406 f(x, false) 407 case *LabelDeclaration: 408 return x == nil || f(x, true) && 409 see(x.IdentifierList, f) && 410 see(&x.Token, f) && 411 see(&x.Token2, f) && 412 see(&x.Token2, f) && 413 f(x, false) 414 case *LabeledStatement: 415 return x == nil || f(x, true) && 416 see(x.AttributeSpecifierList, f) && 417 see(x.ConstantExpression, f) && 418 see(x.ConstantExpression2, f) && 419 see(x.Statement, f) && 420 see(&x.Token, f) && 421 see(&x.Token2, f) && 422 see(&x.Token2, f) && 423 see(&x.Token3, f) && 424 f(x, false) 425 case *LogicalAndExpression: 426 return x == nil || f(x, true) && 427 see(x.InclusiveOrExpression, f) && 428 see(x.LogicalAndExpression, f) && 429 see(&x.Token, f) && 430 f(x, false) 431 case *LogicalOrExpression: 432 return x == nil || f(x, true) && 433 see(x.LogicalAndExpression, f) && 434 see(x.LogicalOrExpression, f) && 435 see(&x.Token, f) && 436 f(x, false) 437 case *MultiplicativeExpression: 438 return x == nil || f(x, true) && 439 see(x.CastExpression, f) && 440 see(x.MultiplicativeExpression, f) && 441 see(&x.Token, f) && 442 f(x, false) 443 case *ParameterDeclaration: 444 return x == nil || f(x, true) && 445 see(x.AbstractDeclarator, f) && 446 see(x.AttributeSpecifierList, f) && 447 see(x.DeclarationSpecifiers, f) && 448 see(x.Declarator, f) && 449 f(x, false) 450 case *ParameterList: 451 return x == nil || f(x, true) && 452 see(x.ParameterDeclaration, f) && 453 see(x.ParameterList, f) && 454 see(&x.Token, f) && 455 f(x, false) 456 case *ParameterTypeList: 457 return x == nil || f(x, true) && 458 see(x.ParameterList, f) && 459 see(&x.Token, f) && 460 see(&x.Token2, f) && 461 see(&x.Token2, f) && 462 f(x, false) 463 case *Pointer: 464 return x == nil || f(x, true) && 465 see(x.Pointer, f) && 466 see(x.TypeQualifiers, f) && 467 see(&x.Token, f) && 468 f(x, false) 469 case *PostfixExpression: 470 return x == nil || f(x, true) && 471 see(x.ArgumentExpressionList, f) && 472 see(x.Expression, f) && 473 see(x.InitializerList, f) && 474 see(x.PostfixExpression, f) && 475 see(x.PrimaryExpression, f) && 476 see(x.TypeName, f) && 477 see(x.TypeName2, f) && 478 see(&x.Token, f) && 479 see(&x.Token2, f) && 480 see(&x.Token2, f) && 481 see(&x.Token3, f) && 482 see(&x.Token4, f) && 483 see(&x.Token5, f) && 484 f(x, false) 485 case *PragmaSTDC: 486 return x == nil || f(x, true) && 487 see(&x.Token, f) && 488 see(&x.Token2, f) && 489 see(&x.Token2, f) && 490 see(&x.Token3, f) && 491 see(&x.Token4, f) && 492 f(x, false) 493 case *PrimaryExpression: 494 return x == nil || f(x, true) && 495 see(x.CompoundStatement, f) && 496 see(x.Expression, f) && 497 see(&x.Token, f) && 498 see(&x.Token2, f) && 499 see(&x.Token2, f) && 500 f(x, false) 501 case *RelationalExpression: 502 return x == nil || f(x, true) && 503 see(x.RelationalExpression, f) && 504 see(x.ShiftExpression, f) && 505 see(&x.Token, f) && 506 f(x, false) 507 case *SelectionStatement: 508 return x == nil || f(x, true) && 509 see(x.Expression, f) && 510 see(x.Statement, f) && 511 see(x.Statement2, f) && 512 see(&x.Token, f) && 513 see(&x.Token2, f) && 514 see(&x.Token2, f) && 515 see(&x.Token3, f) && 516 see(&x.Token4, f) && 517 f(x, false) 518 case *ShiftExpression: 519 return x == nil || f(x, true) && 520 see(x.AdditiveExpression, f) && 521 see(x.ShiftExpression, f) && 522 see(&x.Token, f) && 523 f(x, false) 524 case *SpecifierQualifierList: 525 return x == nil || f(x, true) && 526 see(x.AlignmentSpecifier, f) && 527 see(x.AttributeSpecifier, f) && 528 see(x.SpecifierQualifierList, f) && 529 see(x.TypeQualifier, f) && 530 see(x.TypeSpecifier, f) && 531 f(x, false) 532 case *Statement: 533 return x == nil || f(x, true) && 534 see(x.AsmStatement, f) && 535 see(x.CompoundStatement, f) && 536 see(x.ExpressionStatement, f) && 537 see(x.IterationStatement, f) && 538 see(x.JumpStatement, f) && 539 see(x.LabeledStatement, f) && 540 see(x.SelectionStatement, f) && 541 f(x, false) 542 case *StorageClassSpecifier: 543 return x == nil || f(x, true) && 544 see(&x.Token, f) && 545 f(x, false) 546 case *StructDeclaration: 547 return x == nil || f(x, true) && 548 see(x.SpecifierQualifierList, f) && 549 see(x.StructDeclaratorList, f) && 550 see(&x.Token, f) && 551 f(x, false) 552 case *StructDeclarationList: 553 return x == nil || f(x, true) && 554 see(x.StructDeclaration, f) && 555 see(x.StructDeclarationList, f) && 556 f(x, false) 557 case *StructDeclarator: 558 return x == nil || f(x, true) && 559 see(x.AttributeSpecifierList, f) && 560 see(x.ConstantExpression, f) && 561 see(x.Declarator, f) && 562 see(&x.Token, f) && 563 f(x, false) 564 case *StructDeclaratorList: 565 return x == nil || f(x, true) && 566 see(x.StructDeclarator, f) && 567 see(x.StructDeclaratorList, f) && 568 see(&x.Token, f) && 569 f(x, false) 570 case *StructOrUnion: 571 return x == nil || f(x, true) && 572 see(&x.Token, f) && 573 f(x, false) 574 case *StructOrUnionSpecifier: 575 return x == nil || f(x, true) && 576 see(x.AttributeSpecifierList, f) && 577 see(x.StructDeclarationList, f) && 578 see(x.StructOrUnion, f) && 579 see(&x.Token, f) && 580 see(&x.Token2, f) && 581 see(&x.Token2, f) && 582 see(&x.Token3, f) && 583 f(x, false) 584 case *TranslationUnit: 585 return x == nil || f(x, true) && 586 see(x.ExternalDeclaration, f) && 587 see(x.TranslationUnit, f) && 588 f(x, false) 589 case *TypeName: 590 return x == nil || f(x, true) && 591 see(x.AbstractDeclarator, f) && 592 see(x.SpecifierQualifierList, f) && 593 f(x, false) 594 case *TypeQualifier: 595 return x == nil || f(x, true) && 596 see(&x.Token, f) && 597 f(x, false) 598 case *TypeQualifiers: 599 return x == nil || f(x, true) && 600 see(x.AttributeSpecifier, f) && 601 see(x.TypeQualifier, f) && 602 see(x.TypeQualifiers, f) && 603 f(x, false) 604 case *TypeSpecifier: 605 return x == nil || f(x, true) && 606 see(x.AtomicTypeSpecifier, f) && 607 see(x.EnumSpecifier, f) && 608 see(x.Expression, f) && 609 see(x.StructOrUnionSpecifier, f) && 610 see(x.TypeName, f) && 611 see(&x.Token, f) && 612 see(&x.Token2, f) && 613 see(&x.Token2, f) && 614 see(&x.Token3, f) && 615 f(x, false) 616 case *UnaryExpression: 617 return x == nil || f(x, true) && 618 see(x.CastExpression, f) && 619 see(x.PostfixExpression, f) && 620 see(x.TypeName, f) && 621 see(x.UnaryExpression, f) && 622 see(&x.Token, f) && 623 see(&x.Token2, f) && 624 see(&x.Token2, f) && 625 see(&x.Token3, f) && 626 f(x, false) 627 case *Token: 628 return f(x, true) 629 default: 630 panic(todo("internal error: %T", x)) 631 } 632 }