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