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