[b87a5ed] | 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 | // |
---|
[c92bdcc] | 7 | // DeclarationNode.cpp -- |
---|
[b87a5ed] | 8 | // |
---|
| 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // Created On : Sat May 16 12:34:05 2015 |
---|
[b38f6da] | 11 | // Last Modified By : Peter A. Buhr |
---|
[4eb3a7c5] | 12 | // Last Modified On : Fri Feb 23 18:25:57 2024 |
---|
| 13 | // Update Count : 1533 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[c92bdcc] | 16 | #include "DeclarationNode.hpp" |
---|
| 17 | |
---|
| 18 | #include <cassert> // for assert, assertf, strict_dynami... |
---|
| 19 | #include <iterator> // for back_insert_iterator |
---|
| 20 | #include <list> // for list |
---|
| 21 | #include <memory> // for unique_ptr |
---|
| 22 | #include <ostream> // for operator<<, ostream, basic_ost... |
---|
| 23 | #include <string> // for string, operator+, allocator, ... |
---|
| 24 | |
---|
| 25 | #include "AST/Attribute.hpp" // for Attribute |
---|
| 26 | #include "AST/Copy.hpp" // for shallowCopy |
---|
| 27 | #include "AST/Decl.hpp" // for Decl |
---|
| 28 | #include "AST/Expr.hpp" // for Expr |
---|
| 29 | #include "AST/Print.hpp" // for print |
---|
| 30 | #include "AST/Stmt.hpp" // for AsmStmt, DirectiveStmt |
---|
| 31 | #include "AST/StorageClasses.hpp" // for Storage::Class |
---|
| 32 | #include "AST/Type.hpp" // for Type |
---|
| 33 | #include "Common/CodeLocation.hpp" // for CodeLocation |
---|
| 34 | #include "Common/Iterate.hpp" // for reverseIterate |
---|
| 35 | #include "Common/SemanticError.hpp" // for SemanticError |
---|
| 36 | #include "Common/UniqueName.hpp" // for UniqueName |
---|
| 37 | #include "Common/Utility.hpp" // for copy, spliceBegin |
---|
| 38 | #include "Parser/ExpressionNode.hpp" // for ExpressionNode |
---|
| 39 | #include "Parser/InitializerNode.hpp" // for InitializerNode |
---|
| 40 | #include "Parser/StatementNode.hpp" // for StatementNode |
---|
| 41 | #include "TypeData.hpp" // for TypeData, TypeData::Aggregate_t |
---|
| 42 | #include "TypedefTable.hpp" // for TypedefTable |
---|
[bdd516a] | 43 | |
---|
[de62360d] | 44 | extern TypedefTable typedefTable; |
---|
| 45 | |
---|
[51b7345] | 46 | using namespace std; |
---|
| 47 | |
---|
| 48 | UniqueName DeclarationNode::anonymous( "__anonymous" ); |
---|
| 49 | |
---|
[bb7422a] | 50 | extern ast::Linkage::Spec linkage; // defined in parser.yy |
---|
[51b7345] | 51 | |
---|
[7d05e7e] | 52 | DeclarationNode::DeclarationNode() : |
---|
[e07caa2] | 53 | linkage( ::linkage ) { |
---|
[2298f728] | 54 | |
---|
[bb7422a] | 55 | variable.tyClass = ast::TypeDecl::NUMBER_OF_KINDS; |
---|
[28307be] | 56 | variable.assertions = nullptr; |
---|
[67cf18c] | 57 | variable.initializer = nullptr; |
---|
[7d05e7e] | 58 | |
---|
[f6e3e34] | 59 | assert.condition = nullptr; |
---|
| 60 | assert.message = nullptr; |
---|
[28307be] | 61 | } |
---|
| 62 | |
---|
| 63 | DeclarationNode::~DeclarationNode() { |
---|
[4c0b674] | 64 | delete name; |
---|
| 65 | |
---|
[2298f728] | 66 | delete variable.assertions; |
---|
[67cf18c] | 67 | delete variable.initializer; |
---|
[2298f728] | 68 | |
---|
[4a72fef] | 69 | delete type; |
---|
[28307be] | 70 | delete bitfieldWidth; |
---|
[e994912] | 71 | |
---|
| 72 | delete asmStmt; |
---|
[58dd019] | 73 | // asmName, no delete, passed to next stage |
---|
[28307be] | 74 | delete initializer; |
---|
[f6e3e34] | 75 | |
---|
| 76 | delete assert.condition; |
---|
| 77 | delete assert.message; |
---|
[28307be] | 78 | } |
---|
| 79 | |
---|
[ba7aa2d] | 80 | DeclarationNode * DeclarationNode::clone() const { |
---|
| 81 | DeclarationNode * newnode = new DeclarationNode; |
---|
[44adf1b] | 82 | newnode->next = maybeCopy( next ); |
---|
[2298f728] | 83 | newnode->name = name ? new string( *name ) : nullptr; |
---|
[c0aa336] | 84 | |
---|
[5bf685f] | 85 | newnode->type = maybeCopy( type ); |
---|
[679e644] | 86 | newnode->inLine = inLine; |
---|
[a7c90d4] | 87 | newnode->storageClasses = storageClasses; |
---|
| 88 | newnode->funcSpecs = funcSpecs; |
---|
[5bf685f] | 89 | newnode->bitfieldWidth = maybeCopy( bitfieldWidth ); |
---|
| 90 | newnode->enumeratorValue.reset( maybeCopy( enumeratorValue.get() ) ); |
---|
[b87a5ed] | 91 | newnode->hasEllipsis = hasEllipsis; |
---|
| 92 | newnode->linkage = linkage; |
---|
[bb7422a] | 93 | newnode->asmName = maybeCopy( asmName ); |
---|
| 94 | newnode->attributes = attributes; |
---|
[5bf685f] | 95 | newnode->initializer = maybeCopy( initializer ); |
---|
[c0aa336] | 96 | newnode->extension = extension; |
---|
[5bf685f] | 97 | newnode->asmStmt = maybeCopy( asmStmt ); |
---|
[c0aa336] | 98 | newnode->error = error; |
---|
[3848e0e] | 99 | |
---|
[28307be] | 100 | newnode->variable.tyClass = variable.tyClass; |
---|
[5bf685f] | 101 | newnode->variable.assertions = maybeCopy( variable.assertions ); |
---|
| 102 | newnode->variable.initializer = maybeCopy( variable.initializer ); |
---|
[3848e0e] | 103 | |
---|
[5bf685f] | 104 | newnode->assert.condition = maybeCopy( assert.condition ); |
---|
[bb7422a] | 105 | newnode->assert.message = maybeCopy( assert.message ); |
---|
[28307be] | 106 | return newnode; |
---|
| 107 | } // DeclarationNode::clone |
---|
[3848e0e] | 108 | |
---|
[f2f512ba] | 109 | void DeclarationNode::print( std::ostream & os, int indent ) const { |
---|
[59db689] | 110 | os << string( indent, ' ' ); |
---|
[2298f728] | 111 | if ( name ) { |
---|
| 112 | os << *name << ": "; |
---|
[68cd1ce] | 113 | } // if |
---|
[51b7345] | 114 | |
---|
[bb7422a] | 115 | if ( linkage != ast::Linkage::Cforall ) { |
---|
| 116 | os << ast::Linkage::name( linkage ) << " "; |
---|
[68cd1ce] | 117 | } // if |
---|
[3848e0e] | 118 | |
---|
[bb7422a] | 119 | ast::print( os, storageClasses ); |
---|
| 120 | ast::print( os, funcSpecs ); |
---|
[dd020c0] | 121 | |
---|
[b87a5ed] | 122 | if ( type ) { |
---|
| 123 | type->print( os, indent ); |
---|
| 124 | } else { |
---|
| 125 | os << "untyped entity "; |
---|
[68cd1ce] | 126 | } // if |
---|
[3848e0e] | 127 | |
---|
[b87a5ed] | 128 | if ( bitfieldWidth ) { |
---|
[59db689] | 129 | os << endl << string( indent + 2, ' ' ) << "with bitfield width "; |
---|
[b87a5ed] | 130 | bitfieldWidth->printOneLine( os ); |
---|
[68cd1ce] | 131 | } // if |
---|
[3848e0e] | 132 | |
---|
[2298f728] | 133 | if ( initializer ) { |
---|
[59db689] | 134 | os << endl << string( indent + 2, ' ' ) << "with initializer "; |
---|
[b87a5ed] | 135 | initializer->printOneLine( os ); |
---|
[974906e2] | 136 | os << " maybe constructed? " << initializer->get_maybeConstructed(); |
---|
[68cd1ce] | 137 | } // if |
---|
[3848e0e] | 138 | |
---|
[692c1cc] | 139 | if ( ! attributes.empty() ) { |
---|
[4eb3a7c5] | 140 | os << string( indent + 2, ' ' ) << "with attributes" << endl; |
---|
[bb7422a] | 141 | for ( ast::ptr<ast::Attribute> const & attr : reverseIterate( attributes ) ) { |
---|
[4eb3a7c5] | 142 | os << string( indent + 4, ' ' ); |
---|
| 143 | ast::print( os, attr, indent + 2 ); |
---|
[692c1cc] | 144 | } // for |
---|
| 145 | } // if |
---|
[66406f3] | 146 | |
---|
[b87a5ed] | 147 | os << endl; |
---|
[51b7345] | 148 | } |
---|
| 149 | |
---|
[f2f512ba] | 150 | void DeclarationNode::printList( std::ostream & os, int indent ) const { |
---|
[dc3fbe5] | 151 | ParseList::printList( os, indent ); |
---|
[b87a5ed] | 152 | if ( hasEllipsis ) { |
---|
| 153 | os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; |
---|
[68cd1ce] | 154 | } // if |
---|
[51b7345] | 155 | } |
---|
| 156 | |
---|
[6cef439] | 157 | DeclarationNode * DeclarationNode::newFromTypeData( TypeData * type ) { |
---|
| 158 | DeclarationNode * newnode = new DeclarationNode; |
---|
| 159 | newnode->type = type; |
---|
| 160 | return newnode; |
---|
| 161 | } // DeclarationNode::newFromTypeData |
---|
| 162 | |
---|
[bb7422a] | 163 | DeclarationNode * DeclarationNode::newStorageClass( ast::Storage::Classes sc ) { |
---|
[ba7aa2d] | 164 | DeclarationNode * newnode = new DeclarationNode; |
---|
[08d5507b] | 165 | newnode->storageClasses = sc; |
---|
[b87a5ed] | 166 | return newnode; |
---|
[dd020c0] | 167 | } // DeclarationNode::newStorageClass |
---|
[3848e0e] | 168 | |
---|
[bb7422a] | 169 | DeclarationNode * DeclarationNode::newFuncSpecifier( ast::Function::Specs fs ) { |
---|
[ba7aa2d] | 170 | DeclarationNode * newnode = new DeclarationNode; |
---|
[08d5507b] | 171 | newnode->funcSpecs = fs; |
---|
[c1c1112] | 172 | return newnode; |
---|
[dd020c0] | 173 | } // DeclarationNode::newFuncSpecifier |
---|
[c1c1112] | 174 | |
---|
[bb7422a] | 175 | DeclarationNode * DeclarationNode::newAggregate( ast::AggregateDecl::Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { |
---|
[ba7aa2d] | 176 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 177 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
[8f6f47d7] | 178 | newnode->type->aggregate.kind = kind; |
---|
[692c1cc] | 179 | newnode->type->aggregate.anon = name == nullptr; |
---|
| 180 | newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name; |
---|
[8f6f47d7] | 181 | newnode->type->aggregate.actuals = actuals; |
---|
| 182 | newnode->type->aggregate.fields = fields; |
---|
| 183 | newnode->type->aggregate.body = body; |
---|
[b87a5ed] | 184 | return newnode; |
---|
[984dce6] | 185 | } // DeclarationNode::newAggregate |
---|
[3848e0e] | 186 | |
---|
[99f7f37] | 187 | DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool isCfa, DeclarationNode * base, EnumHiding hiding ) { |
---|
[67467a3] | 188 | DeclarationNode * newnode = newAggregate( ast::AggregateDecl::Enum, name, nullptr, constants, body ); |
---|
[99f7f37] | 189 | newnode->type->aggregate.isCfa = isCfa; |
---|
[67467a3] | 190 | newnode->type->aggregate.hiding = hiding; |
---|
[057608a] | 191 | if ( base ) { |
---|
[99f7f37] | 192 | assert( isCfa ); |
---|
[057608a] | 193 | assert( base->type ); |
---|
[ed9a1ae] | 194 | newnode->type->base = base->type; |
---|
[057608a] | 195 | base->type = nullptr; |
---|
| 196 | delete base; |
---|
[9e7236f4] | 197 | } // if |
---|
| 198 | |
---|
[b87a5ed] | 199 | return newnode; |
---|
[984dce6] | 200 | } // DeclarationNode::newEnum |
---|
[3848e0e] | 201 | |
---|
[a46b69c] | 202 | DeclarationNode * DeclarationNode::newName( const string * name ) { |
---|
[ba7aa2d] | 203 | DeclarationNode * newnode = new DeclarationNode; |
---|
[a46b69c] | 204 | assert( ! newnode->name ); |
---|
[2298f728] | 205 | newnode->name = name; |
---|
[a46b69c] | 206 | return newnode; |
---|
| 207 | } // DeclarationNode::newName |
---|
| 208 | |
---|
[374cb117] | 209 | DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) { |
---|
[a46b69c] | 210 | DeclarationNode * newnode = newName( name ); |
---|
[4f147cc] | 211 | newnode->enumeratorValue.reset( constant ); |
---|
[b87a5ed] | 212 | return newnode; |
---|
[984dce6] | 213 | } // DeclarationNode::newEnumConstant |
---|
[3848e0e] | 214 | |
---|
[374cb117] | 215 | DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) { |
---|
[057608a] | 216 | if ( nullptr == init ) { |
---|
[b0d9ff7] | 217 | return newName( name ); |
---|
[057608a] | 218 | } else if ( init->get_expression() ) { |
---|
| 219 | return newEnumConstant( name, init->get_expression() ); |
---|
| 220 | } else { |
---|
| 221 | DeclarationNode * newnode = newName( name ); |
---|
| 222 | newnode->initializer = init; |
---|
| 223 | return newnode; |
---|
[374cb117] | 224 | } // if |
---|
[9e7236f4] | 225 | } // DeclarationNode::newEnumValueGeneric |
---|
[374cb117] | 226 | |
---|
[d9bad51] | 227 | DeclarationNode * DeclarationNode::newEnumInLine( const std::string * name ) { |
---|
| 228 | DeclarationNode * newnode = newName( name ); |
---|
[1e30df7] | 229 | newnode->enumInLine = true; |
---|
| 230 | return newnode; |
---|
| 231 | } |
---|
| 232 | |
---|
[bb7422a] | 233 | DeclarationNode * DeclarationNode::newTypeParam( ast::TypeDecl::Kind tc, const string * name ) { |
---|
[a46b69c] | 234 | DeclarationNode * newnode = newName( name ); |
---|
[2298f728] | 235 | newnode->type = nullptr; |
---|
[28307be] | 236 | newnode->variable.tyClass = tc; |
---|
[faddbd8] | 237 | newnode->variable.assertions = nullptr; |
---|
[b87a5ed] | 238 | return newnode; |
---|
[984dce6] | 239 | } // DeclarationNode::newTypeParam |
---|
[3848e0e] | 240 | |
---|
[fb114fa1] | 241 | DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) { |
---|
[ba7aa2d] | 242 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 243 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
[2298f728] | 244 | newnode->type->aggregate.name = name; |
---|
[bb7422a] | 245 | newnode->type->aggregate.kind = ast::AggregateDecl::Trait; |
---|
[8f6f47d7] | 246 | newnode->type->aggregate.params = params; |
---|
| 247 | newnode->type->aggregate.fields = asserts; |
---|
[b87a5ed] | 248 | return newnode; |
---|
[984dce6] | 249 | } // DeclarationNode::newTrait |
---|
[3848e0e] | 250 | |
---|
[fb114fa1] | 251 | DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) { |
---|
[ba7aa2d] | 252 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 253 | newnode->type = new TypeData( TypeData::AggregateInst ); |
---|
[8f6f47d7] | 254 | newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate ); |
---|
[bb7422a] | 255 | newnode->type->aggInst.aggregate->aggregate.kind = ast::AggregateDecl::Trait; |
---|
[2298f728] | 256 | newnode->type->aggInst.aggregate->aggregate.name = name; |
---|
[8f6f47d7] | 257 | newnode->type->aggInst.params = params; |
---|
[b87a5ed] | 258 | return newnode; |
---|
[984dce6] | 259 | } // DeclarationNode::newTraitUse |
---|
[3848e0e] | 260 | |
---|
[25bca42] | 261 | DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) { |
---|
[a46b69c] | 262 | DeclarationNode * newnode = newName( name ); |
---|
[b87a5ed] | 263 | newnode->type = new TypeData( TypeData::Symbolic ); |
---|
[8f6f47d7] | 264 | newnode->type->symbolic.isTypedef = false; |
---|
| 265 | newnode->type->symbolic.params = typeParams; |
---|
[b87a5ed] | 266 | return newnode; |
---|
[984dce6] | 267 | } // DeclarationNode::newTypeDecl |
---|
[3848e0e] | 268 | |
---|
[ce8c12f] | 269 | DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) { |
---|
[ba7aa2d] | 270 | DeclarationNode * newnode = new DeclarationNode; |
---|
[ce8c12f] | 271 | newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference ); |
---|
[71d0eab] | 272 | if ( kind == OperKinds::And ) { |
---|
| 273 | // T && is parsed as 'And' operator rather than two references => add a second reference type |
---|
| 274 | TypeData * td = new TypeData( TypeData::Reference ); |
---|
| 275 | td->base = newnode->type; |
---|
| 276 | newnode->type = td; |
---|
| 277 | } |
---|
[c3396e0] | 278 | if ( qualifiers ) { |
---|
| 279 | return newnode->addQualifiers( qualifiers ); |
---|
| 280 | } else { |
---|
| 281 | return newnode; |
---|
| 282 | } // if |
---|
[984dce6] | 283 | } // DeclarationNode::newPointer |
---|
[3848e0e] | 284 | |
---|
[ba7aa2d] | 285 | DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) { |
---|
| 286 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 287 | newnode->type = new TypeData( TypeData::Array ); |
---|
[8f6f47d7] | 288 | newnode->type->array.dimension = size; |
---|
| 289 | newnode->type->array.isStatic = isStatic; |
---|
[a3525c4] | 290 | newnode->type->array.isVarLen = size && !size->isExpressionType<ast::ConstantExpr *>(); |
---|
[b87a5ed] | 291 | return newnode->addQualifiers( qualifiers ); |
---|
[984dce6] | 292 | } // DeclarationNode::newArray |
---|
[3848e0e] | 293 | |
---|
[ba7aa2d] | 294 | DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) { |
---|
| 295 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 296 | newnode->type = new TypeData( TypeData::Array ); |
---|
[2298f728] | 297 | newnode->type->array.dimension = nullptr; |
---|
[8f6f47d7] | 298 | newnode->type->array.isStatic = false; |
---|
| 299 | newnode->type->array.isVarLen = true; |
---|
[b87a5ed] | 300 | return newnode->addQualifiers( qualifiers ); |
---|
[3848e0e] | 301 | } |
---|
| 302 | |
---|
[ba7aa2d] | 303 | DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) { |
---|
| 304 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 305 | newnode->bitfieldWidth = size; |
---|
| 306 | return newnode; |
---|
[3848e0e] | 307 | } |
---|
| 308 | |
---|
[ba7aa2d] | 309 | DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) { |
---|
| 310 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 311 | newnode->type = new TypeData( TypeData::Tuple ); |
---|
[8f6f47d7] | 312 | newnode->type->tuple = members; |
---|
[b87a5ed] | 313 | return newnode; |
---|
[3848e0e] | 314 | } |
---|
| 315 | |
---|
[f855545] | 316 | DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) { |
---|
[ba7aa2d] | 317 | DeclarationNode * newnode = new DeclarationNode; |
---|
[f855545] | 318 | newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof ); |
---|
[8f6f47d7] | 319 | newnode->type->typeexpr = expr; |
---|
[b87a5ed] | 320 | return newnode; |
---|
[3848e0e] | 321 | } |
---|
| 322 | |
---|
[a46b69c] | 323 | DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) { |
---|
| 324 | DeclarationNode * newnode = newName( name ); |
---|
| 325 | newnode->type = new TypeData( TypeData::Function ); |
---|
| 326 | newnode->type->function.params = param; |
---|
| 327 | newnode->type->function.body = body; |
---|
| 328 | |
---|
| 329 | if ( ret ) { |
---|
| 330 | newnode->type->base = ret->type; |
---|
| 331 | ret->type = nullptr; |
---|
| 332 | delete ret; |
---|
| 333 | } // if |
---|
| 334 | |
---|
| 335 | return newnode; |
---|
| 336 | } // DeclarationNode::newFunction |
---|
| 337 | |
---|
[25bca42] | 338 | DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) { |
---|
[44a81853] | 339 | DeclarationNode * newnode = new DeclarationNode; |
---|
| 340 | newnode->type = nullptr; |
---|
[bb7422a] | 341 | std::vector<ast::ptr<ast::Expr>> exprs; |
---|
[44a81853] | 342 | buildList( expr, exprs ); |
---|
[b38f6da] | 343 | newnode->attributes.push_back( new ast::Attribute( *name, std::move( exprs ) ) ); |
---|
[44a81853] | 344 | delete name; |
---|
| 345 | return newnode; |
---|
| 346 | } |
---|
| 347 | |
---|
[2d019af] | 348 | DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) { |
---|
| 349 | DeclarationNode * newnode = new DeclarationNode; |
---|
| 350 | newnode->directiveStmt = stmt; |
---|
| 351 | return newnode; |
---|
| 352 | } |
---|
| 353 | |
---|
[e994912] | 354 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) { |
---|
| 355 | DeclarationNode * newnode = new DeclarationNode; |
---|
| 356 | newnode->asmStmt = stmt; |
---|
| 357 | return newnode; |
---|
| 358 | } |
---|
| 359 | |
---|
[bb7422a] | 360 | DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) { |
---|
[f6e3e34] | 361 | DeclarationNode * newnode = new DeclarationNode; |
---|
| 362 | newnode->assert.condition = condition; |
---|
| 363 | newnode->assert.message = message; |
---|
| 364 | return newnode; |
---|
| 365 | } |
---|
| 366 | |
---|
[bb7422a] | 367 | static void appendError( string & dst, const string & src ) { |
---|
[5b639ee] | 368 | if ( src.empty() ) return; |
---|
| 369 | if ( dst.empty() ) { dst = src; return; } |
---|
| 370 | dst += ", " + src; |
---|
| 371 | } // appendError |
---|
| 372 | |
---|
[ba7aa2d] | 373 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { |
---|
[bb7422a] | 374 | const ast::CV::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization |
---|
| 375 | const ast::CV::Qualifiers duplicates = qsrc & qdst; |
---|
| 376 | |
---|
| 377 | if ( duplicates.any() ) { |
---|
| 378 | std::stringstream str; |
---|
| 379 | str << "duplicate "; |
---|
| 380 | ast::print( str, duplicates ); |
---|
| 381 | str << "qualifier(s)"; |
---|
| 382 | appendError( error, str.str() ); |
---|
[a7c90d4] | 383 | } // for |
---|
[c1c1112] | 384 | } // DeclarationNode::checkQualifiers |
---|
| 385 | |
---|
[a7c90d4] | 386 | void DeclarationNode::checkSpecifiers( DeclarationNode * src ) { |
---|
[bb7422a] | 387 | ast::Function::Specs fsDups = funcSpecs & src->funcSpecs; |
---|
| 388 | if ( fsDups.any() ) { |
---|
| 389 | std::stringstream str; |
---|
| 390 | str << "duplicate "; |
---|
| 391 | ast::print( str, fsDups ); |
---|
| 392 | str << "function specifier(s)"; |
---|
| 393 | appendError( error, str.str() ); |
---|
[dd020c0] | 394 | } // if |
---|
| 395 | |
---|
[bb7422a] | 396 | // Skip if everything is unset. |
---|
| 397 | if ( storageClasses.any() && src->storageClasses.any() ) { |
---|
| 398 | ast::Storage::Classes dups = storageClasses & src->storageClasses; |
---|
| 399 | // Check for duplicates. |
---|
| 400 | if ( dups.any() ) { |
---|
| 401 | std::stringstream str; |
---|
| 402 | str << "duplicate "; |
---|
| 403 | ast::print( str, dups ); |
---|
| 404 | str << "storage class(es)"; |
---|
| 405 | appendError( error, str.str() ); |
---|
| 406 | // Check for conflicts. |
---|
| 407 | } else if ( !src->storageClasses.is_threadlocal_any() ) { |
---|
| 408 | std::stringstream str; |
---|
| 409 | str << "conflicting "; |
---|
| 410 | ast::print( str, ast::Storage::Classes( 1 << storageClasses.ffs() ) ); |
---|
| 411 | str << "& "; |
---|
| 412 | ast::print( str, ast::Storage::Classes( 1 << src->storageClasses.ffs() ) ); |
---|
| 413 | str << "storage classes"; |
---|
| 414 | appendError( error, str.str() ); |
---|
| 415 | // FIX to preserve invariant of one basic storage specifier |
---|
| 416 | src->storageClasses.reset(); |
---|
| 417 | } |
---|
[b6424d9] | 418 | } // if |
---|
[dd020c0] | 419 | |
---|
[a7c90d4] | 420 | appendError( error, src->error ); |
---|
| 421 | } // DeclarationNode::checkSpecifiers |
---|
[b6424d9] | 422 | |
---|
[4eb3a7c5] | 423 | DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q, bool copyattr ) { |
---|
[6f95000] | 424 | funcSpecs |= q->funcSpecs; |
---|
| 425 | storageClasses |= q->storageClasses; |
---|
[c0aa336] | 426 | |
---|
[4eb3a7c5] | 427 | if ( copyattr ) { |
---|
| 428 | std::vector<ast::ptr<ast::Attribute>> tmp; |
---|
| 429 | tmp.reserve( q->attributes.size() ); |
---|
| 430 | for ( auto const & attr : q->attributes ) { |
---|
| 431 | tmp.emplace_back( ast::shallowCopy( attr.get() ) ); |
---|
| 432 | } // for |
---|
| 433 | spliceBegin( attributes, tmp ); |
---|
| 434 | } // if |
---|
[bb7422a] | 435 | |
---|
[b6424d9] | 436 | return this; |
---|
[a7c90d4] | 437 | } // DeclarationNode::copySpecifiers |
---|
[b6424d9] | 438 | |
---|
[ba7aa2d] | 439 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { |
---|
[af9da5f] | 440 | if ( ! q ) { return this; } // empty qualifier |
---|
[101e0bd] | 441 | |
---|
[a7c90d4] | 442 | checkSpecifiers( q ); |
---|
| 443 | copySpecifiers( q ); |
---|
[101e0bd] | 444 | |
---|
[c38ae92] | 445 | if ( ! q->type ) { delete q; return this; } |
---|
[101e0bd] | 446 | |
---|
| 447 | if ( ! type ) { |
---|
[c38ae92] | 448 | type = q->type; // reuse structure |
---|
[1b772749] | 449 | q->type = nullptr; |
---|
| 450 | delete q; |
---|
[101e0bd] | 451 | return this; |
---|
| 452 | } // if |
---|
| 453 | |
---|
[a7c90d4] | 454 | checkQualifiers( type, q->type ); |
---|
[e048ece] | 455 | TypeData::BuiltinType const builtin = type->builtintype; |
---|
| 456 | if ( (builtin == TypeData::Zero || builtin == TypeData::One) && q->type->qualifiers.any() && error.length() == 0 ) { |
---|
| 457 | SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, TypeData::builtinTypeNames[builtin] ); |
---|
[9dc31c10] | 458 | } // if |
---|
[1cfe640] | 459 | type = ::addQualifiers( type, q->type ); |
---|
[af60383] | 460 | q->type = nullptr; |
---|
[6ef2d81] | 461 | |
---|
[b87a5ed] | 462 | delete q; |
---|
| 463 | return this; |
---|
[101e0bd] | 464 | } // addQualifiers |
---|
[3848e0e] | 465 | |
---|
[af60383] | 466 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o, bool copyattr ) { |
---|
| 467 | if ( !o ) return this; |
---|
| 468 | |
---|
| 469 | checkSpecifiers( o ); |
---|
| 470 | copySpecifiers( o, copyattr ); |
---|
| 471 | if ( o->type ) { |
---|
[1cfe640] | 472 | type = ::addType( type, o->type, o->attributes ); |
---|
[af60383] | 473 | o->type = nullptr; |
---|
[101e0bd] | 474 | } // if |
---|
[af60383] | 475 | if ( o->bitfieldWidth ) { |
---|
| 476 | bitfieldWidth = o->bitfieldWidth; |
---|
[68cd1ce] | 477 | } // if |
---|
[3848e0e] | 478 | |
---|
[af60383] | 479 | // there may be typedefs chained onto the type |
---|
| 480 | if ( o->next ) { |
---|
| 481 | set_last( o->next->clone() ); |
---|
[68cd1ce] | 482 | } // if |
---|
[66406f3] | 483 | |
---|
[af60383] | 484 | delete o; |
---|
[b87a5ed] | 485 | return this; |
---|
[3848e0e] | 486 | } |
---|
| 487 | |
---|
[f135b50] | 488 | DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) { |
---|
[af60383] | 489 | if ( o && o->type ) { |
---|
| 490 | type->base = o->type; |
---|
[b38f6da] | 491 | } // if |
---|
[f135b50] | 492 | delete o; |
---|
| 493 | return this; |
---|
| 494 | } |
---|
| 495 | |
---|
[2583407] | 496 | // This code handles a special issue with the attribute transparent_union. |
---|
| 497 | // |
---|
| 498 | // typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union )) |
---|
| 499 | // |
---|
| 500 | // Here the attribute aligned goes with the typedef_name, so variables declared of this type are |
---|
| 501 | // aligned. However, the attribute transparent_union must be moved from the typedef_name to |
---|
| 502 | // alias union U. Currently, this is the only know attribute that must be moved from typedef to |
---|
| 503 | // alias. |
---|
| 504 | static void moveUnionAttribute( DeclarationNode * decl, DeclarationNode * unionDecl ) { |
---|
| 505 | assert( decl->type->kind == TypeData::Symbolic ); |
---|
| 506 | assert( decl->type->symbolic.isTypedef ); |
---|
| 507 | assert( unionDecl->type->kind == TypeData::Aggregate ); |
---|
| 508 | |
---|
| 509 | if ( unionDecl->type->aggregate.kind != ast::AggregateDecl::Union ) return; |
---|
| 510 | |
---|
| 511 | // Ignore the Aggregate_t::attributes. Why did we add that before the rework? |
---|
| 512 | for ( auto attr = decl->attributes.begin() ; attr != decl->attributes.end() ; ) { |
---|
| 513 | if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) { |
---|
| 514 | unionDecl->attributes.emplace_back( attr->release() ); |
---|
| 515 | attr = decl->attributes.erase( attr ); |
---|
| 516 | } else { |
---|
| 517 | ++attr; |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | // Helper for addTypedef, handles the case where the typedef wraps an |
---|
| 523 | // aggregate declaration (not a type), returns a chain of nodes. |
---|
| 524 | static DeclarationNode * addTypedefAggr( |
---|
| 525 | DeclarationNode * olddecl, TypeData * newtype ) { |
---|
| 526 | TypeData *& oldaggr = olddecl->type->aggInst.aggregate; |
---|
| 527 | |
---|
| 528 | // Handle anonymous aggregates: typedef struct { int i; } foo |
---|
| 529 | // Give the typedefed type a consistent name across translation units. |
---|
| 530 | if ( oldaggr->aggregate.anon ) { |
---|
| 531 | delete oldaggr->aggregate.name; |
---|
| 532 | oldaggr->aggregate.name = new string( "__anonymous_" + *olddecl->name ); |
---|
| 533 | oldaggr->aggregate.anon = false; |
---|
| 534 | oldaggr->qualifiers.reset(); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | // Replace the wrapped TypeData with a forward declaration. |
---|
| 538 | TypeData * newaggr = new TypeData( TypeData::Aggregate ); |
---|
| 539 | newaggr->aggregate.kind = oldaggr->aggregate.kind; |
---|
| 540 | newaggr->aggregate.name = oldaggr->aggregate.name ? new string( *oldaggr->aggregate.name ) : nullptr; |
---|
| 541 | newaggr->aggregate.body = false; |
---|
| 542 | newaggr->aggregate.anon = oldaggr->aggregate.anon; |
---|
[99f7f37] | 543 | newaggr->aggregate.isCfa = oldaggr->aggregate.isCfa; |
---|
[67467a3] | 544 | newaggr->aggregate.hiding = oldaggr->aggregate.hiding; |
---|
[2583407] | 545 | swap( newaggr, oldaggr ); |
---|
| 546 | |
---|
| 547 | newtype->base = olddecl->type; |
---|
| 548 | olddecl->type = newtype; |
---|
| 549 | DeclarationNode * newdecl = new DeclarationNode; |
---|
| 550 | newdecl->type = newaggr; |
---|
| 551 | newdecl->next = olddecl; |
---|
| 552 | |
---|
| 553 | moveUnionAttribute( olddecl, newdecl ); |
---|
| 554 | |
---|
| 555 | return newdecl; |
---|
| 556 | } |
---|
| 557 | |
---|
[057608a] | 558 | // Wrap the declaration in a typedef. It actually does that by modifying the |
---|
| 559 | // existing declaration, and may split it into two declarations. |
---|
| 560 | // This only happens if the wrapped type is actually a declaration of a SUE |
---|
| 561 | // type. If it does, the DeclarationNode for the SUE declaration is the node |
---|
| 562 | // returned, make sure later transformations are applied to the right node. |
---|
[ba7aa2d] | 563 | DeclarationNode * DeclarationNode::addTypedef() { |
---|
| 564 | TypeData * newtype = new TypeData( TypeData::Symbolic ); |
---|
[2298f728] | 565 | newtype->symbolic.params = nullptr; |
---|
[8f6f47d7] | 566 | newtype->symbolic.isTypedef = true; |
---|
[2298f728] | 567 | newtype->symbolic.name = name ? new string( *name ) : nullptr; |
---|
[2583407] | 568 | // If this typedef is wrapping an aggregate, separate them out. |
---|
| 569 | if ( TypeData::AggregateInst == type->kind |
---|
| 570 | && TypeData::Aggregate == type->aggInst.aggregate->kind |
---|
| 571 | && type->aggInst.aggregate->aggregate.body ) { |
---|
| 572 | return addTypedefAggr( this, newtype ); |
---|
| 573 | // There is no internal declaration, just a type. |
---|
| 574 | } else { |
---|
| 575 | newtype->base = type; |
---|
| 576 | type = newtype; |
---|
| 577 | return this; |
---|
| 578 | } |
---|
[3848e0e] | 579 | } |
---|
| 580 | |
---|
[ba7aa2d] | 581 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) { |
---|
[bb7422a] | 582 | if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) { |
---|
[4a72fef] | 583 | extend( variable.assertions, assertions ); |
---|
[702e826] | 584 | return this; |
---|
[2298f728] | 585 | } // if |
---|
| 586 | |
---|
[b87a5ed] | 587 | assert( type ); |
---|
[4a72fef] | 588 | assert( TypeData::Symbolic == type->kind ); |
---|
| 589 | extend( type->symbolic.assertions, assertions ); |
---|
[974906e2] | 590 | |
---|
[b87a5ed] | 591 | return this; |
---|
[51b7345] | 592 | } |
---|
| 593 | |
---|
[fb114fa1] | 594 | DeclarationNode * DeclarationNode::addName( string * newname ) { |
---|
[2298f728] | 595 | assert( ! name ); |
---|
| 596 | name = newname; |
---|
[b87a5ed] | 597 | return this; |
---|
[51b7345] | 598 | } |
---|
| 599 | |
---|
[c0aa336] | 600 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) { |
---|
[58dd019] | 601 | assert( ! asmName ); |
---|
[c0aa336] | 602 | asmName = newname ? newname->asmName : nullptr; |
---|
| 603 | return this->addQualifiers( newname ); |
---|
[58dd019] | 604 | } |
---|
| 605 | |
---|
[ba7aa2d] | 606 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { |
---|
[b87a5ed] | 607 | bitfieldWidth = size; |
---|
| 608 | return this; |
---|
[51b7345] | 609 | } |
---|
| 610 | |
---|
[ba7aa2d] | 611 | DeclarationNode * DeclarationNode::addVarArgs() { |
---|
[b87a5ed] | 612 | assert( type ); |
---|
| 613 | hasEllipsis = true; |
---|
| 614 | return this; |
---|
[51b7345] | 615 | } |
---|
| 616 | |
---|
[c453ac4] | 617 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) { |
---|
[b87a5ed] | 618 | assert( type ); |
---|
| 619 | assert( type->kind == TypeData::Function ); |
---|
[2298f728] | 620 | assert( ! type->function.body ); |
---|
[8f6f47d7] | 621 | type->function.body = body; |
---|
[c453ac4] | 622 | type->function.withExprs = withExprs; |
---|
[b87a5ed] | 623 | return this; |
---|
[51b7345] | 624 | } |
---|
| 625 | |
---|
[ba7aa2d] | 626 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) { |
---|
[b87a5ed] | 627 | assert( type ); |
---|
| 628 | assert( type->kind == TypeData::Function ); |
---|
[2298f728] | 629 | assert( ! type->function.oldDeclList ); |
---|
[8f6f47d7] | 630 | type->function.oldDeclList = list; |
---|
[b87a5ed] | 631 | return this; |
---|
[51b7345] | 632 | } |
---|
| 633 | |
---|
[c0aa336] | 634 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) { |
---|
[b87a5ed] | 635 | if ( type ) { |
---|
[af60383] | 636 | type->setLastBase( newType ); |
---|
[b87a5ed] | 637 | } else { |
---|
| 638 | type = newType; |
---|
[68cd1ce] | 639 | } // if |
---|
[c0aa336] | 640 | return this; |
---|
[3848e0e] | 641 | } |
---|
| 642 | |
---|
[c0aa336] | 643 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) { |
---|
| 644 | if ( a ) { |
---|
[bb7422a] | 645 | spliceBegin( attributes, a->attributes ); |
---|
[c0aa336] | 646 | a->attributes.clear(); |
---|
| 647 | } // if |
---|
| 648 | return this; |
---|
| 649 | } // copyAttribute |
---|
| 650 | |
---|
[ba7aa2d] | 651 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) { |
---|
[b87a5ed] | 652 | if ( p ) { |
---|
[6926a6d] | 653 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); |
---|
[c0aa336] | 654 | setBase( p->type ); |
---|
[2298f728] | 655 | p->type = nullptr; |
---|
[c0aa336] | 656 | copyAttribute( p ); |
---|
[b87a5ed] | 657 | delete p; |
---|
[68cd1ce] | 658 | } // if |
---|
[b87a5ed] | 659 | return this; |
---|
[3848e0e] | 660 | } |
---|
| 661 | |
---|
[ba7aa2d] | 662 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) { |
---|
[b87a5ed] | 663 | if ( a ) { |
---|
| 664 | assert( a->type->kind == TypeData::Array ); |
---|
[c0aa336] | 665 | setBase( a->type ); |
---|
[2298f728] | 666 | a->type = nullptr; |
---|
[c0aa336] | 667 | copyAttribute( a ); |
---|
[b87a5ed] | 668 | delete a; |
---|
[68cd1ce] | 669 | } // if |
---|
[b87a5ed] | 670 | return this; |
---|
[51b7345] | 671 | } |
---|
| 672 | |
---|
[ba7aa2d] | 673 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) { |
---|
[4a72fef] | 674 | if ( !p ) return this; |
---|
| 675 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); |
---|
| 676 | if ( type ) { |
---|
| 677 | p->type->base = makeNewBase( type ); |
---|
| 678 | type = nullptr; |
---|
[68cd1ce] | 679 | } // if |
---|
[4a72fef] | 680 | delete this; |
---|
| 681 | return p; |
---|
[51b7345] | 682 | } |
---|
| 683 | |
---|
[ba7aa2d] | 684 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { |
---|
[4a72fef] | 685 | if ( !a ) return this; |
---|
[738e304] | 686 | assert( a->type->kind == TypeData::Array ); |
---|
| 687 | if ( type ) { |
---|
[af60383] | 688 | a->type->setLastBase( makeNewBase( type ) ); |
---|
[738e304] | 689 | type = nullptr; |
---|
[68cd1ce] | 690 | } // if |
---|
[738e304] | 691 | delete this; |
---|
| 692 | return a; |
---|
[51b7345] | 693 | } |
---|
[3848e0e] | 694 | |
---|
[ba7aa2d] | 695 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) { |
---|
| 696 | TypeData * ftype = new TypeData( TypeData::Function ); |
---|
[8f6f47d7] | 697 | ftype->function.params = params; |
---|
[c0aa336] | 698 | setBase( ftype ); |
---|
[b87a5ed] | 699 | return this; |
---|
[3848e0e] | 700 | } |
---|
| 701 | |
---|
[ba7aa2d] | 702 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) { |
---|
[b87a5ed] | 703 | if ( type ) { |
---|
| 704 | if ( type->kind != TypeData::Function ) { |
---|
| 705 | type->base = addIdListToType( type->base, ids ); |
---|
| 706 | } else { |
---|
[8f6f47d7] | 707 | type->function.idList = ids; |
---|
[68cd1ce] | 708 | } // if |
---|
[b87a5ed] | 709 | return type; |
---|
[3848e0e] | 710 | } else { |
---|
[ba7aa2d] | 711 | TypeData * newtype = new TypeData( TypeData::Function ); |
---|
[8f6f47d7] | 712 | newtype->function.idList = ids; |
---|
[b87a5ed] | 713 | return newtype; |
---|
[68cd1ce] | 714 | } // if |
---|
[2298f728] | 715 | } // addIdListToType |
---|
[974906e2] | 716 | |
---|
[ba7aa2d] | 717 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) { |
---|
[b87a5ed] | 718 | type = addIdListToType( type, ids ); |
---|
| 719 | return this; |
---|
[3848e0e] | 720 | } |
---|
| 721 | |
---|
[ba7aa2d] | 722 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { |
---|
[b87a5ed] | 723 | initializer = init; |
---|
| 724 | return this; |
---|
[3848e0e] | 725 | } |
---|
| 726 | |
---|
[67cf18c] | 727 | DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) { |
---|
[bb7422a] | 728 | assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." ); |
---|
[67cf18c] | 729 | variable.initializer = init; |
---|
| 730 | return this; |
---|
| 731 | } |
---|
| 732 | |
---|
[a46b69c] | 733 | DeclarationNode * DeclarationNode::cloneType( string * name ) { |
---|
| 734 | DeclarationNode * newnode = newName( name ); |
---|
[5bf685f] | 735 | newnode->type = maybeCopy( type ); |
---|
[a7c90d4] | 736 | newnode->copySpecifiers( this ); |
---|
[b87a5ed] | 737 | return newnode; |
---|
[3848e0e] | 738 | } |
---|
| 739 | |
---|
[4eb3a7c5] | 740 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o, bool copyattr ) { |
---|
[2298f728] | 741 | if ( ! o ) return nullptr; |
---|
[4eb3a7c5] | 742 | o->copySpecifiers( this, copyattr ); |
---|
[2298f728] | 743 | if ( type ) { |
---|
[af60383] | 744 | o->type = ::cloneBaseType( type, o->type ); |
---|
[68cd1ce] | 745 | } // if |
---|
[b87a5ed] | 746 | return o; |
---|
[51b7345] | 747 | } |
---|
| 748 | |
---|
[ba7aa2d] | 749 | DeclarationNode * DeclarationNode::extractAggregate() const { |
---|
[b87a5ed] | 750 | if ( type ) { |
---|
[ba7aa2d] | 751 | TypeData * ret = typeextractAggregate( type ); |
---|
[b87a5ed] | 752 | if ( ret ) { |
---|
[ba7aa2d] | 753 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 754 | newnode->type = ret; |
---|
[4eb3a7c5] | 755 | if ( ret->kind == TypeData::Aggregate ) { |
---|
| 756 | newnode->attributes.swap( ret->aggregate.attributes ); |
---|
[4a72fef] | 757 | } // if |
---|
[b87a5ed] | 758 | return newnode; |
---|
[843054c2] | 759 | } // if |
---|
| 760 | } // if |
---|
[2298f728] | 761 | return nullptr; |
---|
[3848e0e] | 762 | } |
---|
| 763 | |
---|
[45e753c] | 764 | // Get the non-anonymous name of the instance type of the declaration, |
---|
| 765 | // if one exists. |
---|
| 766 | static const std::string * getInstTypeOfName( ast::Decl * decl ) { |
---|
| 767 | if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) { |
---|
| 768 | if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) { |
---|
| 769 | if ( aggr->name.find("anonymous") == std::string::npos ) { |
---|
| 770 | return &aggr->name; |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | } |
---|
| 774 | return nullptr; |
---|
| 775 | } |
---|
| 776 | |
---|
[b38f6da] | 777 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList ) { |
---|
[a16764a6] | 778 | SemanticErrorException errors; |
---|
[bb7422a] | 779 | std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList ); |
---|
[2298f728] | 780 | |
---|
[dc3fbe5] | 781 | for ( const DeclarationNode * cur = firstNode ; cur ; cur = cur->next ) { |
---|
[b87a5ed] | 782 | try { |
---|
[45e753c] | 783 | bool extracted_named = false; |
---|
[692c1cc] | 784 | |
---|
[ba7aa2d] | 785 | if ( DeclarationNode * extr = cur->extractAggregate() ) { |
---|
[78e2fca] | 786 | assert( cur->type ); |
---|
[692c1cc] | 787 | |
---|
[45e753c] | 788 | if ( ast::Decl * decl = extr->build() ) { |
---|
[692c1cc] | 789 | *out++ = decl; |
---|
[3d7e53b] | 790 | |
---|
| 791 | // need to remember the cases where a declaration contains an anonymous aggregate definition |
---|
| 792 | assert( extr->type ); |
---|
| 793 | if ( extr->type->kind == TypeData::Aggregate ) { |
---|
[692c1cc] | 794 | // typedef struct { int A } B is the only case? |
---|
[4eb3a7c5] | 795 | extracted_named = ! extr->type->aggregate.anon; |
---|
[45e753c] | 796 | } else { |
---|
| 797 | extracted_named = true; |
---|
[3d7e53b] | 798 | } |
---|
[843054c2] | 799 | } // if |
---|
[f39096c] | 800 | delete extr; |
---|
[843054c2] | 801 | } // if |
---|
[2298f728] | 802 | |
---|
[45e753c] | 803 | if ( ast::Decl * decl = cur->build() ) { |
---|
| 804 | if ( "" == decl->name && !cur->get_inLine() ) { |
---|
| 805 | // Don't include anonymous declaration for named aggregates, |
---|
| 806 | // but do include them for anonymous aggregates, e.g.: |
---|
| 807 | // struct S { |
---|
| 808 | // struct T { int x; }; // no anonymous member |
---|
| 809 | // struct { int y; }; // anonymous member |
---|
| 810 | // struct T; // anonymous member |
---|
| 811 | // }; |
---|
| 812 | if ( extracted_named ) { |
---|
| 813 | continue; |
---|
| 814 | } |
---|
[692c1cc] | 815 | |
---|
[45e753c] | 816 | if ( auto name = getInstTypeOfName( decl ) ) { |
---|
| 817 | // Temporary: warn about anonymous member declarations of named types, since |
---|
| 818 | // this conflicts with the syntax for the forward declaration of an anonymous type. |
---|
| 819 | SemanticWarning( cur->location, Warning::AggrForwardDecl, name->c_str() ); |
---|
| 820 | } |
---|
[e07caa2] | 821 | } // if |
---|
[45e753c] | 822 | *out++ = decl; |
---|
[843054c2] | 823 | } // if |
---|
[45e753c] | 824 | } catch ( SemanticErrorException & e ) { |
---|
[b87a5ed] | 825 | errors.append( e ); |
---|
[843054c2] | 826 | } // try |
---|
[e07caa2] | 827 | } // for |
---|
[2298f728] | 828 | |
---|
[b87a5ed] | 829 | if ( ! errors.isEmpty() ) { |
---|
| 830 | throw errors; |
---|
[843054c2] | 831 | } // if |
---|
[2298f728] | 832 | } // buildList |
---|
[3848e0e] | 833 | |
---|
[3d7e53b] | 834 | // currently only builds assertions, function parameters, and return values |
---|
[6611177] | 835 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) { |
---|
[a16764a6] | 836 | SemanticErrorException errors; |
---|
[bb7422a] | 837 | std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList ); |
---|
[43c89a7] | 838 | |
---|
[dc3fbe5] | 839 | for ( const DeclarationNode * cur = firstNode; cur; cur = cur->next ) { |
---|
[b87a5ed] | 840 | try { |
---|
[bb7422a] | 841 | ast::Decl * decl = cur->build(); |
---|
[45e753c] | 842 | assertf( decl, "buildList: build for ast::DeclWithType." ); |
---|
[bb7422a] | 843 | if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) { |
---|
[47498bd] | 844 | dwt->location = cur->location; |
---|
[3ca7ef3] | 845 | *out++ = dwt; |
---|
[bb7422a] | 846 | } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) { |
---|
[3d7e53b] | 847 | // e.g., int foo(struct S) {} |
---|
[bb7422a] | 848 | auto inst = new ast::StructInstType( agg->name ); |
---|
| 849 | auto obj = new ast::ObjectDecl( cur->location, "", inst ); |
---|
| 850 | obj->linkage = linkage; |
---|
[3ca7ef3] | 851 | *out++ = obj; |
---|
[47498bd] | 852 | delete agg; |
---|
[bb7422a] | 853 | } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) { |
---|
[3d7e53b] | 854 | // e.g., int foo(union U) {} |
---|
[bb7422a] | 855 | auto inst = new ast::UnionInstType( agg->name ); |
---|
| 856 | auto obj = new ast::ObjectDecl( cur->location, |
---|
| 857 | "", inst, nullptr, ast::Storage::Classes(), |
---|
| 858 | linkage ); |
---|
[3ca7ef3] | 859 | *out++ = obj; |
---|
[bb7422a] | 860 | } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) { |
---|
[3d7e53b] | 861 | // e.g., int foo(enum E) {} |
---|
[bb7422a] | 862 | auto inst = new ast::EnumInstType( agg->name ); |
---|
| 863 | auto obj = new ast::ObjectDecl( cur->location, |
---|
| 864 | "", |
---|
| 865 | inst, |
---|
| 866 | nullptr, |
---|
| 867 | ast::Storage::Classes(), |
---|
| 868 | linkage |
---|
| 869 | ); |
---|
[3ca7ef3] | 870 | *out++ = obj; |
---|
[45e753c] | 871 | } else { |
---|
| 872 | assertf( false, "buildList: Could not convert to ast::DeclWithType." ); |
---|
[843054c2] | 873 | } // if |
---|
[45e753c] | 874 | } catch ( SemanticErrorException & e ) { |
---|
[b87a5ed] | 875 | errors.append( e ); |
---|
[843054c2] | 876 | } // try |
---|
[3a5131ed] | 877 | } // for |
---|
| 878 | |
---|
[b87a5ed] | 879 | if ( ! errors.isEmpty() ) { |
---|
| 880 | throw errors; |
---|
[843054c2] | 881 | } // if |
---|
[2298f728] | 882 | } // buildList |
---|
[3848e0e] | 883 | |
---|
[bb7422a] | 884 | void buildTypeList( const DeclarationNode * firstNode, |
---|
| 885 | std::vector<ast::ptr<ast::Type>> & outputList ) { |
---|
[a16764a6] | 886 | SemanticErrorException errors; |
---|
[bb7422a] | 887 | std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList ); |
---|
[2298f728] | 888 | |
---|
[dc3fbe5] | 889 | for ( const DeclarationNode * cur = firstNode ; cur ; cur = cur->next ) { |
---|
[b87a5ed] | 890 | try { |
---|
[ba7aa2d] | 891 | * out++ = cur->buildType(); |
---|
[45e753c] | 892 | } catch ( SemanticErrorException & e ) { |
---|
[b87a5ed] | 893 | errors.append( e ); |
---|
[843054c2] | 894 | } // try |
---|
[45e753c] | 895 | } // for |
---|
[2298f728] | 896 | |
---|
[b87a5ed] | 897 | if ( ! errors.isEmpty() ) { |
---|
| 898 | throw errors; |
---|
[843054c2] | 899 | } // if |
---|
[2298f728] | 900 | } // buildTypeList |
---|
[51b7345] | 901 | |
---|
[bb7422a] | 902 | ast::Decl * DeclarationNode::build() const { |
---|
[a16764a6] | 903 | if ( ! error.empty() ) SemanticError( this, error + " in declaration of " ); |
---|
[2298f728] | 904 | |
---|
[e994912] | 905 | if ( asmStmt ) { |
---|
[bb7422a] | 906 | auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() ); |
---|
| 907 | return new ast::AsmDecl( stmt->location, stmt ); |
---|
[e994912] | 908 | } // if |
---|
[2d019af] | 909 | if ( directiveStmt ) { |
---|
[bb7422a] | 910 | auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() ); |
---|
| 911 | return new ast::DirectiveDecl( stmt->location, stmt ); |
---|
[2d019af] | 912 | } // if |
---|
[e994912] | 913 | |
---|
[bb7422a] | 914 | if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) { |
---|
[f0ecf9b] | 915 | // otype is internally converted to dtype + otype parameters |
---|
[bb7422a] | 916 | static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension }; |
---|
| 917 | static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." ); |
---|
[8f60f0b] | 918 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); |
---|
[bb7422a] | 919 | ast::TypeDecl * ret = new ast::TypeDecl( location, |
---|
| 920 | *name, |
---|
| 921 | ast::Storage::Classes(), |
---|
| 922 | (ast::Type *)nullptr, |
---|
| 923 | kindMap[ variable.tyClass ], |
---|
| 924 | variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype, |
---|
| 925 | variable.initializer ? variable.initializer->buildType() : nullptr |
---|
| 926 | ); |
---|
| 927 | buildList( variable.assertions, ret->assertions ); |
---|
[2298f728] | 928 | return ret; |
---|
| 929 | } // if |
---|
| 930 | |
---|
[843054c2] | 931 | if ( type ) { |
---|
[dd020c0] | 932 | // Function specifiers can only appear on a function definition/declaration. |
---|
| 933 | // |
---|
| 934 | // inline _Noreturn int f(); // allowed |
---|
| 935 | // inline _Noreturn int g( int i ); // allowed |
---|
| 936 | // inline _Noreturn int i; // disallowed |
---|
[fb04321] | 937 | if ( type->kind != TypeData::Function && funcSpecs.any() ) { |
---|
[a16764a6] | 938 | SemanticError( this, "invalid function specifier for " ); |
---|
[dd020c0] | 939 | } // if |
---|
[284da8c] | 940 | // Forall qualifier can only appear on a function/aggregate definition/declaration. |
---|
| 941 | // |
---|
| 942 | // forall int f(); // allowed |
---|
| 943 | // forall int g( int i ); // allowed |
---|
| 944 | // forall int i; // disallowed |
---|
| 945 | if ( type->kind != TypeData::Function && type->forall ) { |
---|
| 946 | SemanticError( this, "invalid type qualifier for " ); |
---|
| 947 | } // if |
---|
[3ed994e] | 948 | bool isDelete = initializer && initializer->get_isDelete(); |
---|
[bb7422a] | 949 | ast::Decl * decl = buildDecl( |
---|
| 950 | type, |
---|
| 951 | name ? *name : string( "" ), |
---|
| 952 | storageClasses, |
---|
| 953 | maybeBuild( bitfieldWidth ), |
---|
| 954 | funcSpecs, |
---|
| 955 | linkage, |
---|
| 956 | asmName, |
---|
| 957 | isDelete ? nullptr : maybeBuild( initializer ), |
---|
| 958 | copy( attributes ) |
---|
| 959 | )->set_extension( extension ); |
---|
[3ed994e] | 960 | if ( isDelete ) { |
---|
[bb7422a] | 961 | auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl ); |
---|
[3ed994e] | 962 | dwt->isDeleted = true; |
---|
| 963 | } |
---|
| 964 | return decl; |
---|
[843054c2] | 965 | } // if |
---|
[2298f728] | 966 | |
---|
[f6e3e34] | 967 | if ( assert.condition ) { |
---|
[bb7422a] | 968 | auto cond = maybeBuild( assert.condition ); |
---|
| 969 | auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) ); |
---|
| 970 | return new ast::StaticAssertDecl( location, cond, msg ); |
---|
[f6e3e34] | 971 | } |
---|
| 972 | |
---|
[dd020c0] | 973 | // SUE's cannot have function specifiers, either |
---|
| 974 | // |
---|
[79aae15] | 975 | // inline _Noreturn struct S { ... }; // disallowed |
---|
| 976 | // inline _Noreturn enum E { ... }; // disallowed |
---|
[fb04321] | 977 | if ( funcSpecs.any() ) { |
---|
[a16764a6] | 978 | SemanticError( this, "invalid function specifier for " ); |
---|
[843054c2] | 979 | } // if |
---|
[e874605] | 980 | if ( enumInLine ) { |
---|
[bb7422a] | 981 | return new ast::InlineMemberDecl( location, |
---|
| 982 | *name, (ast::Type*)nullptr, storageClasses, linkage ); |
---|
[e874605] | 983 | } // if |
---|
[dd020c0] | 984 | assertf( name, "ObjectDecl must a have name\n" ); |
---|
[bb7422a] | 985 | auto ret = new ast::ObjectDecl( location, |
---|
| 986 | *name, |
---|
| 987 | (ast::Type*)nullptr, |
---|
| 988 | maybeBuild( initializer ), |
---|
| 989 | storageClasses, |
---|
| 990 | linkage, |
---|
| 991 | maybeBuild( bitfieldWidth ) |
---|
| 992 | ); |
---|
| 993 | ret->asmName = asmName; |
---|
| 994 | ret->extension = extension; |
---|
| 995 | return ret; |
---|
[51b7345] | 996 | } |
---|
| 997 | |
---|
[bb7422a] | 998 | ast::Type * DeclarationNode::buildType() const { |
---|
[b87a5ed] | 999 | assert( type ); |
---|
[974906e2] | 1000 | |
---|
[b87a5ed] | 1001 | switch ( type->kind ) { |
---|
[0d0931d] | 1002 | case TypeData::Aggregate: { |
---|
[bb7422a] | 1003 | ast::BaseInstType * ret = |
---|
| 1004 | buildComAggInst( type, copy( attributes ), linkage ); |
---|
| 1005 | buildList( type->aggregate.actuals, ret->params ); |
---|
[0d0931d] | 1006 | return ret; |
---|
| 1007 | } |
---|
| 1008 | case TypeData::Symbolic: { |
---|
[bb7422a] | 1009 | ast::TypeInstType * ret = new ast::TypeInstType( |
---|
| 1010 | *type->symbolic.name, |
---|
| 1011 | // This is just a default, the true value is not known yet. |
---|
| 1012 | ast::TypeDecl::Dtype, |
---|
| 1013 | buildQualifiers( type ), |
---|
| 1014 | copy( attributes ) ); |
---|
| 1015 | buildList( type->symbolic.actuals, ret->params ); |
---|
[0d0931d] | 1016 | return ret; |
---|
| 1017 | } |
---|
| 1018 | default: |
---|
[bb7422a] | 1019 | ast::Type * simpletypes = typebuild( type ); |
---|
| 1020 | // copy because member is const |
---|
| 1021 | simpletypes->attributes = attributes; |
---|
[c0aa336] | 1022 | return simpletypes; |
---|
[b87a5ed] | 1023 | } // switch |
---|
[3848e0e] | 1024 | } |
---|
| 1025 | |
---|
[b87a5ed] | 1026 | // Local Variables: // |
---|
| 1027 | // tab-width: 4 // |
---|
| 1028 | // mode: c++ // |
---|
| 1029 | // compile-command: "make install" // |
---|
| 1030 | // End: // |
---|