| 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 : Fri Jul 20 14:56:54 2018
|
|---|
| 13 | // Update Count : 1107
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <cassert> // for assert, assertf, strict_dynamic_cast
|
|---|
| 17 | #include <iterator> // for back_insert_iterator
|
|---|
| 18 | #include <list> // for list
|
|---|
| 19 | #include <memory> // for unique_ptr
|
|---|
| 20 | #include <ostream> // for operator<<, ostream, basic_ostream
|
|---|
| 21 | #include <string> // for string, operator+, allocator, char...
|
|---|
| 22 |
|
|---|
| 23 | #include "Common/SemanticError.h" // for SemanticError
|
|---|
| 24 | #include "Common/UniqueName.h" // for UniqueName
|
|---|
| 25 | #include "Common/utility.h" // for maybeClone, maybeBuild, CodeLocation
|
|---|
| 26 | #include "Parser/LinkageSpec.h" // for Spec, linkageName, Cforall
|
|---|
| 27 | #include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
|
|---|
| 28 | #include "SynTree/Attribute.h" // for Attribute
|
|---|
| 29 | #include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, Declaration
|
|---|
| 30 | #include "SynTree/Expression.h" // for Expression, ConstantExpr
|
|---|
| 31 | #include "SynTree/Statement.h" // for AsmStmt
|
|---|
| 32 | #include "SynTree/Type.h" // for Type, Type::StorageClasses, Type::...
|
|---|
| 33 | #include "TypeData.h" // for TypeData, TypeData::Aggregate_t
|
|---|
| 34 | #include "TypedefTable.h" // for TypedefTable
|
|---|
| 35 |
|
|---|
| 36 | class Initializer;
|
|---|
| 37 |
|
|---|
| 38 | extern TypedefTable typedefTable;
|
|---|
| 39 |
|
|---|
| 40 | using namespace std;
|
|---|
| 41 |
|
|---|
| 42 | // These must harmonize with the corresponding DeclarationNode enumerations.
|
|---|
| 43 | const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" };
|
|---|
| 44 | const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
|
|---|
| 45 | const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
|
|---|
| 46 | const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
|
|---|
| 47 | const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" };
|
|---|
| 48 | const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
|
|---|
| 49 | const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "zero_t", "one_t", "NoBuiltinTypeNames" };
|
|---|
| 50 |
|
|---|
| 51 | UniqueName DeclarationNode::anonymous( "__anonymous" );
|
|---|
| 52 |
|
|---|
| 53 | extern LinkageSpec::Spec linkage; // defined in parser.yy
|
|---|
| 54 |
|
|---|
| 55 | DeclarationNode::DeclarationNode() :
|
|---|
| 56 | linkage( ::linkage ) {
|
|---|
| 57 |
|
|---|
| 58 | // variable.name = nullptr;
|
|---|
| 59 | variable.tyClass = NoTypeClass;
|
|---|
| 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::linkageName( 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( 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( TypeClass 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 = 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 = 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 ) {
|
|---|
| 405 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 406 | newnode->type = new TypeData( 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::newAttr( const string * name, ExpressionNode * expr ) {
|
|---|
| 420 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 421 | newnode->type = nullptr;
|
|---|
| 422 | // newnode->attr.name = name;
|
|---|
| 423 | newnode->name = name;
|
|---|
| 424 | newnode->attr.expr = expr;
|
|---|
| 425 | return newnode;
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | DeclarationNode * DeclarationNode::newAttr( const string * name, DeclarationNode * type ) {
|
|---|
| 429 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 430 | newnode->type = nullptr;
|
|---|
| 431 | // newnode->attr.name = name;
|
|---|
| 432 | newnode->name = name;
|
|---|
| 433 | newnode->attr.type = type;
|
|---|
| 434 | return newnode;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
|
|---|
| 438 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 439 | newnode->type = nullptr;
|
|---|
| 440 | std::list< Expression * > exprs;
|
|---|
| 441 | buildList( expr, exprs );
|
|---|
| 442 | newnode->attributes.push_back( new Attribute( *name, exprs ) );
|
|---|
| 443 | delete name;
|
|---|
| 444 | return newnode;
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
|
|---|
| 448 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 449 | newnode->asmStmt = stmt;
|
|---|
| 450 | return newnode;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, Expression * message ) {
|
|---|
| 454 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 455 | newnode->assert.condition = condition;
|
|---|
| 456 | newnode->assert.message = message;
|
|---|
| 457 | return newnode;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 | void appendError( string & dst, const string & src ) {
|
|---|
| 462 | if ( src.empty() ) return;
|
|---|
| 463 | if ( dst.empty() ) { dst = src; return; }
|
|---|
| 464 | dst += ", " + src;
|
|---|
| 465 | } // appendError
|
|---|
| 466 |
|
|---|
| 467 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
|
|---|
| 468 | const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
|
|---|
| 469 |
|
|---|
| 470 | if ( (qsrc & qdst).any() ) { // duplicates ?
|
|---|
| 471 | for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
|
|---|
| 472 | if ( qsrc[i] && qdst[i] ) {
|
|---|
| 473 | appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
|
|---|
| 474 | } // if
|
|---|
| 475 | } // for
|
|---|
| 476 | } // for
|
|---|
| 477 | } // DeclarationNode::checkQualifiers
|
|---|
| 478 |
|
|---|
| 479 | void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
|
|---|
| 480 | if ( (funcSpecs & src->funcSpecs).any() ) { // duplicates ?
|
|---|
| 481 | for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
|
|---|
| 482 | if ( funcSpecs[i] && src->funcSpecs[i] ) {
|
|---|
| 483 | appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
|
|---|
| 484 | } // if
|
|---|
| 485 | } // for
|
|---|
| 486 | } // if
|
|---|
| 487 |
|
|---|
| 488 | if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
|
|---|
| 489 | if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
|
|---|
| 490 | for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
|
|---|
| 491 | if ( storageClasses[i] && src->storageClasses[i] ) {
|
|---|
| 492 | appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
|
|---|
| 493 | } // if
|
|---|
| 494 | } // for
|
|---|
| 495 | // src is the new item being added and has a single bit
|
|---|
| 496 | } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
|
|---|
| 497 | appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
|
|---|
| 498 | " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
|
|---|
| 499 | src->storageClasses.reset(); // FIX to preserve invariant of one basic storage specifier
|
|---|
| 500 | } // if
|
|---|
| 501 | } // if
|
|---|
| 502 |
|
|---|
| 503 | appendError( error, src->error );
|
|---|
| 504 | } // DeclarationNode::checkSpecifiers
|
|---|
| 505 |
|
|---|
| 506 | DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
|
|---|
| 507 | funcSpecs |= q->funcSpecs;
|
|---|
| 508 | storageClasses |= q->storageClasses;
|
|---|
| 509 |
|
|---|
| 510 | for ( Attribute *attr: reverseIterate( q->attributes ) ) {
|
|---|
| 511 | attributes.push_front( attr->clone() );
|
|---|
| 512 | } // for
|
|---|
| 513 | return this;
|
|---|
| 514 | } // DeclarationNode::copySpecifiers
|
|---|
| 515 |
|
|---|
| 516 | static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
|
|---|
| 517 | if ( dst->base ) {
|
|---|
| 518 | addQualifiersToType( src, dst->base );
|
|---|
| 519 | } else if ( dst->kind == TypeData::Function ) {
|
|---|
| 520 | dst->base = src;
|
|---|
| 521 | src = nullptr;
|
|---|
| 522 | } else {
|
|---|
| 523 | dst->qualifiers |= src->qualifiers;
|
|---|
| 524 | } // if
|
|---|
| 525 | } // addQualifiersToType
|
|---|
| 526 |
|
|---|
| 527 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
|
|---|
| 528 | if ( ! q ) { return this; } // empty qualifier
|
|---|
| 529 |
|
|---|
| 530 | checkSpecifiers( q );
|
|---|
| 531 | copySpecifiers( q );
|
|---|
| 532 |
|
|---|
| 533 | if ( ! q->type ) { delete q; return this; }
|
|---|
| 534 |
|
|---|
| 535 | if ( ! type ) {
|
|---|
| 536 | type = q->type; // reuse structure
|
|---|
| 537 | q->type = nullptr;
|
|---|
| 538 | delete q;
|
|---|
| 539 | return this;
|
|---|
| 540 | } // if
|
|---|
| 541 |
|
|---|
| 542 | if ( q->type->forall ) { // forall qualifier ?
|
|---|
| 543 | if ( type->forall ) { // polymorphic routine ?
|
|---|
| 544 | type->forall->appendList( q->type->forall ); // augment forall qualifier
|
|---|
| 545 | } else {
|
|---|
| 546 | if ( type->kind == TypeData::Aggregate ) { // struct/union ?
|
|---|
| 547 | if ( type->aggregate.params ) { // polymorphic ?
|
|---|
| 548 | type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
|
|---|
| 549 | } else { // not polymorphic
|
|---|
| 550 | type->aggregate.params = q->type->forall; // set forall qualifier
|
|---|
| 551 | } // if
|
|---|
| 552 | } else { // not polymorphic
|
|---|
| 553 | type->forall = q->type->forall; // make polymorphic routine
|
|---|
| 554 | } // if
|
|---|
| 555 | } // if
|
|---|
| 556 | q->type->forall = nullptr; // forall qualifier moved
|
|---|
| 557 | } // if
|
|---|
| 558 |
|
|---|
| 559 | checkQualifiers( type, q->type );
|
|---|
| 560 | if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
|
|---|
| 561 | SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
|
|---|
| 562 | } // if
|
|---|
| 563 | addQualifiersToType( q->type, type );
|
|---|
| 564 |
|
|---|
| 565 | delete q;
|
|---|
| 566 | return this;
|
|---|
| 567 | } // addQualifiers
|
|---|
| 568 |
|
|---|
| 569 | static void addTypeToType( TypeData *& src, TypeData *& dst ) {
|
|---|
| 570 | if ( src->forall && dst->kind == TypeData::Function ) {
|
|---|
| 571 | if ( dst->forall ) {
|
|---|
| 572 | dst->forall->appendList( src->forall );
|
|---|
| 573 | } else {
|
|---|
| 574 | dst->forall = src->forall;
|
|---|
| 575 | } // if
|
|---|
| 576 | src->forall = nullptr;
|
|---|
| 577 | } // if
|
|---|
| 578 | if ( dst->base ) {
|
|---|
| 579 | addTypeToType( src, dst->base );
|
|---|
| 580 | } else {
|
|---|
| 581 | switch ( dst->kind ) {
|
|---|
| 582 | case TypeData::Unknown:
|
|---|
| 583 | src->qualifiers |= dst->qualifiers;
|
|---|
| 584 | dst = src;
|
|---|
| 585 | src = nullptr;
|
|---|
| 586 | break;
|
|---|
| 587 | case TypeData::Basic:
|
|---|
| 588 | dst->qualifiers |= src->qualifiers;
|
|---|
| 589 | if ( src->kind != TypeData::Unknown ) {
|
|---|
| 590 | assert( src->kind == TypeData::Basic );
|
|---|
| 591 |
|
|---|
| 592 | if ( dst->basictype == DeclarationNode::NoBasicType ) {
|
|---|
| 593 | dst->basictype = src->basictype;
|
|---|
| 594 | } else if ( src->basictype != DeclarationNode::NoBasicType )
|
|---|
| 595 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
|
|---|
| 596 |
|
|---|
| 597 | if ( dst->complextype == DeclarationNode::NoComplexType ) {
|
|---|
| 598 | dst->complextype = src->complextype;
|
|---|
| 599 | } else if ( src->complextype != DeclarationNode::NoComplexType )
|
|---|
| 600 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
|
|---|
| 601 |
|
|---|
| 602 | if ( dst->signedness == DeclarationNode::NoSignedness ) {
|
|---|
| 603 | dst->signedness = src->signedness;
|
|---|
| 604 | } else if ( src->signedness != DeclarationNode::NoSignedness )
|
|---|
| 605 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
|
|---|
| 606 |
|
|---|
| 607 | if ( dst->length == DeclarationNode::NoLength ) {
|
|---|
| 608 | dst->length = src->length;
|
|---|
| 609 | } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
|
|---|
| 610 | dst->length = DeclarationNode::LongLong;
|
|---|
| 611 | } else if ( src->length != DeclarationNode::NoLength )
|
|---|
| 612 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
|
|---|
| 613 | } // if
|
|---|
| 614 | break;
|
|---|
| 615 | default:
|
|---|
| 616 | switch ( src->kind ) {
|
|---|
| 617 | case TypeData::Aggregate:
|
|---|
| 618 | case TypeData::Enum:
|
|---|
| 619 | dst->base = new TypeData( TypeData::AggregateInst );
|
|---|
| 620 | dst->base->aggInst.aggregate = src;
|
|---|
| 621 | if ( src->kind == TypeData::Aggregate ) {
|
|---|
| 622 | dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
|
|---|
| 623 | } // if
|
|---|
| 624 | dst->base->qualifiers |= src->qualifiers;
|
|---|
| 625 | src = nullptr;
|
|---|
| 626 | break;
|
|---|
| 627 | default:
|
|---|
| 628 | if ( dst->forall ) {
|
|---|
| 629 | dst->forall->appendList( src->forall );
|
|---|
| 630 | } else {
|
|---|
| 631 | dst->forall = src->forall;
|
|---|
| 632 | } // if
|
|---|
| 633 | src->forall = nullptr;
|
|---|
| 634 | dst->base = src;
|
|---|
| 635 | src = nullptr;
|
|---|
| 636 | } // switch
|
|---|
| 637 | } // switch
|
|---|
| 638 | } // if
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
|
|---|
| 642 | if ( o ) {
|
|---|
| 643 | checkSpecifiers( o );
|
|---|
| 644 | copySpecifiers( o );
|
|---|
| 645 | if ( o->type ) {
|
|---|
| 646 | if ( ! type ) {
|
|---|
| 647 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
|
|---|
| 648 | type = new TypeData( TypeData::AggregateInst );
|
|---|
| 649 | type->aggInst.aggregate = o->type;
|
|---|
| 650 | if ( o->type->kind == TypeData::Aggregate ) {
|
|---|
| 651 | type->aggInst.hoistType = o->type->aggregate.body;
|
|---|
| 652 | type->aggInst.params = maybeClone( o->type->aggregate.actuals );
|
|---|
| 653 | } else {
|
|---|
| 654 | type->aggInst.hoistType = o->type->enumeration.body;
|
|---|
| 655 | } // if
|
|---|
| 656 | type->qualifiers |= o->type->qualifiers;
|
|---|
| 657 | } else {
|
|---|
| 658 | type = o->type;
|
|---|
| 659 | } // if
|
|---|
| 660 | o->type = nullptr;
|
|---|
| 661 | } else {
|
|---|
| 662 | addTypeToType( o->type, type );
|
|---|
| 663 | } // if
|
|---|
| 664 | } // if
|
|---|
| 665 | if ( o->bitfieldWidth ) {
|
|---|
| 666 | bitfieldWidth = o->bitfieldWidth;
|
|---|
| 667 | } // if
|
|---|
| 668 |
|
|---|
| 669 | // there may be typedefs chained onto the type
|
|---|
| 670 | if ( o->get_next() ) {
|
|---|
| 671 | set_last( o->get_next()->clone() );
|
|---|
| 672 | } // if
|
|---|
| 673 | } // if
|
|---|
| 674 | delete o;
|
|---|
| 675 | return this;
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | DeclarationNode * DeclarationNode::addTypedef() {
|
|---|
| 679 | TypeData * newtype = new TypeData( TypeData::Symbolic );
|
|---|
| 680 | newtype->symbolic.params = nullptr;
|
|---|
| 681 | newtype->symbolic.isTypedef = true;
|
|---|
| 682 | newtype->symbolic.name = name ? new string( *name ) : nullptr;
|
|---|
| 683 | newtype->base = type;
|
|---|
| 684 | type = newtype;
|
|---|
| 685 | return this;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
|
|---|
| 689 | if ( variable.tyClass != NoTypeClass ) {
|
|---|
| 690 | if ( variable.assertions ) {
|
|---|
| 691 | variable.assertions->appendList( assertions );
|
|---|
| 692 | } else {
|
|---|
| 693 | variable.assertions = assertions;
|
|---|
| 694 | } // if
|
|---|
| 695 | return this;
|
|---|
| 696 | } // if
|
|---|
| 697 |
|
|---|
| 698 | assert( type );
|
|---|
| 699 | switch ( type->kind ) {
|
|---|
| 700 | case TypeData::Symbolic:
|
|---|
| 701 | if ( type->symbolic.assertions ) {
|
|---|
| 702 | type->symbolic.assertions->appendList( assertions );
|
|---|
| 703 | } else {
|
|---|
| 704 | type->symbolic.assertions = assertions;
|
|---|
| 705 | } // if
|
|---|
| 706 | break;
|
|---|
| 707 | default:
|
|---|
| 708 | assert( false );
|
|---|
| 709 | } // switch
|
|---|
| 710 |
|
|---|
| 711 | return this;
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | DeclarationNode * DeclarationNode::addName( string * newname ) {
|
|---|
| 715 | assert( ! name );
|
|---|
| 716 | name = newname;
|
|---|
| 717 | return this;
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
|
|---|
| 721 | assert( ! asmName );
|
|---|
| 722 | asmName = newname ? newname->asmName : nullptr;
|
|---|
| 723 | return this->addQualifiers( newname );
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
|
|---|
| 727 | bitfieldWidth = size;
|
|---|
| 728 | return this;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | DeclarationNode * DeclarationNode::addVarArgs() {
|
|---|
| 732 | assert( type );
|
|---|
| 733 | hasEllipsis = true;
|
|---|
| 734 | return this;
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
|
|---|
| 738 | assert( type );
|
|---|
| 739 | assert( type->kind == TypeData::Function );
|
|---|
| 740 | assert( ! type->function.body );
|
|---|
| 741 | type->function.body = body;
|
|---|
| 742 | type->function.withExprs = withExprs;
|
|---|
| 743 | return this;
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
|
|---|
| 747 | assert( type );
|
|---|
| 748 | assert( type->kind == TypeData::Function );
|
|---|
| 749 | assert( ! type->function.oldDeclList );
|
|---|
| 750 | type->function.oldDeclList = list;
|
|---|
| 751 | return this;
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
|
|---|
| 755 | if ( type ) {
|
|---|
| 756 | TypeData * prevBase = type;
|
|---|
| 757 | TypeData * curBase = type->base;
|
|---|
| 758 | while ( curBase != nullptr ) {
|
|---|
| 759 | prevBase = curBase;
|
|---|
| 760 | curBase = curBase->base;
|
|---|
| 761 | } // while
|
|---|
| 762 | prevBase->base = newType;
|
|---|
| 763 | } else {
|
|---|
| 764 | type = newType;
|
|---|
| 765 | } // if
|
|---|
| 766 | return this;
|
|---|
| 767 | }
|
|---|
| 768 |
|
|---|
| 769 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
|
|---|
| 770 | if ( a ) {
|
|---|
| 771 | for ( Attribute *attr: reverseIterate( a->attributes ) ) {
|
|---|
| 772 | attributes.push_front( attr );
|
|---|
| 773 | } // for
|
|---|
| 774 | a->attributes.clear();
|
|---|
| 775 | } // if
|
|---|
| 776 | return this;
|
|---|
| 777 | } // copyAttribute
|
|---|
| 778 |
|
|---|
| 779 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
|
|---|
| 780 | if ( p ) {
|
|---|
| 781 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
|
|---|
| 782 | setBase( p->type );
|
|---|
| 783 | p->type = nullptr;
|
|---|
| 784 | copyAttribute( p );
|
|---|
| 785 | delete p;
|
|---|
| 786 | } // if
|
|---|
| 787 | return this;
|
|---|
| 788 | }
|
|---|
| 789 |
|
|---|
| 790 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
|
|---|
| 791 | if ( a ) {
|
|---|
| 792 | assert( a->type->kind == TypeData::Array );
|
|---|
| 793 | setBase( a->type );
|
|---|
| 794 | a->type = nullptr;
|
|---|
| 795 | copyAttribute( a );
|
|---|
| 796 | delete a;
|
|---|
| 797 | } // if
|
|---|
| 798 | return this;
|
|---|
| 799 | }
|
|---|
| 800 |
|
|---|
| 801 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
|
|---|
| 802 | if ( p ) {
|
|---|
| 803 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
|
|---|
| 804 | if ( type ) {
|
|---|
| 805 | switch ( type->kind ) {
|
|---|
| 806 | case TypeData::Aggregate:
|
|---|
| 807 | case TypeData::Enum:
|
|---|
| 808 | p->type->base = new TypeData( TypeData::AggregateInst );
|
|---|
| 809 | p->type->base->aggInst.aggregate = type;
|
|---|
| 810 | if ( type->kind == TypeData::Aggregate ) {
|
|---|
| 811 | p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
|
|---|
| 812 | } // if
|
|---|
| 813 | p->type->base->qualifiers |= type->qualifiers;
|
|---|
| 814 | break;
|
|---|
| 815 |
|
|---|
| 816 | default:
|
|---|
| 817 | p->type->base = type;
|
|---|
| 818 | } // switch
|
|---|
| 819 | type = nullptr;
|
|---|
| 820 | } // if
|
|---|
| 821 | delete this;
|
|---|
| 822 | return p;
|
|---|
| 823 | } else {
|
|---|
| 824 | return this;
|
|---|
| 825 | } // if
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | static TypeData * findLast( TypeData * a ) {
|
|---|
| 829 | assert( a );
|
|---|
| 830 | TypeData * cur = a;
|
|---|
| 831 | while ( cur->base ) {
|
|---|
| 832 | cur = cur->base;
|
|---|
| 833 | } // while
|
|---|
| 834 | return cur;
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
|
|---|
| 838 | if ( ! a ) return this;
|
|---|
| 839 | assert( a->type->kind == TypeData::Array );
|
|---|
| 840 | TypeData * lastArray = findLast( a->type );
|
|---|
| 841 | if ( type ) {
|
|---|
| 842 | switch ( type->kind ) {
|
|---|
| 843 | case TypeData::Aggregate:
|
|---|
| 844 | case TypeData::Enum:
|
|---|
| 845 | lastArray->base = new TypeData( TypeData::AggregateInst );
|
|---|
| 846 | lastArray->base->aggInst.aggregate = type;
|
|---|
| 847 | if ( type->kind == TypeData::Aggregate ) {
|
|---|
| 848 | lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
|
|---|
| 849 | } // if
|
|---|
| 850 | lastArray->base->qualifiers |= type->qualifiers;
|
|---|
| 851 | break;
|
|---|
| 852 | default:
|
|---|
| 853 | lastArray->base = type;
|
|---|
| 854 | } // switch
|
|---|
| 855 | type = nullptr;
|
|---|
| 856 | } // if
|
|---|
| 857 | delete this;
|
|---|
| 858 | return a;
|
|---|
| 859 | }
|
|---|
| 860 |
|
|---|
| 861 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
|
|---|
| 862 | TypeData * ftype = new TypeData( TypeData::Function );
|
|---|
| 863 | ftype->function.params = params;
|
|---|
| 864 | setBase( ftype );
|
|---|
| 865 | return this;
|
|---|
| 866 | }
|
|---|
| 867 |
|
|---|
| 868 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
|
|---|
| 869 | if ( type ) {
|
|---|
| 870 | if ( type->kind != TypeData::Function ) {
|
|---|
| 871 | type->base = addIdListToType( type->base, ids );
|
|---|
| 872 | } else {
|
|---|
| 873 | type->function.idList = ids;
|
|---|
| 874 | } // if
|
|---|
| 875 | return type;
|
|---|
| 876 | } else {
|
|---|
| 877 | TypeData * newtype = new TypeData( TypeData::Function );
|
|---|
| 878 | newtype->function.idList = ids;
|
|---|
| 879 | return newtype;
|
|---|
| 880 | } // if
|
|---|
| 881 | } // addIdListToType
|
|---|
| 882 |
|
|---|
| 883 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
|
|---|
| 884 | type = addIdListToType( type, ids );
|
|---|
| 885 | return this;
|
|---|
| 886 | }
|
|---|
| 887 |
|
|---|
| 888 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
|
|---|
| 889 | initializer = init;
|
|---|
| 890 | return this;
|
|---|
| 891 | }
|
|---|
| 892 |
|
|---|
| 893 | DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
|
|---|
| 894 | assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
|
|---|
| 895 | variable.initializer = init;
|
|---|
| 896 | return this;
|
|---|
| 897 | }
|
|---|
| 898 |
|
|---|
| 899 | DeclarationNode * DeclarationNode::cloneType( string * newName ) {
|
|---|
| 900 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 901 | newnode->type = maybeClone( type );
|
|---|
| 902 | newnode->copySpecifiers( this );
|
|---|
| 903 | assert( newName );
|
|---|
| 904 | newnode->name = newName;
|
|---|
| 905 | return newnode;
|
|---|
| 906 | }
|
|---|
| 907 |
|
|---|
| 908 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
|
|---|
| 909 | if ( ! o ) return nullptr;
|
|---|
| 910 |
|
|---|
| 911 | o->copySpecifiers( this );
|
|---|
| 912 | if ( type ) {
|
|---|
| 913 | TypeData * srcType = type;
|
|---|
| 914 |
|
|---|
| 915 | // search for the base type by scanning off pointers and array designators
|
|---|
| 916 | while ( srcType->base ) {
|
|---|
| 917 | srcType = srcType->base;
|
|---|
| 918 | } // while
|
|---|
| 919 |
|
|---|
| 920 | TypeData * newType = srcType->clone();
|
|---|
| 921 | if ( newType->kind == TypeData::AggregateInst ) {
|
|---|
| 922 | // don't duplicate members
|
|---|
| 923 | if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
|
|---|
| 924 | delete newType->aggInst.aggregate->enumeration.constants;
|
|---|
| 925 | newType->aggInst.aggregate->enumeration.constants = nullptr;
|
|---|
| 926 | newType->aggInst.aggregate->enumeration.body = false;
|
|---|
| 927 | } else {
|
|---|
| 928 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
|
|---|
| 929 | delete newType->aggInst.aggregate->aggregate.fields;
|
|---|
| 930 | newType->aggInst.aggregate->aggregate.fields = nullptr;
|
|---|
| 931 | newType->aggInst.aggregate->aggregate.body = false;
|
|---|
| 932 | } // if
|
|---|
| 933 | // don't hoist twice
|
|---|
| 934 | newType->aggInst.hoistType = false;
|
|---|
| 935 | } // if
|
|---|
| 936 |
|
|---|
| 937 | newType->forall = maybeClone( type->forall );
|
|---|
| 938 | if ( ! o->type ) {
|
|---|
| 939 | o->type = newType;
|
|---|
| 940 | } else {
|
|---|
| 941 | addTypeToType( newType, o->type );
|
|---|
| 942 | delete newType;
|
|---|
| 943 | } // if
|
|---|
| 944 | } // if
|
|---|
| 945 | return o;
|
|---|
| 946 | }
|
|---|
| 947 |
|
|---|
| 948 | DeclarationNode * DeclarationNode::extractAggregate() const {
|
|---|
| 949 | if ( type ) {
|
|---|
| 950 | TypeData * ret = typeextractAggregate( type );
|
|---|
| 951 | if ( ret ) {
|
|---|
| 952 | DeclarationNode * newnode = new DeclarationNode;
|
|---|
| 953 | newnode->type = ret;
|
|---|
| 954 | return newnode;
|
|---|
| 955 | } // if
|
|---|
| 956 | } // if
|
|---|
| 957 | return nullptr;
|
|---|
| 958 | }
|
|---|
| 959 |
|
|---|
| 960 | void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ) {
|
|---|
| 961 | SemanticErrorException errors;
|
|---|
| 962 | std::back_insert_iterator< std::list< Declaration * > > out( outputList );
|
|---|
| 963 |
|
|---|
| 964 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
|
|---|
| 965 | try {
|
|---|
| 966 | bool extracted = false;
|
|---|
| 967 | bool anon = false;
|
|---|
| 968 | if ( DeclarationNode * extr = cur->extractAggregate() ) {
|
|---|
| 969 | // handle the case where a structure declaration is contained within an object or type declaration
|
|---|
| 970 | Declaration * decl = extr->build();
|
|---|
| 971 | if ( decl ) {
|
|---|
| 972 | // hoist the structure declaration
|
|---|
| 973 | decl->location = cur->location;
|
|---|
| 974 | * out++ = decl;
|
|---|
| 975 |
|
|---|
| 976 | // need to remember the cases where a declaration contains an anonymous aggregate definition
|
|---|
| 977 | extracted = true;
|
|---|
| 978 | assert( extr->type );
|
|---|
| 979 | if ( extr->type->kind == TypeData::Aggregate ) {
|
|---|
| 980 | anon = extr->type->aggregate.anon;
|
|---|
| 981 | } else if ( extr->type->kind == TypeData::Enum ) {
|
|---|
| 982 | // xxx - is it useful to have an implicit anonymous enum member?
|
|---|
| 983 | anon = extr->type->enumeration.anon;
|
|---|
| 984 | }
|
|---|
| 985 | } // if
|
|---|
| 986 | delete extr;
|
|---|
| 987 | } // if
|
|---|
| 988 |
|
|---|
| 989 | Declaration * decl = cur->build();
|
|---|
| 990 | if ( decl ) {
|
|---|
| 991 | // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.:
|
|---|
| 992 | // struct S {
|
|---|
| 993 | // struct T { int x; }; // no anonymous member
|
|---|
| 994 | // struct { int y; }; // anonymous member
|
|---|
| 995 | // struct T; // anonymous member
|
|---|
| 996 | // };
|
|---|
| 997 | if ( ! (extracted && decl->name == "" && ! anon && ! cur->get_inLine()) ) {
|
|---|
| 998 | if ( decl->name == "" ) {
|
|---|
| 999 | if ( DeclarationWithType * dwt = dynamic_cast<DeclarationWithType *>( decl ) ) {
|
|---|
| 1000 | if ( ReferenceToType * aggr = dynamic_cast<ReferenceToType *>( dwt->get_type() ) ) {
|
|---|
| 1001 | if ( aggr->name.find("anonymous") == std::string::npos ) {
|
|---|
| 1002 | if ( ! cur->get_inLine() ) {
|
|---|
| 1003 | // temporary: warn about anonymous member declarations of named types, since
|
|---|
| 1004 | // this conflicts with the syntax for the forward declaration of an anonymous type
|
|---|
| 1005 | SemanticWarning( cur->location, Warning::AggrForwardDecl, aggr->name.c_str() );
|
|---|
| 1006 | } // if
|
|---|
| 1007 | } // if
|
|---|
| 1008 | } // if
|
|---|
| 1009 | } // if
|
|---|
| 1010 | } // if
|
|---|
| 1011 | decl->location = cur->location;
|
|---|
| 1012 | *out++ = decl;
|
|---|
| 1013 | } // if
|
|---|
| 1014 | } // if
|
|---|
| 1015 | } catch( SemanticErrorException & e ) {
|
|---|
| 1016 | errors.append( e );
|
|---|
| 1017 | } // try
|
|---|
| 1018 | } // for
|
|---|
| 1019 |
|
|---|
| 1020 | if ( ! errors.isEmpty() ) {
|
|---|
| 1021 | throw errors;
|
|---|
| 1022 | } // if
|
|---|
| 1023 | } // buildList
|
|---|
| 1024 |
|
|---|
| 1025 | // currently only builds assertions, function parameters, and return values
|
|---|
| 1026 | void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > & outputList ) {
|
|---|
| 1027 | SemanticErrorException errors;
|
|---|
| 1028 | std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
|
|---|
| 1029 |
|
|---|
| 1030 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
|
|---|
| 1031 | try {
|
|---|
| 1032 | Declaration * decl = cur->build();
|
|---|
| 1033 | assert( decl );
|
|---|
| 1034 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
|
|---|
| 1035 | dwt->location = cur->location;
|
|---|
| 1036 | * out++ = dwt;
|
|---|
| 1037 | } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
|
|---|
| 1038 | // e.g., int foo(struct S) {}
|
|---|
| 1039 | StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name );
|
|---|
| 1040 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
|
|---|
| 1041 | obj->location = cur->location;
|
|---|
| 1042 | * out++ = obj;
|
|---|
| 1043 | delete agg;
|
|---|
| 1044 | } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
|
|---|
| 1045 | // e.g., int foo(union U) {}
|
|---|
| 1046 | UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name );
|
|---|
| 1047 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
|
|---|
| 1048 | obj->location = cur->location;
|
|---|
| 1049 | * out++ = obj;
|
|---|
| 1050 | } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) {
|
|---|
| 1051 | // e.g., int foo(enum E) {}
|
|---|
| 1052 | EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name );
|
|---|
| 1053 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
|
|---|
| 1054 | obj->location = cur->location;
|
|---|
| 1055 | * out++ = obj;
|
|---|
| 1056 | } // if
|
|---|
| 1057 | } catch( SemanticErrorException & e ) {
|
|---|
| 1058 | errors.append( e );
|
|---|
| 1059 | } // try
|
|---|
| 1060 | } // for
|
|---|
| 1061 |
|
|---|
| 1062 | if ( ! errors.isEmpty() ) {
|
|---|
| 1063 | throw errors;
|
|---|
| 1064 | } // if
|
|---|
| 1065 | } // buildList
|
|---|
| 1066 |
|
|---|
| 1067 | void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ) {
|
|---|
| 1068 | SemanticErrorException errors;
|
|---|
| 1069 | std::back_insert_iterator< std::list< Type * > > out( outputList );
|
|---|
| 1070 | const DeclarationNode * cur = firstNode;
|
|---|
| 1071 |
|
|---|
| 1072 | while ( cur ) {
|
|---|
| 1073 | try {
|
|---|
| 1074 | * out++ = cur->buildType();
|
|---|
| 1075 | } catch( SemanticErrorException & e ) {
|
|---|
| 1076 | errors.append( e );
|
|---|
| 1077 | } // try
|
|---|
| 1078 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
|
|---|
| 1079 | } // while
|
|---|
| 1080 |
|
|---|
| 1081 | if ( ! errors.isEmpty() ) {
|
|---|
| 1082 | throw errors;
|
|---|
| 1083 | } // if
|
|---|
| 1084 | } // buildTypeList
|
|---|
| 1085 |
|
|---|
| 1086 | Declaration * DeclarationNode::build() const {
|
|---|
| 1087 | if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
|
|---|
| 1088 |
|
|---|
| 1089 | if ( asmStmt ) {
|
|---|
| 1090 | return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
|
|---|
| 1091 | } // if
|
|---|
| 1092 |
|
|---|
| 1093 | if ( variable.tyClass != NoTypeClass ) {
|
|---|
| 1094 | // otype is internally converted to dtype + otype parameters
|
|---|
| 1095 | static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
|
|---|
| 1096 | assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
|
|---|
| 1097 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
|
|---|
| 1098 | TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
|
|---|
| 1099 | buildList( variable.assertions, ret->get_assertions() );
|
|---|
| 1100 | return ret;
|
|---|
| 1101 | } // if
|
|---|
| 1102 |
|
|---|
| 1103 | if ( type ) {
|
|---|
| 1104 | // Function specifiers can only appear on a function definition/declaration.
|
|---|
| 1105 | //
|
|---|
| 1106 | // inline _Noreturn int f(); // allowed
|
|---|
| 1107 | // inline _Noreturn int g( int i ); // allowed
|
|---|
| 1108 | // inline _Noreturn int i; // disallowed
|
|---|
| 1109 | if ( type->kind != TypeData::Function && funcSpecs.any() ) {
|
|---|
| 1110 | SemanticError( this, "invalid function specifier for " );
|
|---|
| 1111 | } // if
|
|---|
| 1112 | // Forall qualifier can only appear on a function/aggregate definition/declaration.
|
|---|
| 1113 | //
|
|---|
| 1114 | // forall int f(); // allowed
|
|---|
| 1115 | // forall int g( int i ); // allowed
|
|---|
| 1116 | // forall int i; // disallowed
|
|---|
| 1117 | if ( type->kind != TypeData::Function && type->forall ) {
|
|---|
| 1118 | SemanticError( this, "invalid type qualifier for " );
|
|---|
| 1119 | } // if
|
|---|
| 1120 | bool isDelete = initializer && initializer->get_isDelete();
|
|---|
| 1121 | Declaration * decl = buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, isDelete ? nullptr : maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
|
|---|
| 1122 | if ( isDelete ) {
|
|---|
| 1123 | DeclarationWithType * dwt = strict_dynamic_cast<DeclarationWithType *>( decl );
|
|---|
| 1124 | dwt->isDeleted = true;
|
|---|
| 1125 | }
|
|---|
| 1126 | return decl;
|
|---|
| 1127 | } // if
|
|---|
| 1128 |
|
|---|
| 1129 | if ( assert.condition ) {
|
|---|
| 1130 | return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
|
|---|
| 1131 | }
|
|---|
| 1132 |
|
|---|
| 1133 | // SUE's cannot have function specifiers, either
|
|---|
| 1134 | //
|
|---|
| 1135 | // inlne _Noreturn struct S { ... }; // disallowed
|
|---|
| 1136 | // inlne _Noreturn enum E { ... }; // disallowed
|
|---|
| 1137 | if ( funcSpecs.any() ) {
|
|---|
| 1138 | SemanticError( this, "invalid function specifier for " );
|
|---|
| 1139 | } // if
|
|---|
| 1140 | assertf( name, "ObjectDecl must a have name\n" );
|
|---|
| 1141 | return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
|
|---|
| 1142 | }
|
|---|
| 1143 |
|
|---|
| 1144 | Type * DeclarationNode::buildType() const {
|
|---|
| 1145 | assert( type );
|
|---|
| 1146 |
|
|---|
| 1147 | if ( attr.expr ) {
|
|---|
| 1148 | return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
|
|---|
| 1149 | } else if ( attr.type ) {
|
|---|
| 1150 | return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
|
|---|
| 1151 | } // if
|
|---|
| 1152 |
|
|---|
| 1153 | switch ( type->kind ) {
|
|---|
| 1154 | case TypeData::Enum:
|
|---|
| 1155 | case TypeData::Aggregate: {
|
|---|
| 1156 | ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
|
|---|
| 1157 | buildList( type->aggregate.actuals, ret->get_parameters() );
|
|---|
| 1158 | return ret;
|
|---|
| 1159 | }
|
|---|
| 1160 | case TypeData::Symbolic: {
|
|---|
| 1161 | TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
|
|---|
| 1162 | buildList( type->symbolic.actuals, ret->get_parameters() );
|
|---|
| 1163 | return ret;
|
|---|
| 1164 | }
|
|---|
| 1165 | default:
|
|---|
| 1166 | Type * simpletypes = typebuild( type );
|
|---|
| 1167 | simpletypes->get_attributes() = attributes; // copy because member is const
|
|---|
| 1168 | return simpletypes;
|
|---|
| 1169 | } // switch
|
|---|
| 1170 | }
|
|---|
| 1171 |
|
|---|
| 1172 | // Local Variables: //
|
|---|
| 1173 | // tab-width: 4 //
|
|---|
| 1174 | // mode: c++ //
|
|---|
| 1175 | // compile-command: "make install" //
|
|---|
| 1176 | // End: //
|
|---|