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