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