Changes in src/Parser/DeclarationNode.cc [5b639ee:2298f728]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (53 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r5b639ee r2298f728 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Sep 12 21:03:18201613 // Update Count : 49112 // Last Modified On : Sat Sep 24 11:12:52 2016 13 // Update Count : 627 14 14 // 15 15 … … 31 31 32 32 // These must remain in the same order as the corresponding DeclarationNode enumerations. 33 const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" };34 const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" };35 const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" };36 const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" };37 const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" };38 const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" };39 const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" };40 const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" };41 const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };33 const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" }; 34 const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" }; 35 const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" }; 36 const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" }; 37 const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" }; 38 const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" }; 39 const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" }; 40 const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" }; 41 const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" }; 42 42 43 43 UniqueName DeclarationNode::anonymous( "__anonymous" ); … … 46 46 47 47 DeclarationNode::DeclarationNode() : 48 type( 0),48 type( nullptr ), 49 49 storageClass( NoStorageClass ), 50 50 isInline( false ), 51 51 isNoreturn( false ), 52 bitfieldWidth( 0),53 initializer( 0),52 bitfieldWidth( nullptr ), 53 initializer( nullptr ), 54 54 hasEllipsis( false ), 55 55 linkage( ::linkage ), 56 56 extension( false ) { 57 58 variable.name = nullptr; 57 59 variable.tyClass = DeclarationNode::Otype; 58 60 variable.assertions = nullptr; 59 61 62 attr.name = nullptr; 60 63 attr.expr = nullptr; 61 64 attr.type = nullptr; … … 63 66 64 67 DeclarationNode::~DeclarationNode() { 68 delete attr.name; 65 69 delete attr.expr; 66 70 delete attr.type; 71 72 delete variable.name; 73 delete variable.assertions; 74 67 75 delete type; 68 76 delete bitfieldWidth; … … 70 78 } 71 79 72 DeclarationNode * DeclarationNode::clone() const {73 DeclarationNode * newnode = new DeclarationNode;80 DeclarationNode * DeclarationNode::clone() const { 81 DeclarationNode * newnode = new DeclarationNode; 74 82 newnode->type = maybeClone( type ); 75 newnode->name = name ;83 newnode->name = name ? new string( *name ) : nullptr; 76 84 newnode->storageClass = storageClass; 77 85 newnode->isInline = isInline; … … 83 91 newnode->linkage = linkage; 84 92 93 newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr; 85 94 newnode->variable.assertions = maybeClone( variable.assertions ); 86 newnode->variable.name = variable.name;87 95 newnode->variable.tyClass = variable.tyClass; 88 96 97 newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr; 89 98 newnode->attr.expr = maybeClone( attr.expr ); 90 99 newnode->attr.type = maybeClone( attr.type ); … … 98 107 void DeclarationNode::print( std::ostream &os, int indent ) const { 99 108 os << string( indent, ' ' ); 100 if ( name == "" ) { 109 if ( name ) { 110 os << *name << ": "; 111 } else { 101 112 os << "unnamed: "; 102 } else {103 os << name << ": ";104 113 } // if 105 114 … … 122 131 } // if 123 132 124 if ( initializer != 0) {133 if ( initializer ) { 125 134 os << endl << string( indent + 2, ' ' ) << "with initializer "; 126 135 initializer->printOneLine( os ); … … 139 148 } 140 149 141 DeclarationNode * DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {142 DeclarationNode * newnode = new DeclarationNode;143 newnode->name = assign_strptr( name );150 DeclarationNode * DeclarationNode::newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) { 151 DeclarationNode * newnode = new DeclarationNode; 152 newnode->name = name; 144 153 145 154 newnode->type = new TypeData( TypeData::Function ); … … 147 156 newnode->type->function.newStyle = newStyle; 148 157 newnode->type->function.body = body; 149 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID ); 158 // ignore unnamed routine declarations: void p( int (*)(int) ); 159 if ( newnode->name ) { 160 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); 161 } // if 150 162 151 163 if ( body ) { … … 155 167 if ( ret ) { 156 168 newnode->type->base = ret->type; 157 ret->type = 0;169 ret->type = nullptr; 158 170 delete ret; 159 171 } // if … … 163 175 164 176 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { 165 DeclarationNode * newnode = new DeclarationNode;177 DeclarationNode * newnode = new DeclarationNode; 166 178 newnode->type = new TypeData(); 167 179 newnode->type->qualifiers[ q ] = 1; … … 169 181 } // DeclarationNode::newQualifier 170 182 171 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {172 DeclarationNode * newnode = new DeclarationNode;183 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { 184 DeclarationNode * newnode = new DeclarationNode; 173 185 newnode->type = new TypeData( TypeData::Unknown ); 174 186 newnode->type->forall = forall; … … 177 189 178 190 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { 179 DeclarationNode *newnode = new DeclarationNode; 180 //switch (sc) { 181 // case Inline: newnode->isInline = true; break; 182 // case Noreturn: newnode->isNoreturn = true; break; 183 // default: newnode->storageClass = sc; break; 184 //} 191 DeclarationNode * newnode = new DeclarationNode; 185 192 newnode->storageClass = sc; 186 193 return newnode; … … 188 195 189 196 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { 190 DeclarationNode * newnode = new DeclarationNode;197 DeclarationNode * newnode = new DeclarationNode; 191 198 newnode->type = new TypeData( TypeData::Basic ); 192 199 newnode->type->basictype = bt; … … 195 202 196 203 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) { 197 DeclarationNode * newnode = new DeclarationNode;204 DeclarationNode * newnode = new DeclarationNode; 198 205 newnode->type = new TypeData( TypeData::Basic ); 199 206 newnode->type->complextype = ct; … … 202 209 203 210 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) { 204 DeclarationNode * newnode = new DeclarationNode;211 DeclarationNode * newnode = new DeclarationNode; 205 212 newnode->type = new TypeData( TypeData::Basic ); 206 213 newnode->type->signedness = sn; … … 209 216 210 217 DeclarationNode * DeclarationNode::newLength( Length lnth ) { 211 DeclarationNode * newnode = new DeclarationNode;218 DeclarationNode * newnode = new DeclarationNode; 212 219 newnode->type = new TypeData( TypeData::Basic ); 213 220 newnode->type->length = lnth; … … 215 222 } // DeclarationNode::newLength 216 223 217 DeclarationNode * DeclarationNode::newFromTypedef( std::string * name ) {218 DeclarationNode * newnode = new DeclarationNode;224 DeclarationNode * DeclarationNode::newFromTypedef( std::string * name ) { 225 DeclarationNode * newnode = new DeclarationNode; 219 226 newnode->type = new TypeData( TypeData::SymbolicInst ); 220 newnode->type->symbolic.name = assign_strptr( name );227 newnode->type->symbolic.name = name ? new string( *name ) : nullptr; 221 228 newnode->type->symbolic.isTypedef = true; 222 newnode->type->symbolic.params = 0;229 newnode->type->symbolic.params = nullptr; 223 230 return newnode; 224 231 } // DeclarationNode::newFromTypedef 225 232 226 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string * name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {227 DeclarationNode * newnode = new DeclarationNode;233 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { 234 DeclarationNode * newnode = new DeclarationNode; 228 235 newnode->type = new TypeData( TypeData::Aggregate ); 229 236 newnode->type->aggregate.kind = kind; 230 newnode->type->aggregate.name = assign_strptr( name ); 231 if ( newnode->type->aggregate.name == "" ) { // anonymous aggregate ? 232 newnode->type->aggregate.name = anonymous.newName(); 237 if ( name ) { 238 newnode->type->aggregate.name = new string( *name ); 239 } else { // anonymous aggregate ? 240 newnode->type->aggregate.name = new string( anonymous.newName() ); 233 241 } // if 234 242 newnode->type->aggregate.actuals = actuals; … … 238 246 } // DeclarationNode::newAggregate 239 247 240 DeclarationNode * DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {241 DeclarationNode * newnode = new DeclarationNode;242 newnode->name = assign_strptr( name );248 DeclarationNode * DeclarationNode::newEnum( std::string * name, DeclarationNode * constants ) { 249 DeclarationNode * newnode = new DeclarationNode; 250 newnode->name = name; 243 251 newnode->type = new TypeData( TypeData::Enum ); 244 newnode->type->enumeration.name = newnode->name; 245 if ( newnode->type->enumeration.name == "" ) { // anonymous enumeration ? 246 newnode->type->enumeration.name = DeclarationNode::anonymous.newName(); 252 if ( name ) { 253 newnode->type->enumeration.name = new string( *name ); 254 } else { // anonymous aggregate ? 255 newnode->type->enumeration.name = new string( anonymous.newName() ); 247 256 } // if 248 257 newnode->type->enumeration.constants = constants; … … 250 259 } // DeclarationNode::newEnum 251 260 252 DeclarationNode * DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {253 DeclarationNode * newnode = new DeclarationNode;254 newnode->name = assign_strptr( name );261 DeclarationNode * DeclarationNode::newEnumConstant( std::string * name, ExpressionNode * constant ) { 262 DeclarationNode * newnode = new DeclarationNode; 263 newnode->name = name; 255 264 newnode->enumeratorValue.reset( constant ); 256 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );265 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); 257 266 return newnode; 258 267 } // DeclarationNode::newEnumConstant 259 268 260 DeclarationNode * DeclarationNode::newName( std::string *name ) {261 DeclarationNode * newnode = new DeclarationNode;262 newnode->name = assign_strptr( name );269 DeclarationNode * DeclarationNode::newName( std::string * name ) { 270 DeclarationNode * newnode = new DeclarationNode; 271 newnode->name = name; 263 272 return newnode; 264 273 } // DeclarationNode::newName 265 274 266 DeclarationNode * DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {267 DeclarationNode * newnode = new DeclarationNode;275 DeclarationNode * DeclarationNode::newFromTypeGen( std::string * name, ExpressionNode * params ) { 276 DeclarationNode * newnode = new DeclarationNode; 268 277 newnode->type = new TypeData( TypeData::SymbolicInst ); 269 newnode->type->symbolic.name = assign_strptr( name );278 newnode->type->symbolic.name = name ? new string( *name ) : nullptr; 270 279 newnode->type->symbolic.isTypedef = false; 271 280 newnode->type->symbolic.actuals = params; … … 273 282 } // DeclarationNode::newFromTypeGen 274 283 275 DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) { 276 DeclarationNode *newnode = new DeclarationNode; 277 newnode->name = assign_strptr( name ); 278 newnode->type = new TypeData( TypeData::Variable ); 284 DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, std::string * name ) { 285 DeclarationNode * newnode = new DeclarationNode; 286 newnode->name = name; 287 // newnode->type = new TypeData( TypeData::Variable ); 288 newnode->type = nullptr; 279 289 newnode->variable.tyClass = tc; 280 newnode->variable.name = newnode->name ;290 newnode->variable.name = newnode->name ? new string( *newnode->name ) : nullptr; 281 291 return newnode; 282 292 } // DeclarationNode::newTypeParam 283 293 284 DeclarationNode * DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {285 DeclarationNode * newnode = new DeclarationNode;294 DeclarationNode * DeclarationNode::newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts ) { 295 DeclarationNode * newnode = new DeclarationNode; 286 296 newnode->type = new TypeData( TypeData::Aggregate ); 297 newnode->type->aggregate.name = name; 287 298 newnode->type->aggregate.kind = Trait; 288 299 newnode->type->aggregate.params = params; 289 300 newnode->type->aggregate.fields = asserts; 290 newnode->type->aggregate.name = assign_strptr( name );291 301 return newnode; 292 302 } // DeclarationNode::newTrait 293 303 294 DeclarationNode * DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) {295 DeclarationNode * newnode = new DeclarationNode;304 DeclarationNode * DeclarationNode::newTraitUse( const std::string * name, ExpressionNode * params ) { 305 DeclarationNode * newnode = new DeclarationNode; 296 306 newnode->type = new TypeData( TypeData::AggregateInst ); 297 307 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate ); 298 308 newnode->type->aggInst.aggregate->aggregate.kind = Trait; 299 newnode->type->aggInst.aggregate->aggregate.name = assign_strptr( name );309 newnode->type->aggInst.aggregate->aggregate.name = name; 300 310 newnode->type->aggInst.params = params; 301 311 return newnode; 302 312 } // DeclarationNode::newTraitUse 303 313 304 DeclarationNode * DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {305 DeclarationNode * newnode = new DeclarationNode;306 newnode->name = assign_strptr( name );314 DeclarationNode * DeclarationNode::newTypeDecl( std::string * name, DeclarationNode * typeParams ) { 315 DeclarationNode * newnode = new DeclarationNode; 316 newnode->name = name; 307 317 newnode->type = new TypeData( TypeData::Symbolic ); 308 318 newnode->type->symbolic.isTypedef = false; … … 312 322 } // DeclarationNode::newTypeDecl 313 323 314 DeclarationNode * DeclarationNode::newPointer( DeclarationNode *qualifiers ) {315 DeclarationNode * newnode = new DeclarationNode;324 DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) { 325 DeclarationNode * newnode = new DeclarationNode; 316 326 newnode->type = new TypeData( TypeData::Pointer ); 317 327 return newnode->addQualifiers( qualifiers ); 318 328 } // DeclarationNode::newPointer 319 329 320 DeclarationNode * DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {321 DeclarationNode * newnode = new DeclarationNode;330 DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) { 331 DeclarationNode * newnode = new DeclarationNode; 322 332 newnode->type = new TypeData( TypeData::Array ); 323 333 newnode->type->array.dimension = size; 324 334 newnode->type->array.isStatic = isStatic; 325 if ( newnode->type->array.dimension == 0 || newnode->type->array.dimension->isExpressionType<ConstantExpr *>() ) {335 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) { 326 336 newnode->type->array.isVarLen = false; 327 337 } else { … … 331 341 } // DeclarationNode::newArray 332 342 333 DeclarationNode * DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {334 DeclarationNode * newnode = new DeclarationNode;343 DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) { 344 DeclarationNode * newnode = new DeclarationNode; 335 345 newnode->type = new TypeData( TypeData::Array ); 336 newnode->type->array.dimension = 0;346 newnode->type->array.dimension = nullptr; 337 347 newnode->type->array.isStatic = false; 338 348 newnode->type->array.isVarLen = true; … … 340 350 } 341 351 342 DeclarationNode * DeclarationNode::newBitfield( ExpressionNode *size ) {343 DeclarationNode * newnode = new DeclarationNode;352 DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) { 353 DeclarationNode * newnode = new DeclarationNode; 344 354 newnode->bitfieldWidth = size; 345 355 return newnode; 346 356 } 347 357 348 DeclarationNode * DeclarationNode::newTuple( DeclarationNode *members ) {349 DeclarationNode * newnode = new DeclarationNode;358 DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) { 359 DeclarationNode * newnode = new DeclarationNode; 350 360 newnode->type = new TypeData( TypeData::Tuple ); 351 361 newnode->type->tuple = members; … … 353 363 } 354 364 355 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode *expr ) {356 DeclarationNode * newnode = new DeclarationNode;365 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) { 366 DeclarationNode * newnode = new DeclarationNode; 357 367 newnode->type = new TypeData( TypeData::Typeof ); 358 368 newnode->type->typeexpr = expr; … … 361 371 362 372 DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) { 363 DeclarationNode * newnode = new DeclarationNode;373 DeclarationNode * newnode = new DeclarationNode; 364 374 newnode->type = new TypeData( TypeData::Builtin ); 365 375 newnode->builtin = bt; … … 367 377 } // DeclarationNode::newBuiltinType 368 378 369 DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) { 370 DeclarationNode *newnode = new DeclarationNode; 371 newnode->type = new TypeData( TypeData::Attr ); 372 newnode->attr.name = assign_strptr( name ); 379 DeclarationNode * DeclarationNode::newAttr( std::string * name, ExpressionNode * expr ) { 380 DeclarationNode * newnode = new DeclarationNode; 381 // newnode->type = new TypeData( TypeData::Attr ); 382 newnode->type = nullptr; 383 newnode->attr.name = name; 373 384 newnode->attr.expr = expr; 374 385 return newnode; 375 386 } 376 387 377 DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) { 378 DeclarationNode *newnode = new DeclarationNode; 379 newnode->type = new TypeData( TypeData::Attr ); 380 newnode->attr.name = assign_strptr( name ); 388 DeclarationNode * DeclarationNode::newAttr( std::string * name, DeclarationNode * type ) { 389 DeclarationNode * newnode = new DeclarationNode; 390 // newnode->type = new TypeData( TypeData::Attr ); 391 newnode->type = nullptr; 392 newnode->attr.name = name; 381 393 newnode->attr.type = type; 382 394 return newnode; 383 }384 385 static void addQualifiersToType( TypeData *&src, TypeData *dst ) {386 if ( src && dst ) {387 if ( src->forall && dst->kind == TypeData::Function ) {388 if ( dst->forall ) {389 dst->forall->appendList( src->forall );390 } else {391 dst->forall = src->forall;392 } // if393 src->forall = 0;394 } // if395 if ( dst->base ) {396 addQualifiersToType( src, dst->base );397 } else if ( dst->kind == TypeData::Function ) {398 dst->base = src;399 src = 0;400 } else {401 dst->qualifiers |= src->qualifiers;402 } // if403 } // if404 395 } 405 396 … … 410 401 } // appendError 411 402 412 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData *dst ) {403 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { 413 404 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization 414 405 … … 422 413 } // DeclarationNode::checkQualifiers 423 414 424 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {415 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) { 425 416 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { 426 417 if ( storageClass == q->storageClass ) { // duplicate qualifier … … 434 425 } // DeclarationNode::copyStorageClasses 435 426 436 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode *q ) {427 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) { 437 428 isInline = isInline || q->isInline; 438 429 isNoreturn = isNoreturn || q->isNoreturn; … … 445 436 } // DeclarationNode::copyStorageClasses 446 437 447 DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) { 448 if ( q ) { 449 checkStorageClasses( q ); 450 copyStorageClasses( q ); 451 if ( q->type ) { 452 if ( ! type ) { 453 type = new TypeData; 438 static void addQualifiersToType( TypeData *&src, TypeData * dst ) { 439 if ( src->forall && dst->kind == TypeData::Function ) { 440 if ( dst->forall ) { 441 dst->forall->appendList( src->forall ); 442 } else { 443 dst->forall = src->forall; 444 } // if 445 src->forall = nullptr; 446 } // if 447 if ( dst->base ) { 448 addQualifiersToType( src, dst->base ); 449 } else if ( dst->kind == TypeData::Function ) { 450 dst->base = src; 451 src = nullptr; 452 } else { 453 dst->qualifiers |= src->qualifiers; 454 } // if 455 } // addQualifiersToType 456 457 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { 458 if ( ! q ) { delete q; return this; } 459 460 checkStorageClasses( q ); 461 copyStorageClasses( q ); 462 463 if ( ! q->type ) { 464 delete q; 465 return this; 466 } // if 467 468 if ( ! type ) { 469 type = q->type; // reuse this structure 470 q->type = nullptr; 471 delete q; 472 return this; 473 } // if 474 475 checkQualifiers( q->type, type ); 476 addQualifiersToType( q->type, type ); 477 478 if ( q->type->forall ) { 479 if ( type->forall ) { 480 type->forall->appendList( q->type->forall ); 481 } else { 482 if ( type->kind == TypeData::Aggregate ) { 483 type->aggregate.params = q->type->forall; 484 // change implicit typedef from TYPEDEFname to TYPEGENname 485 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG ); 454 486 } else { 455 checkQualifiers( q->type, type );487 type->forall = q->type->forall; 456 488 } // if 457 addQualifiersToType( q->type, type ); 458 if ( q->type && q->type->forall ) { 459 if ( type->forall ) { 460 type->forall->appendList( q->type->forall ); 461 } else { 462 if ( type->kind == TypeData::Aggregate ) { 463 type->aggregate.params = q->type->forall; 464 // change implicit typedef from TYPEDEFname to TYPEGENname 465 typedefTable.changeKind( type->aggregate.name, TypedefTable::TG ); 466 } else { 467 type->forall = q->type->forall; 468 } // if 489 } // if 490 q->type->forall = nullptr; 491 } // if 492 delete q; 493 return this; 494 } // addQualifiers 495 496 static void addTypeToType( TypeData *&src, TypeData *&dst ) { 497 if ( src->forall && dst->kind == TypeData::Function ) { 498 if ( dst->forall ) { 499 dst->forall->appendList( src->forall ); 500 } else { 501 dst->forall = src->forall; 502 } // if 503 src->forall = nullptr; 504 } // if 505 if ( dst->base ) { 506 addTypeToType( src, dst->base ); 507 } else { 508 switch ( dst->kind ) { 509 case TypeData::Unknown: 510 src->qualifiers |= dst->qualifiers; 511 dst = src; 512 src = nullptr; 513 break; 514 case TypeData::Basic: 515 dst->qualifiers |= src->qualifiers; 516 if ( src->kind != TypeData::Unknown ) { 517 assert( src->kind == TypeData::Basic ); 518 519 if ( dst->basictype == DeclarationNode::NoBasicType ) { 520 dst->basictype = src->basictype; 521 } else if ( src->basictype != DeclarationNode::NoBasicType ) 522 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src ); 523 524 if ( dst->complextype == DeclarationNode::NoComplexType ) { 525 dst->complextype = src->complextype; 526 } else if ( src->complextype != DeclarationNode::NoComplexType ) 527 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src ); 528 529 if ( dst->signedness == DeclarationNode::NoSignedness ) { 530 dst->signedness = src->signedness; 531 } else if ( src->signedness != DeclarationNode::NoSignedness ) 532 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src ); 533 534 if ( dst->length == DeclarationNode::NoLength ) { 535 dst->length = src->length; 536 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { 537 dst->length = DeclarationNode::LongLong; 538 } else if ( src->length != DeclarationNode::NoLength ) 539 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src ); 540 } // if 541 break; 542 default: 543 switch ( src->kind ) { 544 case TypeData::Aggregate: 545 case TypeData::Enum: 546 dst->base = new TypeData( TypeData::AggregateInst ); 547 dst->base->aggInst.aggregate = src; 548 if ( src->kind == TypeData::Aggregate ) { 549 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 469 550 } // if 470 q->type->forall = 0; 471 } // if 472 } // if 473 } // if 474 delete q; 475 return this; 476 } 477 478 static void addTypeToType( TypeData *&src, TypeData *&dst ) { 479 if ( src && dst ) { 480 if ( src->forall && dst->kind == TypeData::Function ) { 481 if ( dst->forall ) { 482 dst->forall->appendList( src->forall ); 483 } else { 484 dst->forall = src->forall; 485 } // if 486 src->forall = 0; 487 } // if 488 if ( dst->base ) { 489 addTypeToType( src, dst->base ); 490 } else { 491 switch ( dst->kind ) { 492 case TypeData::Unknown: 493 src->qualifiers |= dst->qualifiers; 494 dst = src; 495 src = 0; 496 break; 497 case TypeData::Basic: 498 dst->qualifiers |= src->qualifiers; 499 if ( src->kind != TypeData::Unknown ) { 500 assert( src->kind == TypeData::Basic ); 501 502 if ( dst->basictype == DeclarationNode::NoBasicType ) { 503 dst->basictype = src->basictype; 504 } else if ( src->basictype != DeclarationNode::NoBasicType ) 505 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src ); 506 507 if ( dst->complextype == DeclarationNode::NoComplexType ) { 508 dst->complextype = src->complextype; 509 } else if ( src->complextype != DeclarationNode::NoComplexType ) 510 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src ); 511 512 if ( dst->signedness == DeclarationNode::NoSignedness ) { 513 dst->signedness = src->signedness; 514 } else if ( src->signedness != DeclarationNode::NoSignedness ) 515 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src ); 516 517 if ( dst->length == DeclarationNode::NoLength ) { 518 dst->length = src->length; 519 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { 520 dst->length = DeclarationNode::LongLong; 521 } else if ( src->length != DeclarationNode::NoLength ) 522 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src ); 523 } // if 551 dst->base->qualifiers |= src->qualifiers; 552 src = nullptr; 524 553 break; 525 554 default: 526 switch ( src->kind ) { 527 case TypeData::Aggregate: 528 case TypeData::Enum: 529 dst->base = new TypeData( TypeData::AggregateInst ); 530 dst->base->aggInst.aggregate = src; 531 if ( src->kind == TypeData::Aggregate ) { 532 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 533 } // if 534 dst->base->qualifiers |= src->qualifiers; 535 src = 0; 536 break; 537 default: 538 if ( dst->forall ) { 539 dst->forall->appendList( src->forall ); 540 } else { 541 dst->forall = src->forall; 542 } // if 543 src->forall = 0; 544 dst->base = src; 545 src = 0; 546 } // switch 555 if ( dst->forall ) { 556 dst->forall->appendList( src->forall ); 557 } else { 558 dst->forall = src->forall; 559 } // if 560 src->forall = nullptr; 561 dst->base = src; 562 src = nullptr; 547 563 } // switch 548 } // if549 } // if 550 } 551 552 DeclarationNode * DeclarationNode::addType( DeclarationNode *o ) {564 } // switch 565 } // if 566 } 567 568 DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) { 553 569 if ( o ) { 554 570 checkStorageClasses( o ); … … 566 582 type = o->type; 567 583 } // if 568 o->type = 0;584 o->type = nullptr; 569 585 } else { 570 586 addTypeToType( o->type, type ); … … 584 600 } 585 601 586 DeclarationNode * DeclarationNode::addTypedef() {587 TypeData * newtype = new TypeData( TypeData::Symbolic );588 newtype->symbolic.params = 0;602 DeclarationNode * DeclarationNode::addTypedef() { 603 TypeData * newtype = new TypeData( TypeData::Symbolic ); 604 newtype->symbolic.params = nullptr; 589 605 newtype->symbolic.isTypedef = true; 590 newtype->symbolic.name = name ;606 newtype->symbolic.name = name ? new string( *name ) : nullptr; 591 607 newtype->base = type; 592 608 type = newtype; … … 594 610 } 595 611 596 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) { 612 DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) { 613 if ( variable.name ) { 614 if ( variable.assertions ) { 615 variable.assertions->appendList( assertions ); 616 } else { 617 variable.assertions = assertions; 618 } // if 619 return this; 620 } // if 621 597 622 assert( type ); 598 623 switch ( type->kind ) { … … 604 629 } // if 605 630 break; 606 case TypeData::Variable:607 if ( variable.assertions ) {608 variable.assertions->appendList( assertions );609 } else {610 variable.assertions = assertions;611 } // if612 break;631 // case TypeData::Variable: 632 // if ( variable.assertions ) { 633 // variable.assertions->appendList( assertions ); 634 // } else { 635 // variable.assertions = assertions; 636 // } // if 637 // break; 613 638 default: 614 639 assert( false ); … … 618 643 } 619 644 620 DeclarationNode *DeclarationNode::addName( std::string *newname ) { 621 name = assign_strptr( newname ); 622 return this; 623 } 624 625 DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) { 645 DeclarationNode * DeclarationNode::addName( std::string * newname ) { 646 assert( ! name ); 647 name = newname; 648 return this; 649 } 650 651 DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { 626 652 bitfieldWidth = size; 627 653 return this; 628 654 } 629 655 630 DeclarationNode * DeclarationNode::addVarArgs() {656 DeclarationNode * DeclarationNode::addVarArgs() { 631 657 assert( type ); 632 658 hasEllipsis = true; … … 634 660 } 635 661 636 DeclarationNode * DeclarationNode::addFunctionBody( StatementNode *body ) {662 DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) { 637 663 assert( type ); 638 664 assert( type->kind == TypeData::Function ); 639 assert( type->function.body == 0);665 assert( ! type->function.body ); 640 666 type->function.body = body; 641 667 type->function.hasBody = true; … … 643 669 } 644 670 645 DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode *list ) {671 DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) { 646 672 assert( type ); 647 673 assert( type->kind == TypeData::Function ); 648 assert( type->function.oldDeclList == 0);674 assert( ! type->function.oldDeclList ); 649 675 type->function.oldDeclList = list; 650 676 return this; 651 677 } 652 678 653 static void setBase( TypeData *&type, TypeData * newType ) {679 static void setBase( TypeData *&type, TypeData * newType ) { 654 680 if ( type ) { 655 TypeData * prevBase = type;656 TypeData * curBase = type->base;657 while ( curBase != 0) {681 TypeData * prevBase = type; 682 TypeData * curBase = type->base; 683 while ( curBase != nullptr ) { 658 684 prevBase = curBase; 659 685 curBase = curBase->base; … … 665 691 } 666 692 667 DeclarationNode * DeclarationNode::addPointer( DeclarationNode *p ) {693 DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) { 668 694 if ( p ) { 669 695 assert( p->type->kind == TypeData::Pointer ); 670 696 setBase( type, p->type ); 671 p->type = 0;697 p->type = nullptr; 672 698 delete p; 673 699 } // if … … 675 701 } 676 702 677 DeclarationNode * DeclarationNode::addArray( DeclarationNode *a ) {703 DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) { 678 704 if ( a ) { 679 705 assert( a->type->kind == TypeData::Array ); 680 706 setBase( type, a->type ); 681 a->type = 0;707 a->type = nullptr; 682 708 delete a; 683 709 } // if … … 685 711 } 686 712 687 DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode *p ) {713 DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) { 688 714 if ( p ) { 689 715 assert( p->type->kind == TypeData::Pointer ); … … 703 729 p->type->base = type; 704 730 } // switch 705 type = 0;731 type = nullptr; 706 732 } // if 707 733 delete this; … … 712 738 } 713 739 714 static TypeData * findLast( TypeData *a ) {740 static TypeData * findLast( TypeData * a ) { 715 741 assert( a ); 716 TypeData * cur = a;742 TypeData * cur = a; 717 743 while ( cur->base ) { 718 744 cur = cur->base; … … 721 747 } 722 748 723 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode *a ) {749 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { 724 750 if ( a ) { 725 751 assert( a->type->kind == TypeData::Array ); 726 TypeData * lastArray = findLast( a->type );752 TypeData * lastArray = findLast( a->type ); 727 753 if ( type ) { 728 754 switch ( type->kind ) { … … 739 765 lastArray->base = type; 740 766 } // switch 741 type = 0;767 type = nullptr; 742 768 } // if 743 769 delete this; … … 748 774 } 749 775 750 DeclarationNode * DeclarationNode::addParamList( DeclarationNode *params ) {751 TypeData * ftype = new TypeData( TypeData::Function );776 DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) { 777 TypeData * ftype = new TypeData( TypeData::Function ); 752 778 ftype->function.params = params; 753 779 setBase( type, ftype ); … … 755 781 } 756 782 757 static TypeData * addIdListToType( TypeData *type, DeclarationNode *ids ) {783 static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) { 758 784 if ( type ) { 759 785 if ( type->kind != TypeData::Function ) { … … 764 790 return type; 765 791 } else { 766 TypeData * newtype = new TypeData( TypeData::Function );792 TypeData * newtype = new TypeData( TypeData::Function ); 767 793 newtype->function.idList = ids; 768 794 return newtype; 769 795 } // if 770 } 771 772 DeclarationNode * DeclarationNode::addIdList( DeclarationNode *ids ) {796 } // addIdListToType 797 798 DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) { 773 799 type = addIdListToType( type, ids ); 774 800 return this; 775 801 } 776 802 777 DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) { 778 //assert 803 DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { 779 804 initializer = init; 780 805 return this; 781 806 } 782 807 783 DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) { 784 DeclarationNode *newnode = new DeclarationNode; 785 TypeData *srcType = type; 786 while ( srcType->base ) { 787 srcType = srcType->base; 788 } // while 789 newnode->type = maybeClone( srcType ); 790 if ( newnode->type->kind == TypeData::AggregateInst ) { 791 // don't duplicate members 792 if ( newnode->type->aggInst.aggregate->kind == TypeData::Enum ) { 793 delete newnode->type->aggInst.aggregate->enumeration.constants; 794 newnode->type->aggInst.aggregate->enumeration.constants = 0; 795 } else { 796 assert( newnode->type->aggInst.aggregate->kind == TypeData::Aggregate ); 797 delete newnode->type->aggInst.aggregate->aggregate.fields; 798 newnode->type->aggInst.aggregate->aggregate.fields = 0; 799 } // if 800 } // if 801 newnode->type->forall = maybeClone( type->forall ); 802 assert( storageClass == NoStorageClass ); 803 newnode->copyStorageClasses( this ); 804 newnode->name = assign_strptr( newName ); 805 return newnode; 806 } 807 808 DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) { 809 if ( o ) { 810 o->copyStorageClasses( this ); 811 if ( type ) { 812 TypeData *srcType = type; 813 while ( srcType->base ) { 814 srcType = srcType->base; 815 } // while 816 TypeData *newType = srcType->clone(); 817 if ( newType->kind == TypeData::AggregateInst ) { 818 // don't duplicate members 819 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { 820 delete newType->aggInst.aggregate->enumeration.constants; 821 newType->aggInst.aggregate->enumeration.constants = 0; 822 } else { 823 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); 824 delete newType->aggInst.aggregate->aggregate.fields; 825 newType->aggInst.aggregate->aggregate.fields = 0; 826 } // if 827 } // if 828 newType->forall = maybeClone( type->forall ); 829 if ( ! o->type ) { 830 o->type = newType; 831 } else { 832 addTypeToType( newType, o->type ); 833 delete newType; 834 } // if 835 } // if 836 } // if 837 return o; 838 } 839 840 DeclarationNode *DeclarationNode::cloneType( string *newName ) { 841 DeclarationNode *newnode = new DeclarationNode; 808 DeclarationNode * DeclarationNode::cloneType( string * newName ) { 809 DeclarationNode * newnode = new DeclarationNode; 842 810 newnode->type = maybeClone( type ); 843 811 assert( storageClass == NoStorageClass ); 844 812 newnode->copyStorageClasses( this ); 845 newnode->name = assign_strptr( newName ); 846 return newnode; 847 } 848 849 DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) { 850 if ( o ) { 851 assert( storageClass == NoStorageClass ); 852 o->copyStorageClasses( this ); 853 if ( type ) { 854 TypeData *newType = type->clone(); 855 if ( ! o->type ) { 856 o->type = newType; 813 assert( newName ); 814 newnode->name = newName; 815 return newnode; 816 } 817 818 DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) { 819 if ( ! o ) return nullptr; 820 821 o->copyStorageClasses( this ); 822 if ( type ) { 823 TypeData * srcType = type; 824 825 while ( srcType->base ) { 826 srcType = srcType->base; 827 } // while 828 829 TypeData * newType = srcType->clone(); 830 if ( newType->kind == TypeData::AggregateInst ) { 831 // don't duplicate members 832 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { 833 delete newType->aggInst.aggregate->enumeration.constants; 834 newType->aggInst.aggregate->enumeration.constants = nullptr; 857 835 } else { 858 addTypeToType( newType, o->type ); 859 delete newType; 836 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); 837 delete newType->aggInst.aggregate->aggregate.fields; 838 newType->aggInst.aggregate->aggregate.fields = nullptr; 860 839 } // if 861 840 } // if 862 } // if 863 delete o; 841 842 newType->forall = maybeClone( type->forall ); 843 if ( ! o->type ) { 844 o->type = newType; 845 } else { 846 addTypeToType( newType, o->type ); 847 delete newType; 848 } // if 849 } // if 864 850 return o; 865 851 } 866 852 867 DeclarationNode * DeclarationNode::extractAggregate() const {853 DeclarationNode * DeclarationNode::extractAggregate() const { 868 854 if ( type ) { 869 TypeData * ret = typeextractAggregate( type );855 TypeData * ret = typeextractAggregate( type ); 870 856 if ( ret ) { 871 DeclarationNode * newnode = new DeclarationNode;857 DeclarationNode * newnode = new DeclarationNode; 872 858 newnode->type = ret; 873 859 return newnode; 874 860 } // if 875 861 } // if 876 return 0;877 } 878 879 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {862 return nullptr; 863 } 864 865 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) { 880 866 SemanticError errors; 881 867 std::back_insert_iterator< std::list< Declaration * > > out( outputList ); 882 const DeclarationNode *cur = firstNode; 868 const DeclarationNode * cur = firstNode; 869 883 870 while ( cur ) { 884 871 try { 885 if ( DeclarationNode * extr = cur->extractAggregate() ) {872 if ( DeclarationNode * extr = cur->extractAggregate() ) { 886 873 // handle the case where a structure declaration is contained within an object or type declaration 887 Declaration * decl = extr->build();874 Declaration * decl = extr->build(); 888 875 if ( decl ) { 889 * out++ = decl;876 * out++ = decl; 890 877 } // if 891 878 delete extr; 892 879 } // if 893 Declaration *decl = cur->build(); 880 881 Declaration * decl = cur->build(); 894 882 if ( decl ) { 895 * out++ = decl;883 * out++ = decl; 896 884 } // if 897 885 } catch( SemanticError &e ) { … … 900 888 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 901 889 } // while 890 902 891 if ( ! errors.isEmpty() ) { 903 892 throw errors; 904 893 } // if 905 } 906 907 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {894 } // buildList 895 896 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 908 897 SemanticError errors; 909 898 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 910 const DeclarationNode * cur = firstNode;899 const DeclarationNode * cur = firstNode; 911 900 while ( cur ) { 912 901 try { 913 Declaration * decl = cur->build();902 Declaration * decl = cur->build(); 914 903 if ( decl ) { 915 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {916 * out++ = dwt;917 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {918 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );919 * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0);904 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 905 * out++ = dwt; 906 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 907 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 908 * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); 920 909 delete agg; 921 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {922 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );923 * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0);910 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 911 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 912 * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); 924 913 } // if 925 914 } // if … … 932 921 throw errors; 933 922 } // if 934 } 935 936 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {923 } // buildList 924 925 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) { 937 926 SemanticError errors; 938 927 std::back_insert_iterator< std::list< Type * > > out( outputList ); 939 const DeclarationNode *cur = firstNode; 928 const DeclarationNode * cur = firstNode; 929 940 930 while ( cur ) { 941 931 try { 942 * out++ = cur->buildType();932 * out++ = cur->buildType(); 943 933 } catch( SemanticError &e ) { 944 934 errors.append( e ); … … 946 936 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 947 937 } // while 938 948 939 if ( ! errors.isEmpty() ) { 949 940 throw errors; 950 941 } // if 951 } 952 953 Declaration * DeclarationNode::build() const {942 } // buildTypeList 943 944 Declaration * DeclarationNode::build() const { 954 945 if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this ); 946 947 if ( variable.name ) { 948 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype }; 949 TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); 950 buildList( variable.assertions, ret->get_assertions() ); 951 return ret; 952 } // if 953 955 954 if ( type ) { 956 if ( type->kind == TypeData::Variable ) { 957 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype }; 958 TypeDecl * ret = new TypeDecl( variable.name, DeclarationNode::NoStorageClass, 0, kindMap[ variable.tyClass ] ); 959 buildList( variable.assertions, ret->get_assertions() ); 960 return ret; 955 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension ); 956 } // if 957 958 if ( ! isInline && ! isNoreturn ) { 959 assertf( name, "ObjectDecl are assumed to have names\n" ); 960 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_extension( extension ); 961 } // if 962 963 throw SemanticError( "invalid function specifier ", this ); 964 } 965 966 Type * DeclarationNode::buildType() const { 967 assert( type ); 968 969 if ( attr.name ) { 970 AttrType * ret; 971 if ( attr.expr ) { 972 ret = new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() ); 961 973 } else { 962 return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension ); 963 } // if 964 } // if 965 if ( ! isInline && ! isNoreturn ) { 966 return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension ); 967 } // if 968 throw SemanticError( "invalid function specifier ", this ); 969 } 970 971 Type *DeclarationNode::buildType() const { 972 assert( type ); 974 assert( attr.type ); 975 ret = new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() ); 976 } // if 977 return ret; 978 } // if 973 979 974 980 switch ( type->kind ) { 975 981 case TypeData::Enum: 976 return new EnumInstType( buildQualifiers( type ), type->enumeration.name );982 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name ); 977 983 case TypeData::Aggregate: { 978 ReferenceToType * ret;984 ReferenceToType * ret; 979 985 switch ( type->aggregate.kind ) { 980 986 case DeclarationNode::Struct: 981 ret = new StructInstType( buildQualifiers( type ), type->aggregate.name );987 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name ); 982 988 break; 983 989 case DeclarationNode::Union: 984 ret = new UnionInstType( buildQualifiers( type ), type->aggregate.name );990 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name ); 985 991 break; 986 992 case DeclarationNode::Trait: 987 ret = new TraitInstType( buildQualifiers( type ), type->aggregate.name );993 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name ); 988 994 break; 989 995 default: … … 994 1000 } 995 1001 case TypeData::Symbolic: { 996 TypeInstType * ret = new TypeInstType( buildQualifiers( type ),type->symbolic.name, false );1002 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false ); 997 1003 buildList( type->symbolic.actuals, ret->get_parameters() ); 998 return ret;999 }1000 case TypeData::Attr: {1001 assert( type->kind == TypeData::Attr );1002 // assert( type->attr );1003 AttrType * ret;1004 if ( attr.expr ) {1005 ret = new AttrType( buildQualifiers( type ), attr.name, attr.expr->build() );1006 } else {1007 assert( attr.type );1008 ret = new AttrType( buildQualifiers( type ), attr.name, attr.type->buildType() );1009 } // if1010 1004 return ret; 1011 1005 }
Note:
See TracChangeset
for help on using the changeset viewer.