source: src/Parser/DeclarationNode.cc@ 158b026

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 158b026 was 033ff37, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove attribute expression '@'name mechanism

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