Changeset dd020c0 for src/Parser/DeclarationNode.cc
- Timestamp:
- Mar 3, 2017, 10:12:02 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 8191203
- Parents:
- f37147b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rf37147b 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 23 22:21:06201713 // Update Count : 77512 // 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 ); … … 183 191 } // DeclarationNode::newFunction 184 192 185 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 ) { 186 207 DeclarationNode * newnode = new DeclarationNode; 187 208 newnode->type = new TypeData(); 188 newnode->type-> qualifiers[ q ] = 1;209 newnode->type->typeQualifiers[ tq ] = true; 189 210 return newnode; 190 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 191 240 192 241 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { … … 196 245 return newnode; 197 246 } // DeclarationNode::newForall 198 199 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {200 DeclarationNode * newnode = new DeclarationNode;201 newnode->storageClass = sc;202 return newnode;203 } // DeclarationNode::newStorageClass204 205 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {206 DeclarationNode * newnode = new DeclarationNode;207 newnode->type = new TypeData( TypeData::Basic );208 newnode->type->basictype = bt;209 return newnode;210 } // DeclarationNode::newBasicType211 212 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {213 DeclarationNode * newnode = new DeclarationNode;214 newnode->type = new TypeData( TypeData::Basic );215 newnode->type->complextype = ct;216 return newnode;217 } // DeclarationNode::newComplexType218 219 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {220 DeclarationNode * newnode = new DeclarationNode;221 newnode->type = new TypeData( TypeData::Basic );222 newnode->type->signedness = sn;223 return newnode;224 } // DeclarationNode::newSignedNess225 226 DeclarationNode * DeclarationNode::newLength( Length lnth ) {227 DeclarationNode * newnode = new DeclarationNode;228 newnode->type = new TypeData( TypeData::Basic );229 newnode->type->length = lnth;230 return newnode;231 } // DeclarationNode::newLength232 247 233 248 DeclarationNode * DeclarationNode::newFromTypedef( string * name ) { … … 428 443 429 444 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { 430 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization431 432 if ( (qsrc & qdst).any() ) { // common qualifier ?433 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 434 449 if ( qsrc[i] && qdst[i] ) { 435 appendError( error, string( "duplicate " ) + DeclarationNode:: qualifierName[i] );450 appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] ); 436 451 } // if 437 452 } // for … … 440 455 441 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 442 466 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { 443 467 if ( storageClass == q->storageClass ) { // duplicate qualifier 444 appendError( error, string( "duplicate " ) + storage Name[ storageClass ] );468 appendError( error, string( "duplicate " ) + storageClassNames[ storageClass ] ); 445 469 } else { // only one storage class 446 appendError( error, string( "conflicting " ) + storage Name[ storageClass ] + " & " + storageName[ q->storageClass ] );470 appendError( error, string( "conflicting " ) + storageClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] ); 447 471 q->storageClass = storageClass; // FIX ERROR, prevent assertions from triggering 448 472 } // if 449 473 } // if 474 450 475 appendError( error, q->error ); 451 476 } // DeclarationNode::checkStorageClasses 452 477 453 478 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) { 454 isInline = isInline || q->isInline;455 isNoreturn = isNoreturn || q->isNoreturn; 479 funcSpec = funcSpec | q->funcSpec; 480 456 481 // do not overwrite an existing value with NoStorageClass 457 482 if ( q->storageClass != NoStorageClass ) { … … 481 506 src = nullptr; 482 507 } else { 483 dst-> qualifiers |= src->qualifiers;508 dst->typeQualifiers |= src->typeQualifiers; 484 509 } // if 485 510 } // addQualifiersToType … … 539 564 switch ( dst->kind ) { 540 565 case TypeData::Unknown: 541 src-> qualifiers |= dst->qualifiers;566 src->typeQualifiers |= dst->typeQualifiers; 542 567 dst = src; 543 568 src = nullptr; 544 569 break; 545 570 case TypeData::Basic: 546 dst-> qualifiers |= src->qualifiers;571 dst->typeQualifiers |= src->typeQualifiers; 547 572 if ( src->kind != TypeData::Unknown ) { 548 573 assert( src->kind == TypeData::Basic ); … … 551 576 dst->basictype = src->basictype; 552 577 } else if ( src->basictype != DeclarationNode::NoBasicType ) 553 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 ); 554 579 555 580 if ( dst->complextype == DeclarationNode::NoComplexType ) { 556 581 dst->complextype = src->complextype; 557 582 } else if ( src->complextype != DeclarationNode::NoComplexType ) 558 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 ); 559 584 560 585 if ( dst->signedness == DeclarationNode::NoSignedness ) { 561 586 dst->signedness = src->signedness; 562 587 } else if ( src->signedness != DeclarationNode::NoSignedness ) 563 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 ); 564 589 565 590 if ( dst->length == DeclarationNode::NoLength ) { … … 568 593 dst->length = DeclarationNode::LongLong; 569 594 } else if ( src->length != DeclarationNode::NoLength ) 570 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 ); 571 596 } // if 572 597 break; … … 580 605 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 581 606 } // if 582 dst->base-> qualifiers |= src->qualifiers;607 dst->base->typeQualifiers |= src->typeQualifiers; 583 608 src = nullptr; 584 609 break; … … 612 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; … … 768 793 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); 769 794 } // if 770 p->type->base-> qualifiers |= type->qualifiers;795 p->type->base->typeQualifiers |= type->typeQualifiers; 771 796 break; 772 797 … … 805 830 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); 806 831 } // if 807 lastArray->base-> qualifiers |= type->qualifiers;832 lastArray->base->typeQualifiers |= type->typeQualifiers; 808 833 break; 809 834 default: … … 1005 1030 } // if 1006 1031 1007 // if ( variable.name ) {1008 1032 if ( variable.tyClass != NoTypeClass ) { 1009 1033 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype }; 1010 1034 assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." ); 1011 1035 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); 1012 // TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );1013 1036 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); 1014 1037 buildList( variable.assertions, ret->get_assertions() ); … … 1017 1040 1018 1041 if ( type ) { 1019 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); 1020 } // if 1021 1022 if ( ! isInline && ! isNoreturn ) { 1023 assertf( name, "ObjectDecl are assumed to have names\n" ); 1024 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); 1025 } // if 1026 1027 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 ); 1028 1062 } 1029 1063 … … 1032 1066 1033 1067 if ( attr.expr ) { 1034 // return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );1035 1068 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes ); 1036 1069 } else if ( attr.type ) { 1037 // return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );1038 1070 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes ); 1039 1071 } // if
Note: See TracChangeset
for help on using the changeset viewer.