Changes in src/Parser/DeclarationNode.cc [3a5131ed:dd020c0]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (28 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r3a5131ed rdd020c0 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Feb 16 13:06:50201713 // Update Count : 75312 // Last Modified On : Fri Mar 3 21:38:34 2017 13 // Update Count : 862 14 14 // 15 15 … … 32 32 33 33 // These must remain in the same order as the corresponding DeclarationNode enumerations. 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" }; 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" }; 43 44 44 45 UniqueName DeclarationNode::anonymous( "__anonymous" ); … … 50 51 storageClass( NoStorageClass ), 51 52 bitfieldWidth( nullptr ), 52 isInline( false ),53 isNoreturn( false ),54 53 hasEllipsis( false ), 55 54 linkage( ::linkage ), … … 92 91 newnode->storageClass = storageClass; 93 92 newnode->bitfieldWidth = maybeClone( bitfieldWidth ); 94 newnode->isInline = isInline; 95 newnode->isNoreturn = isNoreturn; 93 newnode->funcSpec = funcSpec; 96 94 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) ); 97 95 newnode->hasEllipsis = hasEllipsis; … … 118 116 } 119 117 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 } // if 124 } // for 125 } // if 126 } // print_FuncSpec 127 120 128 void DeclarationNode::print( std::ostream &os, int indent ) const { 121 129 os << string( indent, ' ' ); … … 130 138 } // if 131 139 132 if ( storageClass != NoStorageClass ) os << DeclarationNode::storage Name[storageClass] << ' ';133 if ( isInline ) os << DeclarationNode::storageName[Inline] << ' ';134 if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' '; 140 if ( storageClass != NoStorageClass ) os << DeclarationNode::storageClassNames[storageClass] << ' '; 141 print_FuncSpec( os, funcSpec ); 142 135 143 if ( type ) { 136 144 type->print( os, indent ); … … 174 182 } // if 175 183 176 if ( body ) {177 newnode->type->function.hasBody = true;178 } // if179 180 184 if ( ret ) { 181 185 newnode->type->base = ret->type; … … 187 191 } // DeclarationNode::newFunction 188 192 189 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { 193 194 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { 195 DeclarationNode * newnode = new DeclarationNode; 196 newnode->storageClass = sc; 197 return newnode; 198 } // 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::newFuncSpecifier 205 206 DeclarationNode * DeclarationNode::newTypeQualifier( TypeQualifier tq ) { 190 207 DeclarationNode * newnode = new DeclarationNode; 191 208 newnode->type = new TypeData(); 192 newnode->type-> qualifiers[ q ] = 1;209 newnode->type->typeQualifiers[ tq ] = true; 193 210 return newnode; 194 211 } // DeclarationNode::newQualifier 212 213 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { 214 DeclarationNode * newnode = new DeclarationNode; 215 newnode->type = new TypeData( TypeData::Basic ); 216 newnode->type->basictype = bt; 217 return newnode; 218 } // DeclarationNode::newBasicType 219 220 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) { 221 DeclarationNode * newnode = new DeclarationNode; 222 newnode->type = new TypeData( TypeData::Basic ); 223 newnode->type->complextype = ct; 224 return newnode; 225 } // DeclarationNode::newComplexType 226 227 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) { 228 DeclarationNode * newnode = new DeclarationNode; 229 newnode->type = new TypeData( TypeData::Basic ); 230 newnode->type->signedness = sn; 231 return newnode; 232 } // DeclarationNode::newSignedNess 233 234 DeclarationNode * DeclarationNode::newLength( Length lnth ) { 235 DeclarationNode * newnode = new DeclarationNode; 236 newnode->type = new TypeData( TypeData::Basic ); 237 newnode->type->length = lnth; 238 return newnode; 239 } // DeclarationNode::newLength 195 240 196 241 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { … … 200 245 return newnode; 201 246 } // DeclarationNode::newForall 202 203 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {204 DeclarationNode * newnode = new DeclarationNode;205 newnode->storageClass = sc;206 return newnode;207 } // DeclarationNode::newStorageClass208 209 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {210 DeclarationNode * newnode = new DeclarationNode;211 newnode->type = new TypeData( TypeData::Basic );212 newnode->type->basictype = bt;213 return newnode;214 } // DeclarationNode::newBasicType215 216 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {217 DeclarationNode * newnode = new DeclarationNode;218 newnode->type = new TypeData( TypeData::Basic );219 newnode->type->complextype = ct;220 return newnode;221 } // DeclarationNode::newComplexType222 223 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {224 DeclarationNode * newnode = new DeclarationNode;225 newnode->type = new TypeData( TypeData::Basic );226 newnode->type->signedness = sn;227 return newnode;228 } // DeclarationNode::newSignedNess229 230 DeclarationNode * DeclarationNode::newLength( Length lnth ) {231 DeclarationNode * newnode = new DeclarationNode;232 newnode->type = new TypeData( TypeData::Basic );233 newnode->type->length = lnth;234 return newnode;235 } // DeclarationNode::newLength236 247 237 248 DeclarationNode * DeclarationNode::newFromTypedef( string * name ) { … … 259 270 } // DeclarationNode::newAggregate 260 271 261 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants ) {272 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) { 262 273 DeclarationNode * newnode = new DeclarationNode; 263 274 newnode->type = new TypeData( TypeData::Enum ); … … 268 279 } // if 269 280 newnode->type->enumeration.constants = constants; 281 newnode->type->enumeration.body = body; 270 282 return newnode; 271 283 } // DeclarationNode::newEnum … … 431 443 432 444 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { 433 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization434 435 if ( (qsrc & qdst).any() ) { // common qualifier ?436 for ( int i = 0; i < NoQualifier; i += 1 ) {// find common qualifiers445 const TypeData::TypeQualifiers &qsrc = src->typeQualifiers, &qdst = dst->typeQualifiers; // optimization 446 447 if ( (qsrc & qdst).any() ) { // common qualifier ? 448 for ( unsigned int i = 0; i < NoTypeQualifier; i += 1 ) { // find common qualifiers 437 449 if ( qsrc[i] && qdst[i] ) { 438 appendError( error, string( "duplicate " ) + DeclarationNode:: qualifierName[i] );450 appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] ); 439 451 } // if 440 452 } // for … … 443 455 444 456 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) { 457 const FuncSpec &src = funcSpec, &dst = q->funcSpec; // optimization 458 if ( (src & dst).any() ) { // common specifier ? 459 for ( unsigned int i = 0; i < NoFuncSpecifier; i += 1 ) { // find common specifier 460 if ( src[i] && dst[i] ) { 461 appendError( error, string( "duplicate " ) + DeclarationNode::funcSpecifierNames[i] ); 462 } // if 463 } // for 464 } // if 465 445 466 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { 446 467 if ( storageClass == q->storageClass ) { // duplicate qualifier 447 appendError( error, string( "duplicate " ) + storage Name[ storageClass ] );468 appendError( error, string( "duplicate " ) + storageClassNames[ storageClass ] ); 448 469 } else { // only one storage class 449 appendError( error, string( "conflicting " ) + storage Name[ storageClass ] + " & " + storageName[ q->storageClass ] );470 appendError( error, string( "conflicting " ) + storageClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] ); 450 471 q->storageClass = storageClass; // FIX ERROR, prevent assertions from triggering 451 472 } // if 452 473 } // if 474 453 475 appendError( error, q->error ); 454 476 } // DeclarationNode::checkStorageClasses 455 477 456 478 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) { 457 isInline = isInline || q->isInline;458 isNoreturn = isNoreturn || q->isNoreturn; 479 funcSpec = funcSpec | q->funcSpec; 480 459 481 // do not overwrite an existing value with NoStorageClass 460 482 if ( q->storageClass != NoStorageClass ) { … … 484 506 src = nullptr; 485 507 } else { 486 dst-> qualifiers |= src->qualifiers;508 dst->typeQualifiers |= src->typeQualifiers; 487 509 } // if 488 510 } // addQualifiersToType … … 542 564 switch ( dst->kind ) { 543 565 case TypeData::Unknown: 544 src-> qualifiers |= dst->qualifiers;566 src->typeQualifiers |= dst->typeQualifiers; 545 567 dst = src; 546 568 src = nullptr; 547 569 break; 548 570 case TypeData::Basic: 549 dst-> qualifiers |= src->qualifiers;571 dst->typeQualifiers |= src->typeQualifiers; 550 572 if ( src->kind != TypeData::Unknown ) { 551 573 assert( src->kind == TypeData::Basic ); … … 554 576 dst->basictype = src->basictype; 555 577 } else if ( src->basictype != DeclarationNode::NoBasicType ) 556 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName [ src->basictype ] + " in type: ", src );578 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src ); 557 579 558 580 if ( dst->complextype == DeclarationNode::NoComplexType ) { 559 581 dst->complextype = src->complextype; 560 582 } else if ( src->complextype != DeclarationNode::NoComplexType ) 561 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName [ src->complextype ] + " in type: ", src );583 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src ); 562 584 563 585 if ( dst->signedness == DeclarationNode::NoSignedness ) { 564 586 dst->signedness = src->signedness; 565 587 } else if ( src->signedness != DeclarationNode::NoSignedness ) 566 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName [ src->signedness ] + " in type: ", src );588 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src ); 567 589 568 590 if ( dst->length == DeclarationNode::NoLength ) { … … 571 593 dst->length = DeclarationNode::LongLong; 572 594 } else if ( src->length != DeclarationNode::NoLength ) 573 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName [ src->length ] + " in type: ", src );595 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src ); 574 596 } // if 575 597 break; … … 583 605 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 584 606 } // if 585 dst->base-> qualifiers |= src->qualifiers;607 dst->base->typeQualifiers |= src->typeQualifiers; 586 608 src = nullptr; 587 609 break; … … 610 632 type->aggInst.aggregate = o->type; 611 633 if ( o->type->kind == TypeData::Aggregate ) { 634 type->aggInst.hoistType = o->type->aggregate.body; 612 635 type->aggInst.params = maybeClone( o->type->aggregate.actuals ); 636 } else { 637 type->aggInst.hoistType = o->type->enumeration.body; 613 638 } // if 614 type-> qualifiers |= o->type->qualifiers;639 type->typeQualifiers |= o->type->typeQualifiers; 615 640 } else { 616 641 type = o->type; … … 698 723 assert( ! type->function.body ); 699 724 type->function.body = body; 700 type->function.hasBody = true;701 725 return this; 702 726 } … … 769 793 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); 770 794 } // if 771 p->type->base-> qualifiers |= type->qualifiers;795 p->type->base->typeQualifiers |= type->typeQualifiers; 772 796 break; 773 797 … … 806 830 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); 807 831 } // if 808 lastArray->base-> qualifiers |= type->qualifiers;832 lastArray->base->typeQualifiers |= type->typeQualifiers; 809 833 break; 810 834 default: … … 885 909 newType->aggInst.aggregate->aggregate.fields = nullptr; 886 910 } // if 911 // don't hoist twice 912 newType->aggInst.hoistType = false; 887 913 } // if 888 914 … … 945 971 SemanticError errors; 946 972 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 947 973 948 974 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 949 975 try { … … 957 983 auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); 958 984 obj->location = cur->location; 959 * out++ = obj; 985 * out++ = obj; 960 986 delete agg; 961 987 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { … … 1004 1030 } // if 1005 1031 1006 // if ( variable.name ) {1007 1032 if ( variable.tyClass != NoTypeClass ) { 1008 1033 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype }; 1009 1034 assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." ); 1010 1035 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 ] );1012 1036 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); 1013 1037 buildList( variable.assertions, ret->get_assertions() ); … … 1016 1040 1017 1041 if ( type ) { 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 ); 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 ); 1027 1062 } 1028 1063 … … 1031 1066 1032 1067 if ( attr.expr ) { 1033 // return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );1034 1068 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes ); 1035 1069 } else if ( attr.type ) { 1036 // return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );1037 1070 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes ); 1038 1071 } // if 1039 1072 1040 1073 switch ( type->kind ) { 1041 case TypeData::Enum: { 1042 EnumDecl * typedecl = buildEnum( type, attributes ); 1043 return new EnumInstType( buildQualifiers( type ), typedecl ); 1044 } 1074 case TypeData::Enum: 1045 1075 case TypeData::Aggregate: { 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 1076 ReferenceToType * ret = buildComAggInst( type, attributes ); 1062 1077 buildList( type->aggregate.actuals, ret->get_parameters() ); 1063 1078 return ret;
Note:
See TracChangeset
for help on using the changeset viewer.