source: src/Parser/DeclarationNode.cc@ d95f565

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since d95f565 was fb114fa1, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

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