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