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