source: src/Parser/DeclarationNode.cc@ 120867e

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 120867e was 9e7236f4, checked in by JiadaL <j82liang@…>, 3 years ago

Resolution of struct enum. The codegen of struct enum will be in the next commit

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