source: src/Parser/DeclarationNode.cc@ 29702ad

ADT ast-experimental
Last change on this file since 29702ad was e4d7c1c, checked in by JiadaL <j82liang@…>, 3 years ago

Implement enum Hiding

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