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