Changes in src/Parser/DeclarationNode.cc [68cd1ce:59db689]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (41 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r68cd1ce r59db689 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Jun 13 08:02:03 201513 // Update Count : 5812 // Last Modified On : Sat Jun 6 23:29:13 2015 13 // Update Count : 23 14 14 // 15 15 … … 21 21 22 22 #include "TypeData.h" 23 24 #include "SynTree/Declaration.h"25 23 #include "SynTree/Expression.h" 26 24 25 27 26 using namespace std; 28 27 29 28 // These must remain in the same order as the corresponding DeclarationNode enumerations. 30 const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "" };31 29 const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" }; 32 30 const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" }; 33 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };34 const char *DeclarationNode:: aggregateName[] = { "struct", "union", "context" };31 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" }; 32 const char *DeclarationNode::tyConName[] = { "struct", "union", "context" }; 35 33 const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" }; 36 34 … … 65 63 } 66 64 65 const char *storageClassName[] = { 66 // order must correspond with DeclarationNode::StorageClass 67 "extern", 68 "static", 69 "auto", 70 "register", 71 "inline", 72 "fortran", 73 }; 74 67 75 void DeclarationNode::print( std::ostream &os, int indent ) const { 68 76 os << string( indent, ' ' ); … … 71 79 } else { 72 80 os << name << ": "; 73 } // if81 } 74 82 75 83 if ( linkage != LinkageSpec::Cforall ) { 76 84 os << LinkageSpec::toString( linkage ) << " "; 77 } // if78 79 printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );85 } 86 87 printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os ); 80 88 if ( type ) { 81 89 type->print( os, indent ); 82 90 } else { 83 91 os << "untyped entity "; 84 } // if92 } 85 93 86 94 if ( bitfieldWidth ) { 87 95 os << endl << string( indent + 2, ' ' ) << "with bitfield width "; 88 96 bitfieldWidth->printOneLine( os ); 89 } // if97 } 90 98 91 99 if ( initializer != 0 ) { 92 100 os << endl << string( indent + 2, ' ' ) << "with initializer "; 93 101 initializer->printOneLine( os ); 94 } // if102 } 95 103 96 104 os << endl; … … 101 109 if ( hasEllipsis ) { 102 110 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; 103 } // if111 } 104 112 } 105 113 … … 115 123 if ( body ) { 116 124 newnode->type->function->hasBody = true; 117 } // if125 } 118 126 119 127 if ( ret ) { … … 121 129 ret->type = 0; 122 130 delete ret; 123 } // if131 } 124 132 125 133 return newnode; … … 133 141 } 134 142 135 DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {143 DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) { 136 144 DeclarationNode *newnode = new DeclarationNode; 137 145 newnode->storageClasses.push_back( sc ); … … 169 177 } 170 178 171 DeclarationNode *DeclarationNode::newAggregate( Aggregatekind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {179 DeclarationNode *DeclarationNode::newAggregate( TyCon kind, std::string *name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) { 172 180 DeclarationNode *newnode = new DeclarationNode; 173 181 newnode->type = new TypeData( TypeData::Aggregate ); … … 176 184 if ( newnode->type->aggregate->name == "" ) { 177 185 newnode->type->aggregate->name = DeclarationNode::anonymous.newName(); 178 } // if186 } 179 187 newnode->type->aggregate->params = formals; 180 188 newnode->type->aggregate->actuals = actuals; … … 190 198 if ( newnode->type->enumeration->name == "" ) { 191 199 newnode->type->enumeration->name = DeclarationNode::anonymous.newName(); 192 } // if200 } 193 201 newnode->type->enumeration->constants = constants; 194 202 return newnode; … … 323 331 } else { 324 332 dst->forall = src->forall; 325 } // if333 } 326 334 src->forall = 0; 327 } // if335 } 328 336 if ( dst->base ) { 329 337 addQualifiersToType( src, dst->base ); … … 333 341 } else { 334 342 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers ); 335 } // if336 } // if343 } 344 } 337 345 } 338 346 … … 343 351 if ( ! type ) { 344 352 type = new TypeData; 345 } // if353 } 346 354 addQualifiersToType( q->type, type ); 347 355 if ( q->type && q->type->forall ) { … … 349 357 type->forall->appendList( q->type->forall ); 350 358 } else { 351 if ( type->kind == TypeData::Aggregate ) { 352 type->aggregate->params = q->type->forall; 353 } else { 354 type->forall = q->type->forall; 355 } // if 356 } // if 359 type->forall = q->type->forall; 360 } 357 361 q->type->forall = 0; 358 } // if359 } // if360 } // if362 } 363 } 364 } 361 365 delete q; 362 366 return this; … … 375 379 } else { 376 380 dst->forall = src->forall; 377 } // if381 } 378 382 src->forall = 0; 379 } // if383 } 380 384 if ( dst->base ) { 381 385 addTypeToType( src, dst->base ); … … 394 398 dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers ); 395 399 dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec ); 396 } // if400 } 397 401 break; 398 402 … … 405 409 if ( src->kind == TypeData::Aggregate ) { 406 410 dst->base->aggInst->params = maybeClone( src->aggregate->actuals ); 407 } // if411 } 408 412 dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers ); 409 413 src = 0; … … 415 419 } else { 416 420 dst->forall = src->forall; 417 } // if421 } 418 422 src->forall = 0; 419 423 dst->base = src; 420 424 src = 0; 421 } // switch422 } // switch423 } // if424 } // if425 } 426 } 427 } 428 } 425 429 } 426 430 … … 435 439 if ( o->type->kind == TypeData::Aggregate ) { 436 440 type->aggInst->params = maybeClone( o->type->aggregate->actuals ); 437 } // if441 } 438 442 type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers ); 439 443 } else { 440 444 type = o->type; 441 } // if445 } 442 446 o->type = 0; 443 447 } else { 444 448 addTypeToType( o->type, type ); 445 } // if446 } // if449 } 450 } 447 451 if ( o->bitfieldWidth ) { 448 452 bitfieldWidth = o->bitfieldWidth; 449 } // if450 } // if453 } 454 } 451 455 delete o; 452 456 return this; … … 471 475 } else { 472 476 type->symbolic->assertions = assertions; 473 } // if477 } 474 478 break; 479 475 480 case TypeData::Variable: 476 481 if ( type->variable->assertions ) { … … 478 483 } else { 479 484 type->variable->assertions = assertions; 480 } // if485 } 481 486 break; 487 482 488 default: 483 489 assert( false ); 484 } // switch490 } 485 491 486 492 return this; … … 520 526 } 521 527 522 static void setBase( TypeData *&type, TypeData *newType ) { 528 static void 529 setBase( TypeData *&type, TypeData *newType ) { 523 530 if ( type ) { 524 531 TypeData *prevBase = type; … … 527 534 prevBase = curBase; 528 535 curBase = curBase->base; 529 } // while536 } 530 537 prevBase->base = newType; 531 538 } else { 532 539 type = newType; 533 } // if540 } 534 541 } 535 542 … … 540 547 p->type = 0; 541 548 delete p; 542 } // if549 } 543 550 return this; 544 551 } … … 550 557 a->type = 0; 551 558 delete a; 552 } // if559 } 553 560 return this; 554 561 } … … 565 572 if ( type->kind == TypeData::Aggregate ) { 566 573 p->type->base->aggInst->params = maybeClone( type->aggregate->actuals ); 567 } // if574 } 568 575 p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers ); 569 576 break; … … 571 578 default: 572 579 p->type->base = type; 573 } // switch580 } 574 581 type = 0; 575 } // if582 } 576 583 delete this; 577 584 return p; 578 585 } else { 579 586 return this; 580 } // if587 } 581 588 } 582 589 … … 586 593 while ( cur->base ) { 587 594 cur = cur->base; 588 } // while595 } 589 596 return cur; 590 597 } … … 602 609 if ( type->kind == TypeData::Aggregate ) { 603 610 lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals ); 604 } // if611 } 605 612 lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers ); 606 613 break; 607 614 default: 608 615 lastArray->base = type; 609 } // switch616 } 610 617 type = 0; 611 } // if618 } 612 619 delete this; 613 620 return a; 614 621 } else { 615 622 return this; 616 } // if623 } 617 624 } 618 625 … … 630 637 } else { 631 638 type->function->idList = ids; 632 } // if639 } 633 640 return type; 634 641 } else { … … 636 643 newtype->function->idList = ids; 637 644 return newtype; 638 } // if645 } 639 646 } 640 647 … … 655 662 while ( srcType->base ) { 656 663 srcType = srcType->base; 657 } // while664 } 658 665 newnode->type = maybeClone( srcType ); 659 666 if ( newnode->type->kind == TypeData::AggregateInst ) { … … 666 673 delete newnode->type->aggInst->aggregate->aggregate->members; 667 674 newnode->type->aggInst->aggregate->aggregate->members = 0; 668 } // if669 } // if675 } 676 } 670 677 newnode->type->forall = maybeClone( type->forall ); 671 678 newnode->storageClasses = storageClasses; … … 681 688 while ( srcType->base ) { 682 689 srcType = srcType->base; 683 } // while690 } 684 691 TypeData *newType = srcType->clone(); 685 692 if ( newType->kind == TypeData::AggregateInst ) { … … 692 699 delete newType->aggInst->aggregate->aggregate->members; 693 700 newType->aggInst->aggregate->aggregate->members = 0; 694 } // if695 } // if701 } 702 } 696 703 newType->forall = maybeClone( type->forall ); 697 704 if ( ! o->type ) { … … 700 707 addTypeToType( newType, o->type ); 701 708 delete newType; 702 } // if703 } // if704 } // if709 } 710 } 711 } 705 712 return o; 706 713 } … … 724 731 addTypeToType( newType, o->type ); 725 732 delete newType; 726 } // if727 } // if728 } // if733 } 734 } 735 } 729 736 return o; 730 737 } … … 733 740 if ( node != 0 ) { 734 741 set_link( node ); 735 } // if742 } 736 743 return this; 737 744 } … … 796 803 } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) { 797 804 StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 798 *out++ = new ObjectDecl( "", Declaration Node::NoStorageClass, linkage, 0, inst, 0 );805 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); 799 806 delete agg; 800 807 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) { 801 808 UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 802 *out++ = new ObjectDecl( "", Declaration Node::NoStorageClass, linkage, 0, inst, 0 );809 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); 803 810 } // if 804 811 } // if … … 875 882 } 876 883 877 DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const { 878 DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass; 879 for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) { 880 if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers 881 882 if ( ret != DeclarationNode::NoStorageClass ) { // already have a valid storage class ? 884 Declaration::StorageClass DeclarationNode::buildStorageClass() const { 885 static const Declaration::StorageClass scMap[] = { 886 Declaration::Extern, 887 Declaration::Static, 888 Declaration::Auto, 889 Declaration::Register, 890 Declaration::Inline, 891 Declaration::Fortran 892 }; 893 894 Declaration::StorageClass ret = Declaration::NoStorageClass; 895 for ( std::list< StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) { 896 assert( unsigned( *i ) < sizeof( scMap ) / sizeof( scMap[0] ) ); 897 if ( *i == Inline ) continue; 898 if ( ret != Declaration::NoStorageClass ) { 883 899 throw SemanticError( "invalid combination of storage classes in declaration of ", this ); 884 } // if885 ret = *i;886 } // for900 } 901 ret = scMap[ *i ]; 902 } 887 903 return ret; 888 904 } 889 905 890 906 bool DeclarationNode::buildInline() const { 891 std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), DeclarationNode::Inline );907 std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline ); 892 908 if ( first == storageClasses.end() ) return false; 893 std::list< DeclarationNode::StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), DeclarationNode::Inline );909 std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline ); 894 910 if ( next == storageClasses.end() ) return true; 895 911 throw SemanticError( "duplicate inline specification in declaration of ", this );
Note:
See TracChangeset
for help on using the changeset viewer.