Changes in src/Parser/DeclarationNode.cc [dd020c0:3a5131ed]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rdd020c0 r3a5131ed 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 3 21:38:34201713 // Update Count : 86212 // Last Modified On : Thu Feb 16 13:06:50 2017 13 // Update Count : 753 14 14 // 15 15 … … 32 32 33 33 // These must remain in the same order as the corresponding DeclarationNode enumerations. 34 const char * DeclarationNode::storageClassNames[] = { "extern", "static", "auto", "register", "_Thread_local", "inline", "fortran", "_Noreturn", "NoStorageClassNames" }; 35 const char * DeclarationNode::funcSpecifierNames[] = { "inline", "fortran", "_Noreturn", "NoFunctionSpecifierNames" }; 36 const char * DeclarationNode::typeQualifierNames[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoTypeQualifierNames" }; 37 const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicTypeNames" }; 38 const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" }; 39 const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" }; 40 const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" }; 41 const char * DeclarationNode::aggregateNames[] = { "struct", "union", "context", "NoAggregateNames" }; 42 const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" }; 43 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "NoBuiltinTypeNames" }; 34 const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" }; 35 const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" }; 36 const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" }; 37 const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" }; 38 const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" }; 39 const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" }; 40 const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" }; 41 const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" }; 42 const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" }; 44 43 45 44 UniqueName DeclarationNode::anonymous( "__anonymous" ); … … 51 50 storageClass( NoStorageClass ), 52 51 bitfieldWidth( nullptr ), 52 isInline( false ), 53 isNoreturn( false ), 53 54 hasEllipsis( false ), 54 55 linkage( ::linkage ), … … 91 92 newnode->storageClass = storageClass; 92 93 newnode->bitfieldWidth = maybeClone( bitfieldWidth ); 93 newnode->funcSpec = funcSpec; 94 newnode->isInline = isInline; 95 newnode->isNoreturn = isNoreturn; 94 96 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) ); 95 97 newnode->hasEllipsis = hasEllipsis; … … 116 118 } 117 119 118 void DeclarationNode::print_FuncSpec( std::ostream & output, DeclarationNode::FuncSpec funcSpec ) {119 if ( funcSpec.any() ) { // function specifiers?120 for ( unsigned int i = 0; i < DeclarationNode::NoFuncSpecifier; i += 1 ) {121 if ( funcSpec[i] ) {122 output << DeclarationNode::funcSpecifierNames[i] << ' ';123 } // if124 } // for125 } // if126 } // print_FuncSpec127 128 120 void DeclarationNode::print( std::ostream &os, int indent ) const { 129 121 os << string( indent, ' ' ); … … 138 130 } // if 139 131 140 if ( storageClass != NoStorageClass ) os << DeclarationNode::storage ClassNames[storageClass] << ' ';141 print_FuncSpec( os, funcSpec );142 132 if ( storageClass != NoStorageClass ) os << DeclarationNode::storageName[storageClass] << ' '; 133 if ( isInline ) os << DeclarationNode::storageName[Inline] << ' '; 134 if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' '; 143 135 if ( type ) { 144 136 type->print( os, indent ); … … 182 174 } // if 183 175 176 if ( body ) { 177 newnode->type->function.hasBody = true; 178 } // if 179 184 180 if ( ret ) { 185 181 newnode->type->base = ret->type; … … 191 187 } // DeclarationNode::newFunction 192 188 189 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { 190 DeclarationNode * newnode = new DeclarationNode; 191 newnode->type = new TypeData(); 192 newnode->type->qualifiers[ q ] = 1; 193 return newnode; 194 } // DeclarationNode::newQualifier 195 196 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { 197 DeclarationNode * newnode = new DeclarationNode; 198 newnode->type = new TypeData( TypeData::Unknown ); 199 newnode->type->forall = forall; 200 return newnode; 201 } // DeclarationNode::newForall 193 202 194 203 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { … … 197 206 return newnode; 198 207 } // DeclarationNode::newStorageClass 199 200 DeclarationNode * DeclarationNode::newFuncSpecifier( DeclarationNode::FuncSpecifier fs ) {201 DeclarationNode * newnode = new DeclarationNode;202 newnode->funcSpec[ fs ] = true;203 return newnode;204 } // DeclarationNode::newFuncSpecifier205 206 DeclarationNode * DeclarationNode::newTypeQualifier( TypeQualifier tq ) {207 DeclarationNode * newnode = new DeclarationNode;208 newnode->type = new TypeData();209 newnode->type->typeQualifiers[ tq ] = true;210 return newnode;211 } // DeclarationNode::newQualifier212 208 213 209 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { … … 238 234 return newnode; 239 235 } // DeclarationNode::newLength 240 241 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {242 DeclarationNode * newnode = new DeclarationNode;243 newnode->type = new TypeData( TypeData::Unknown );244 newnode->type->forall = forall;245 return newnode;246 } // DeclarationNode::newForall247 236 248 237 DeclarationNode * DeclarationNode::newFromTypedef( string * name ) { … … 270 259 } // DeclarationNode::newAggregate 271 260 272 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants , bool body) {261 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants ) { 273 262 DeclarationNode * newnode = new DeclarationNode; 274 263 newnode->type = new TypeData( TypeData::Enum ); … … 279 268 } // if 280 269 newnode->type->enumeration.constants = constants; 281 newnode->type->enumeration.body = body;282 270 return newnode; 283 271 } // DeclarationNode::newEnum … … 443 431 444 432 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { 445 const TypeData::TypeQualifiers &qsrc = src->typeQualifiers, &qdst = dst->typeQualifiers; // optimization446 447 if ( (qsrc & qdst).any() ) { // common qualifier ?448 for ( unsigned int i = 0; i < NoTypeQualifier; i += 1 ) {// find common qualifiers433 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization 434 435 if ( (qsrc & qdst).any() ) { // common qualifier ? 436 for ( int i = 0; i < NoQualifier; i += 1 ) { // find common qualifiers 449 437 if ( qsrc[i] && qdst[i] ) { 450 appendError( error, string( "duplicate " ) + DeclarationNode:: typeQualifierNames[i] );438 appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] ); 451 439 } // if 452 440 } // for … … 455 443 456 444 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) { 457 const FuncSpec &src = funcSpec, &dst = q->funcSpec; // optimization458 if ( (src & dst).any() ) { // common specifier ?459 for ( unsigned int i = 0; i < NoFuncSpecifier; i += 1 ) { // find common specifier460 if ( src[i] && dst[i] ) {461 appendError( error, string( "duplicate " ) + DeclarationNode::funcSpecifierNames[i] );462 } // if463 } // for464 } // if465 466 445 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { 467 446 if ( storageClass == q->storageClass ) { // duplicate qualifier 468 appendError( error, string( "duplicate " ) + storage ClassNames[ storageClass ] );447 appendError( error, string( "duplicate " ) + storageName[ storageClass ] ); 469 448 } else { // only one storage class 470 appendError( error, string( "conflicting " ) + storage ClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] );449 appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] ); 471 450 q->storageClass = storageClass; // FIX ERROR, prevent assertions from triggering 472 451 } // if 473 452 } // if 474 475 453 appendError( error, q->error ); 476 454 } // DeclarationNode::checkStorageClasses 477 455 478 456 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) { 479 funcSpec = funcSpec | q->funcSpec;480 457 isInline = isInline || q->isInline; 458 isNoreturn = isNoreturn || q->isNoreturn; 481 459 // do not overwrite an existing value with NoStorageClass 482 460 if ( q->storageClass != NoStorageClass ) { … … 506 484 src = nullptr; 507 485 } else { 508 dst-> typeQualifiers |= src->typeQualifiers;486 dst->qualifiers |= src->qualifiers; 509 487 } // if 510 488 } // addQualifiersToType … … 564 542 switch ( dst->kind ) { 565 543 case TypeData::Unknown: 566 src-> typeQualifiers |= dst->typeQualifiers;544 src->qualifiers |= dst->qualifiers; 567 545 dst = src; 568 546 src = nullptr; 569 547 break; 570 548 case TypeData::Basic: 571 dst-> typeQualifiers |= src->typeQualifiers;549 dst->qualifiers |= src->qualifiers; 572 550 if ( src->kind != TypeData::Unknown ) { 573 551 assert( src->kind == TypeData::Basic ); … … 576 554 dst->basictype = src->basictype; 577 555 } else if ( src->basictype != DeclarationNode::NoBasicType ) 578 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName s[ src->basictype ] + " in type: ", src );556 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src ); 579 557 580 558 if ( dst->complextype == DeclarationNode::NoComplexType ) { 581 559 dst->complextype = src->complextype; 582 560 } else if ( src->complextype != DeclarationNode::NoComplexType ) 583 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName s[ src->complextype ] + " in type: ", src );561 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src ); 584 562 585 563 if ( dst->signedness == DeclarationNode::NoSignedness ) { 586 564 dst->signedness = src->signedness; 587 565 } else if ( src->signedness != DeclarationNode::NoSignedness ) 588 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName s[ src->signedness ] + " in type: ", src );566 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src ); 589 567 590 568 if ( dst->length == DeclarationNode::NoLength ) { … … 593 571 dst->length = DeclarationNode::LongLong; 594 572 } else if ( src->length != DeclarationNode::NoLength ) 595 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName s[ src->length ] + " in type: ", src );573 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src ); 596 574 } // if 597 575 break; … … 605 583 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 606 584 } // if 607 dst->base-> typeQualifiers |= src->typeQualifiers;585 dst->base->qualifiers |= src->qualifiers; 608 586 src = nullptr; 609 587 break; … … 632 610 type->aggInst.aggregate = o->type; 633 611 if ( o->type->kind == TypeData::Aggregate ) { 634 type->aggInst.hoistType = o->type->aggregate.body;635 612 type->aggInst.params = maybeClone( o->type->aggregate.actuals ); 636 } else {637 type->aggInst.hoistType = o->type->enumeration.body;638 613 } // if 639 type-> typeQualifiers |= o->type->typeQualifiers;614 type->qualifiers |= o->type->qualifiers; 640 615 } else { 641 616 type = o->type; … … 723 698 assert( ! type->function.body ); 724 699 type->function.body = body; 700 type->function.hasBody = true; 725 701 return this; 726 702 } … … 793 769 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); 794 770 } // if 795 p->type->base-> typeQualifiers |= type->typeQualifiers;771 p->type->base->qualifiers |= type->qualifiers; 796 772 break; 797 773 … … 830 806 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); 831 807 } // if 832 lastArray->base-> typeQualifiers |= type->typeQualifiers;808 lastArray->base->qualifiers |= type->qualifiers; 833 809 break; 834 810 default: … … 909 885 newType->aggInst.aggregate->aggregate.fields = nullptr; 910 886 } // if 911 // don't hoist twice912 newType->aggInst.hoistType = false;913 887 } // if 914 888 … … 971 945 SemanticError errors; 972 946 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 973 947 974 948 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 975 949 try { … … 983 957 auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); 984 958 obj->location = cur->location; 985 * out++ = obj; 959 * out++ = obj; 986 960 delete agg; 987 961 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { … … 1030 1004 } // if 1031 1005 1006 // if ( variable.name ) { 1032 1007 if ( variable.tyClass != NoTypeClass ) { 1033 1008 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype }; 1034 1009 assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." ); 1035 1010 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); 1011 // TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); 1036 1012 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); 1037 1013 buildList( variable.assertions, ret->get_assertions() ); … … 1040 1016 1041 1017 if ( type ) { 1042 // Function specifiers can only appear on a function definition/declaration. 1043 // 1044 // inline _Noreturn int f(); // allowed 1045 // inline _Noreturn int g( int i ); // allowed 1046 // inline _Noreturn int i; // disallowed 1047 if ( type->kind != TypeData::Function && funcSpec.any() ) { 1048 throw SemanticError( "invalid function specifier for ", this ); 1049 } // if 1050 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), funcSpec, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); 1051 } // if 1052 1053 // SUE's cannot have function specifiers, either 1054 // 1055 // inlne _Noreturn struct S { ... }; // disallowed 1056 // inlne _Noreturn enum E { ... }; // disallowed 1057 if ( funcSpec.any() ) { 1058 throw SemanticError( "invalid function specifier for ", this ); 1059 } // if 1060 assertf( name, "ObjectDecl must a have name\n" ); 1061 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); 1018 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); 1019 } // if 1020 1021 if ( ! isInline && ! isNoreturn ) { 1022 assertf( name, "ObjectDecl are assumed to have names\n" ); 1023 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); 1024 } // if 1025 1026 throw SemanticError( "invalid function specifier ", this ); 1062 1027 } 1063 1028 … … 1066 1031 1067 1032 if ( attr.expr ) { 1033 // return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() ); 1068 1034 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes ); 1069 1035 } else if ( attr.type ) { 1036 // return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() ); 1070 1037 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes ); 1071 1038 } // if 1072 1039 1073 1040 switch ( type->kind ) { 1074 case TypeData::Enum: 1041 case TypeData::Enum: { 1042 EnumDecl * typedecl = buildEnum( type, attributes ); 1043 return new EnumInstType( buildQualifiers( type ), typedecl ); 1044 } 1075 1045 case TypeData::Aggregate: { 1076 ReferenceToType * ret = buildComAggInst( type, attributes ); 1046 AggregateDecl * typedecl = buildAggregate( type, attributes ); 1047 ReferenceToType * ret; 1048 switch ( type->aggregate.kind ) { 1049 case DeclarationNode::Struct: 1050 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl ); 1051 break; 1052 case DeclarationNode::Union: 1053 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl ); 1054 break; 1055 case DeclarationNode::Trait: 1056 assert( false ); 1057 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl ); 1058 break; 1059 default: 1060 assert( false ); 1061 } // switch 1077 1062 buildList( type->aggregate.actuals, ret->get_parameters() ); 1078 1063 return ret;
Note:
See TracChangeset
for help on using the changeset viewer.