Changes in src/Parser/DeclarationNode.cc [2298f728:5b639ee]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (52 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r2298f728 r5b639ee 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Sep 24 11:12:52201613 // Update Count : 62712 // Last Modified On : Mon Sep 12 21:03:18 2016 13 // Update Count : 491 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( nullptr),48 type( 0 ), 49 49 storageClass( NoStorageClass ), 50 50 isInline( false ), 51 51 isNoreturn( false ), 52 bitfieldWidth( nullptr),53 initializer( nullptr),52 bitfieldWidth( 0 ), 53 initializer( 0 ), 54 54 hasEllipsis( false ), 55 55 linkage( ::linkage ), 56 56 extension( false ) { 57 58 variable.name = nullptr;59 57 variable.tyClass = DeclarationNode::Otype; 60 58 variable.assertions = nullptr; 61 59 62 attr.name = nullptr;63 60 attr.expr = nullptr; 64 61 attr.type = nullptr; … … 66 63 67 64 DeclarationNode::~DeclarationNode() { 68 delete attr.name;69 65 delete attr.expr; 70 66 delete attr.type; 71 72 delete variable.name;73 delete variable.assertions;74 75 67 delete type; 76 68 delete bitfieldWidth; … … 78 70 } 79 71 80 DeclarationNode * DeclarationNode::clone() const {81 DeclarationNode * newnode = new DeclarationNode;72 DeclarationNode *DeclarationNode::clone() const { 73 DeclarationNode *newnode = new DeclarationNode; 82 74 newnode->type = maybeClone( type ); 83 newnode->name = name ? new string( *name ) : nullptr;75 newnode->name = name; 84 76 newnode->storageClass = storageClass; 85 77 newnode->isInline = isInline; … … 91 83 newnode->linkage = linkage; 92 84 93 newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;94 85 newnode->variable.assertions = maybeClone( variable.assertions ); 86 newnode->variable.name = variable.name; 95 87 newnode->variable.tyClass = variable.tyClass; 96 88 97 newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;98 89 newnode->attr.expr = maybeClone( attr.expr ); 99 90 newnode->attr.type = maybeClone( attr.type ); … … 107 98 void DeclarationNode::print( std::ostream &os, int indent ) const { 108 99 os << string( indent, ' ' ); 109 if ( name ) {110 os << *name << ": ";100 if ( name == "" ) { 101 os << "unnamed: "; 111 102 } else { 112 os << "unnamed: ";103 os << name << ": "; 113 104 } // if 114 105 … … 131 122 } // if 132 123 133 if ( initializer ) {124 if ( initializer != 0 ) { 134 125 os << endl << string( indent + 2, ' ' ) << "with initializer "; 135 126 initializer->printOneLine( os ); … … 148 139 } 149 140 150 DeclarationNode * DeclarationNode::newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode *body, bool newStyle ) {151 DeclarationNode * newnode = new DeclarationNode;152 newnode->name = name;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 ); 153 144 154 145 newnode->type = new TypeData( TypeData::Function ); … … 156 147 newnode->type->function.newStyle = newStyle; 157 148 newnode->type->function.body = body; 158 // ignore unnamed routine declarations: void p( int (*)(int) ); 159 if ( newnode->name ) { 160 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); 161 } // if 149 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID ); 162 150 163 151 if ( body ) { … … 167 155 if ( ret ) { 168 156 newnode->type->base = ret->type; 169 ret->type = nullptr;157 ret->type = 0; 170 158 delete ret; 171 159 } // if … … 175 163 176 164 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { 177 DeclarationNode * newnode = new DeclarationNode;165 DeclarationNode *newnode = new DeclarationNode; 178 166 newnode->type = new TypeData(); 179 167 newnode->type->qualifiers[ q ] = 1; … … 181 169 } // DeclarationNode::newQualifier 182 170 183 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {184 DeclarationNode * newnode = new DeclarationNode;171 DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) { 172 DeclarationNode *newnode = new DeclarationNode; 185 173 newnode->type = new TypeData( TypeData::Unknown ); 186 174 newnode->type->forall = forall; … … 189 177 190 178 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { 191 DeclarationNode * newnode = new DeclarationNode; 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 //} 192 185 newnode->storageClass = sc; 193 186 return newnode; … … 195 188 196 189 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { 197 DeclarationNode * newnode = new DeclarationNode;190 DeclarationNode *newnode = new DeclarationNode; 198 191 newnode->type = new TypeData( TypeData::Basic ); 199 192 newnode->type->basictype = bt; … … 202 195 203 196 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) { 204 DeclarationNode * newnode = new DeclarationNode;197 DeclarationNode *newnode = new DeclarationNode; 205 198 newnode->type = new TypeData( TypeData::Basic ); 206 199 newnode->type->complextype = ct; … … 209 202 210 203 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) { 211 DeclarationNode * newnode = new DeclarationNode;204 DeclarationNode *newnode = new DeclarationNode; 212 205 newnode->type = new TypeData( TypeData::Basic ); 213 206 newnode->type->signedness = sn; … … 216 209 217 210 DeclarationNode * DeclarationNode::newLength( Length lnth ) { 218 DeclarationNode * newnode = new DeclarationNode;211 DeclarationNode *newnode = new DeclarationNode; 219 212 newnode->type = new TypeData( TypeData::Basic ); 220 213 newnode->type->length = lnth; … … 222 215 } // DeclarationNode::newLength 223 216 224 DeclarationNode * DeclarationNode::newFromTypedef( std::string * name ) {225 DeclarationNode * newnode = new DeclarationNode;217 DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) { 218 DeclarationNode *newnode = new DeclarationNode; 226 219 newnode->type = new TypeData( TypeData::SymbolicInst ); 227 newnode->type->symbolic.name = name ? new string( *name ) : nullptr;220 newnode->type->symbolic.name = assign_strptr( name ); 228 221 newnode->type->symbolic.isTypedef = true; 229 newnode->type->symbolic.params = nullptr;222 newnode->type->symbolic.params = 0; 230 223 return newnode; 231 224 } // DeclarationNode::newFromTypedef 232 225 233 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode *fields, bool body ) {234 DeclarationNode * newnode = new DeclarationNode;226 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) { 227 DeclarationNode *newnode = new DeclarationNode; 235 228 newnode->type = new TypeData( TypeData::Aggregate ); 236 229 newnode->type->aggregate.kind = kind; 237 if ( name ) { 238 newnode->type->aggregate.name = new string( *name ); 239 } else { // anonymous aggregate ? 240 newnode->type->aggregate.name = new string( anonymous.newName() ); 230 newnode->type->aggregate.name = assign_strptr( name ); 231 if ( newnode->type->aggregate.name == "" ) { // anonymous aggregate ? 232 newnode->type->aggregate.name = anonymous.newName(); 241 233 } // if 242 234 newnode->type->aggregate.actuals = actuals; … … 246 238 } // DeclarationNode::newAggregate 247 239 248 DeclarationNode * DeclarationNode::newEnum( std::string * name, DeclarationNode *constants ) {249 DeclarationNode * newnode = new DeclarationNode;250 newnode->name = name;240 DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) { 241 DeclarationNode *newnode = new DeclarationNode; 242 newnode->name = assign_strptr( name ); 251 243 newnode->type = new TypeData( TypeData::Enum ); 252 if ( name ) { 253 newnode->type->enumeration.name = new string( *name ); 254 } else { // anonymous aggregate ? 255 newnode->type->enumeration.name = new string( anonymous.newName() ); 244 newnode->type->enumeration.name = newnode->name; 245 if ( newnode->type->enumeration.name == "" ) { // anonymous enumeration ? 246 newnode->type->enumeration.name = DeclarationNode::anonymous.newName(); 256 247 } // if 257 248 newnode->type->enumeration.constants = constants; … … 259 250 } // DeclarationNode::newEnum 260 251 261 DeclarationNode * DeclarationNode::newEnumConstant( std::string * name, ExpressionNode *constant ) {262 DeclarationNode * newnode = new DeclarationNode;263 newnode->name = name;252 DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) { 253 DeclarationNode *newnode = new DeclarationNode; 254 newnode->name = assign_strptr( name ); 264 255 newnode->enumeratorValue.reset( constant ); 265 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );256 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID ); 266 257 return newnode; 267 258 } // DeclarationNode::newEnumConstant 268 259 269 DeclarationNode * DeclarationNode::newName( std::string *name ) {270 DeclarationNode * newnode = new DeclarationNode;271 newnode->name = name;260 DeclarationNode *DeclarationNode::newName( std::string *name ) { 261 DeclarationNode *newnode = new DeclarationNode; 262 newnode->name = assign_strptr( name ); 272 263 return newnode; 273 264 } // DeclarationNode::newName 274 265 275 DeclarationNode * DeclarationNode::newFromTypeGen( std::string * name, ExpressionNode *params ) {276 DeclarationNode * newnode = new DeclarationNode;266 DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) { 267 DeclarationNode *newnode = new DeclarationNode; 277 268 newnode->type = new TypeData( TypeData::SymbolicInst ); 278 newnode->type->symbolic.name = name ? new string( *name ) : nullptr;269 newnode->type->symbolic.name = assign_strptr( name ); 279 270 newnode->type->symbolic.isTypedef = false; 280 271 newnode->type->symbolic.actuals = params; … … 282 273 } // DeclarationNode::newFromTypeGen 283 274 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; 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 ); 289 279 newnode->variable.tyClass = tc; 290 newnode->variable.name = newnode->name ? new string( *newnode->name ) : nullptr;280 newnode->variable.name = newnode->name; 291 281 return newnode; 292 282 } // DeclarationNode::newTypeParam 293 283 294 DeclarationNode * DeclarationNode::newTrait( const std::string * name, DeclarationNode * params, DeclarationNode *asserts ) {295 DeclarationNode * newnode = new DeclarationNode;284 DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) { 285 DeclarationNode *newnode = new DeclarationNode; 296 286 newnode->type = new TypeData( TypeData::Aggregate ); 297 newnode->type->aggregate.name = name;298 287 newnode->type->aggregate.kind = Trait; 299 288 newnode->type->aggregate.params = params; 300 289 newnode->type->aggregate.fields = asserts; 290 newnode->type->aggregate.name = assign_strptr( name ); 301 291 return newnode; 302 292 } // DeclarationNode::newTrait 303 293 304 DeclarationNode * DeclarationNode::newTraitUse( const std::string * name, ExpressionNode *params ) {305 DeclarationNode * newnode = new DeclarationNode;294 DeclarationNode *DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) { 295 DeclarationNode *newnode = new DeclarationNode; 306 296 newnode->type = new TypeData( TypeData::AggregateInst ); 307 297 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate ); 308 298 newnode->type->aggInst.aggregate->aggregate.kind = Trait; 309 newnode->type->aggInst.aggregate->aggregate.name = name;299 newnode->type->aggInst.aggregate->aggregate.name = assign_strptr( name ); 310 300 newnode->type->aggInst.params = params; 311 301 return newnode; 312 302 } // DeclarationNode::newTraitUse 313 303 314 DeclarationNode * DeclarationNode::newTypeDecl( std::string * name, DeclarationNode *typeParams ) {315 DeclarationNode * newnode = new DeclarationNode;316 newnode->name = name;304 DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) { 305 DeclarationNode *newnode = new DeclarationNode; 306 newnode->name = assign_strptr( name ); 317 307 newnode->type = new TypeData( TypeData::Symbolic ); 318 308 newnode->type->symbolic.isTypedef = false; … … 322 312 } // DeclarationNode::newTypeDecl 323 313 324 DeclarationNode * DeclarationNode::newPointer( DeclarationNode *qualifiers ) {325 DeclarationNode * newnode = new DeclarationNode;314 DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) { 315 DeclarationNode *newnode = new DeclarationNode; 326 316 newnode->type = new TypeData( TypeData::Pointer ); 327 317 return newnode->addQualifiers( qualifiers ); 328 318 } // DeclarationNode::newPointer 329 319 330 DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode *qualifiers, bool isStatic ) {331 DeclarationNode * newnode = new DeclarationNode;320 DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) { 321 DeclarationNode *newnode = new DeclarationNode; 332 322 newnode->type = new TypeData( TypeData::Array ); 333 323 newnode->type->array.dimension = size; 334 324 newnode->type->array.isStatic = isStatic; 335 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr *>() ) {325 if ( newnode->type->array.dimension == 0 || newnode->type->array.dimension->isExpressionType<ConstantExpr *>() ) { 336 326 newnode->type->array.isVarLen = false; 337 327 } else { … … 341 331 } // DeclarationNode::newArray 342 332 343 DeclarationNode * DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {344 DeclarationNode * newnode = new DeclarationNode;333 DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) { 334 DeclarationNode *newnode = new DeclarationNode; 345 335 newnode->type = new TypeData( TypeData::Array ); 346 newnode->type->array.dimension = nullptr;336 newnode->type->array.dimension = 0; 347 337 newnode->type->array.isStatic = false; 348 338 newnode->type->array.isVarLen = true; … … 350 340 } 351 341 352 DeclarationNode * DeclarationNode::newBitfield( ExpressionNode *size ) {353 DeclarationNode * newnode = new DeclarationNode;342 DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) { 343 DeclarationNode *newnode = new DeclarationNode; 354 344 newnode->bitfieldWidth = size; 355 345 return newnode; 356 346 } 357 347 358 DeclarationNode * DeclarationNode::newTuple( DeclarationNode *members ) {359 DeclarationNode * newnode = new DeclarationNode;348 DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) { 349 DeclarationNode *newnode = new DeclarationNode; 360 350 newnode->type = new TypeData( TypeData::Tuple ); 361 351 newnode->type->tuple = members; … … 363 353 } 364 354 365 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode *expr ) {366 DeclarationNode * newnode = new DeclarationNode;355 DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) { 356 DeclarationNode *newnode = new DeclarationNode; 367 357 newnode->type = new TypeData( TypeData::Typeof ); 368 358 newnode->type->typeexpr = expr; … … 371 361 372 362 DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) { 373 DeclarationNode * newnode = new DeclarationNode;363 DeclarationNode *newnode = new DeclarationNode; 374 364 newnode->type = new TypeData( TypeData::Builtin ); 375 365 newnode->builtin = bt; … … 377 367 } // DeclarationNode::newBuiltinType 378 368 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; 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 ); 384 373 newnode->attr.expr = expr; 385 374 return newnode; 386 375 } 387 376 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; 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 ); 393 381 newnode->attr.type = type; 394 382 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 } // if 393 src->forall = 0; 394 } // if 395 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 } // if 403 } // if 395 404 } 396 405 … … 401 410 } // appendError 402 411 403 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData *dst ) {412 void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) { 404 413 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization 405 414 … … 413 422 } // DeclarationNode::checkQualifiers 414 423 415 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {424 void DeclarationNode::checkStorageClasses( DeclarationNode *q ) { 416 425 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { 417 426 if ( storageClass == q->storageClass ) { // duplicate qualifier … … 425 434 } // DeclarationNode::copyStorageClasses 426 435 427 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode *q ) {436 DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) { 428 437 isInline = isInline || q->isInline; 429 438 isNoreturn = isNoreturn || q->isNoreturn; … … 436 445 } // DeclarationNode::copyStorageClasses 437 446 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 ); 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; 454 } else { 455 checkQualifiers( q->type, type ); 456 } // 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 469 } // 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 ); 442 490 } 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 ); 486 } else { 487 type->forall = q->type->forall; 488 } // 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 ); 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 ); 550 523 } // if 551 dst->base->qualifiers |= src->qualifiers;552 src = nullptr;553 524 break; 554 525 default: 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; 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 563 547 } // switch 564 } // switch565 } // if 566 } 567 568 DeclarationNode * DeclarationNode::addType( DeclarationNode *o ) {548 } // if 549 } // if 550 } 551 552 DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) { 569 553 if ( o ) { 570 554 checkStorageClasses( o ); … … 582 566 type = o->type; 583 567 } // if 584 o->type = nullptr;568 o->type = 0; 585 569 } else { 586 570 addTypeToType( o->type, type ); … … 600 584 } 601 585 602 DeclarationNode * DeclarationNode::addTypedef() {603 TypeData * newtype = new TypeData( TypeData::Symbolic );604 newtype->symbolic.params = nullptr;586 DeclarationNode *DeclarationNode::addTypedef() { 587 TypeData *newtype = new TypeData( TypeData::Symbolic ); 588 newtype->symbolic.params = 0; 605 589 newtype->symbolic.isTypedef = true; 606 newtype->symbolic.name = name ? new string( *name ) : nullptr;590 newtype->symbolic.name = name; 607 591 newtype->base = type; 608 592 type = newtype; … … 610 594 } 611 595 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 596 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) { 622 597 assert( type ); 623 598 switch ( type->kind ) { … … 629 604 } // if 630 605 break; 631 //case TypeData::Variable:632 //if ( variable.assertions ) {633 //variable.assertions->appendList( assertions );634 //} else {635 //variable.assertions = assertions;636 //} // if637 //break;606 case TypeData::Variable: 607 if ( variable.assertions ) { 608 variable.assertions->appendList( assertions ); 609 } else { 610 variable.assertions = assertions; 611 } // if 612 break; 638 613 default: 639 614 assert( false ); … … 643 618 } 644 619 645 DeclarationNode * DeclarationNode::addName( std::string * newname ) { 646 assert( ! name ); 647 name = newname; 648 return this; 649 } 650 651 DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { 620 DeclarationNode *DeclarationNode::addName( std::string *newname ) { 621 name = assign_strptr( newname ); 622 return this; 623 } 624 625 DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) { 652 626 bitfieldWidth = size; 653 627 return this; 654 628 } 655 629 656 DeclarationNode * DeclarationNode::addVarArgs() {630 DeclarationNode *DeclarationNode::addVarArgs() { 657 631 assert( type ); 658 632 hasEllipsis = true; … … 660 634 } 661 635 662 DeclarationNode * DeclarationNode::addFunctionBody( StatementNode *body ) {636 DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) { 663 637 assert( type ); 664 638 assert( type->kind == TypeData::Function ); 665 assert( ! type->function.body);639 assert( type->function.body == 0 ); 666 640 type->function.body = body; 667 641 type->function.hasBody = true; … … 669 643 } 670 644 671 DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode *list ) {645 DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) { 672 646 assert( type ); 673 647 assert( type->kind == TypeData::Function ); 674 assert( ! type->function.oldDeclList);648 assert( type->function.oldDeclList == 0 ); 675 649 type->function.oldDeclList = list; 676 650 return this; 677 651 } 678 652 679 static void setBase( TypeData *&type, TypeData * newType ) {653 static void setBase( TypeData *&type, TypeData *newType ) { 680 654 if ( type ) { 681 TypeData * prevBase = type;682 TypeData * curBase = type->base;683 while ( curBase != nullptr) {655 TypeData *prevBase = type; 656 TypeData *curBase = type->base; 657 while ( curBase != 0 ) { 684 658 prevBase = curBase; 685 659 curBase = curBase->base; … … 691 665 } 692 666 693 DeclarationNode * DeclarationNode::addPointer( DeclarationNode *p ) {667 DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) { 694 668 if ( p ) { 695 669 assert( p->type->kind == TypeData::Pointer ); 696 670 setBase( type, p->type ); 697 p->type = nullptr;671 p->type = 0; 698 672 delete p; 699 673 } // if … … 701 675 } 702 676 703 DeclarationNode * DeclarationNode::addArray( DeclarationNode *a ) {677 DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) { 704 678 if ( a ) { 705 679 assert( a->type->kind == TypeData::Array ); 706 680 setBase( type, a->type ); 707 a->type = nullptr;681 a->type = 0; 708 682 delete a; 709 683 } // if … … 711 685 } 712 686 713 DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode *p ) {687 DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) { 714 688 if ( p ) { 715 689 assert( p->type->kind == TypeData::Pointer ); … … 729 703 p->type->base = type; 730 704 } // switch 731 type = nullptr;705 type = 0; 732 706 } // if 733 707 delete this; … … 738 712 } 739 713 740 static TypeData * findLast( TypeData *a ) {714 static TypeData *findLast( TypeData *a ) { 741 715 assert( a ); 742 TypeData * cur = a;716 TypeData *cur = a; 743 717 while ( cur->base ) { 744 718 cur = cur->base; … … 747 721 } 748 722 749 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode *a ) {723 DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) { 750 724 if ( a ) { 751 725 assert( a->type->kind == TypeData::Array ); 752 TypeData * lastArray = findLast( a->type );726 TypeData *lastArray = findLast( a->type ); 753 727 if ( type ) { 754 728 switch ( type->kind ) { … … 765 739 lastArray->base = type; 766 740 } // switch 767 type = nullptr;741 type = 0; 768 742 } // if 769 743 delete this; … … 774 748 } 775 749 776 DeclarationNode * DeclarationNode::addParamList( DeclarationNode *params ) {777 TypeData * ftype = new TypeData( TypeData::Function );750 DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) { 751 TypeData *ftype = new TypeData( TypeData::Function ); 778 752 ftype->function.params = params; 779 753 setBase( type, ftype ); … … 781 755 } 782 756 783 static TypeData * addIdListToType( TypeData * type, DeclarationNode *ids ) {757 static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) { 784 758 if ( type ) { 785 759 if ( type->kind != TypeData::Function ) { … … 790 764 return type; 791 765 } else { 792 TypeData * newtype = new TypeData( TypeData::Function );766 TypeData *newtype = new TypeData( TypeData::Function ); 793 767 newtype->function.idList = ids; 794 768 return newtype; 795 769 } // if 796 } // addIdListToType797 798 DeclarationNode * DeclarationNode::addIdList( DeclarationNode *ids ) {770 } 771 772 DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) { 799 773 type = addIdListToType( type, ids ); 800 774 return this; 801 775 } 802 776 803 DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { 777 DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) { 778 //assert 804 779 initializer = init; 805 780 return this; 806 781 } 807 782 808 DeclarationNode * DeclarationNode::cloneType( string * newName ) { 809 DeclarationNode * newnode = new DeclarationNode; 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; 810 842 newnode->type = maybeClone( type ); 811 843 assert( storageClass == NoStorageClass ); 812 844 newnode->copyStorageClasses( this ); 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 ); 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; 857 } else { 858 addTypeToType( newType, o->type ); 859 delete newType; 860 } // if 861 } // if 862 } // if 863 delete o; 864 return o; 865 } 866 867 DeclarationNode *DeclarationNode::extractAggregate() const { 822 868 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; 835 } else { 836 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); 837 delete newType->aggInst.aggregate->aggregate.fields; 838 newType->aggInst.aggregate->aggregate.fields = nullptr; 839 } // if 840 } // if 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 850 return o; 851 } 852 853 DeclarationNode * DeclarationNode::extractAggregate() const { 854 if ( type ) { 855 TypeData * ret = typeextractAggregate( type ); 869 TypeData *ret = typeextractAggregate( type ); 856 870 if ( ret ) { 857 DeclarationNode * newnode = new DeclarationNode;871 DeclarationNode *newnode = new DeclarationNode; 858 872 newnode->type = ret; 859 873 return newnode; 860 874 } // if 861 875 } // if 862 return nullptr;863 } 864 865 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {876 return 0; 877 } 878 879 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) { 866 880 SemanticError errors; 867 881 std::back_insert_iterator< std::list< Declaration * > > out( outputList ); 868 const DeclarationNode * cur = firstNode; 869 882 const DeclarationNode *cur = firstNode; 870 883 while ( cur ) { 871 884 try { 872 if ( DeclarationNode * extr = cur->extractAggregate() ) {885 if ( DeclarationNode *extr = cur->extractAggregate() ) { 873 886 // handle the case where a structure declaration is contained within an object or type declaration 874 Declaration * decl = extr->build();887 Declaration *decl = extr->build(); 875 888 if ( decl ) { 876 * out++ = decl;889 *out++ = decl; 877 890 } // if 878 891 delete extr; 879 892 } // if 880 881 Declaration * decl = cur->build(); 893 Declaration *decl = cur->build(); 882 894 if ( decl ) { 883 * out++ = decl; 884 } // if 885 } catch( SemanticError &e ) { 886 errors.append( e ); 887 } // try 888 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 889 } // while 890 891 if ( ! errors.isEmpty() ) { 892 throw errors; 893 } // if 894 } // buildList 895 896 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 897 SemanticError errors; 898 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 899 const DeclarationNode * cur = firstNode; 900 while ( cur ) { 901 try { 902 Declaration * decl = cur->build(); 903 if ( decl ) { 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 ); 909 delete agg; 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 ); 913 } // if 895 *out++ = decl; 914 896 } // if 915 897 } catch( SemanticError &e ) { … … 921 903 throw errors; 922 904 } // if 923 } // buildList924 925 void build TypeList( const DeclarationNode * firstNode, std::list<Type * > &outputList ) {905 } 906 907 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) { 926 908 SemanticError errors; 927 std::back_insert_iterator< std::list< Type * > > out( outputList ); 928 const DeclarationNode * cur = firstNode; 929 909 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 910 const DeclarationNode *cur = firstNode; 930 911 while ( cur ) { 931 912 try { 932 * out++ = cur->buildType(); 913 Declaration *decl = cur->build(); 914 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 ); 920 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 ); 924 } // if 925 } // if 933 926 } catch( SemanticError &e ) { 934 927 errors.append( e ); … … 936 929 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 937 930 } // while 938 939 931 if ( ! errors.isEmpty() ) { 940 932 throw errors; 941 933 } // if 942 } // buildTypeList 943 944 Declaration * DeclarationNode::build() const { 934 } 935 936 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) { 937 SemanticError errors; 938 std::back_insert_iterator< std::list< Type * > > out( outputList ); 939 const DeclarationNode *cur = firstNode; 940 while ( cur ) { 941 try { 942 *out++ = cur->buildType(); 943 } catch( SemanticError &e ) { 944 errors.append( e ); 945 } // try 946 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 947 } // while 948 if ( ! errors.isEmpty() ) { 949 throw errors; 950 } // if 951 } 952 953 Declaration *DeclarationNode::build() const { 945 954 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 } // if953 954 955 if ( type ) { 955 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension ); 956 } // if 957 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; 961 } else { 962 return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension ); 963 } // if 964 } // if 958 965 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 966 return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension ); 967 } // if 963 968 throw SemanticError( "invalid function specifier ", this ); 964 969 } 965 970 966 Type * DeclarationNode::buildType() const {971 Type *DeclarationNode::buildType() const { 967 972 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() );973 } else {974 assert( attr.type );975 ret = new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );976 } // if977 return ret;978 } // if979 973 980 974 switch ( type->kind ) { 981 975 case TypeData::Enum: 982 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );976 return new EnumInstType( buildQualifiers( type ), type->enumeration.name ); 983 977 case TypeData::Aggregate: { 984 ReferenceToType * ret;978 ReferenceToType *ret; 985 979 switch ( type->aggregate.kind ) { 986 980 case DeclarationNode::Struct: 987 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );981 ret = new StructInstType( buildQualifiers( type ), type->aggregate.name ); 988 982 break; 989 983 case DeclarationNode::Union: 990 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );984 ret = new UnionInstType( buildQualifiers( type ), type->aggregate.name ); 991 985 break; 992 986 case DeclarationNode::Trait: 993 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );987 ret = new TraitInstType( buildQualifiers( type ), type->aggregate.name ); 994 988 break; 995 989 default: … … 1000 994 } 1001 995 case TypeData::Symbolic: { 1002 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false );996 TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic.name, false ); 1003 997 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 } // if 1004 1010 return ret; 1005 1011 }
Note:
See TracChangeset
for help on using the changeset viewer.