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