source: src/Parser/TypeData.cc@ ca37445

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since ca37445 was a16764a6, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Changed warning system to prepare for toggling warnings

  • Property mode set to 100644
File size: 33.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// TypeData.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 15:12:51 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Feb 22 15:49:00 2018
13// Update Count : 597
14//
15
16#include <cassert> // for assert
17#include <ostream> // for operator<<, ostream, basic_ostream
18
19#include "Common/SemanticError.h" // for SemanticError
20#include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB...
21#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
22#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl
23#include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only)
24#include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only)
25#include "SynTree/Statement.h" // for CompoundStmt, Statement
26#include "SynTree/Type.h" // for BasicType, Type, Type::ForallList
27#include "TypeData.h"
28
29class Attribute;
30
31using namespace std;
32
33TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
34 switch ( kind ) {
35 case Unknown:
36 case Pointer:
37 case Reference:
38 case EnumConstant:
39 // nothing else to initialize
40 break;
41 case Basic:
42 // basic = new Basic_t;
43 break;
44 case Array:
45 // array = new Array_t;
46 array.dimension = nullptr;
47 array.isVarLen = false;
48 array.isStatic = false;
49 break;
50 case Function:
51 // function = new Function_t;
52 function.params = nullptr;
53 function.idList = nullptr;
54 function.oldDeclList = nullptr;
55 function.body = nullptr;
56 function.withExprs = nullptr;
57 break;
58 // Enum is an Aggregate, so both structures are initialized together.
59 case Enum:
60 // enumeration = new Enumeration_t;
61 enumeration.name = nullptr;
62 enumeration.constants = nullptr;
63 enumeration.body = false;
64 case Aggregate:
65 // aggregate = new Aggregate_t;
66 aggregate.name = nullptr;
67 aggregate.params = nullptr;
68 aggregate.actuals = nullptr;
69 aggregate.fields = nullptr;
70 aggregate.body = false;
71 break;
72 case AggregateInst:
73 // aggInst = new AggInst_t;
74 aggInst.aggregate = nullptr;
75 aggInst.params = nullptr;
76 aggInst.hoistType = false;;
77 break;
78 case Symbolic:
79 case SymbolicInst:
80 // symbolic = new Symbolic_t;
81 symbolic.name = nullptr;
82 symbolic.params = nullptr;
83 symbolic.actuals = nullptr;
84 symbolic.assertions = nullptr;
85 break;
86 case Tuple:
87 // tuple = new Tuple_t;
88 tuple = nullptr;
89 break;
90 case Typeof:
91 // typeexpr = new Typeof_t;
92 typeexpr = nullptr;
93 break;
94 case Builtin:
95 // builtin = new Builtin_t;
96 break;
97 } // switch
98} // TypeData::TypeData
99
100
101TypeData::~TypeData() {
102 delete base;
103 delete forall;
104
105 switch ( kind ) {
106 case Unknown:
107 case Pointer:
108 case Reference:
109 case EnumConstant:
110 // nothing to destroy
111 break;
112 case Basic:
113 // delete basic;
114 break;
115 case Array:
116 delete array.dimension;
117 // delete array;
118 break;
119 case Function:
120 delete function.params;
121 delete function.idList;
122 delete function.oldDeclList;
123 delete function.body;
124 delete function.withExprs;
125 // delete function;
126 break;
127 case Aggregate:
128 delete aggregate.name;
129 delete aggregate.params;
130 delete aggregate.actuals;
131 delete aggregate.fields;
132 // delete aggregate;
133 break;
134 case AggregateInst:
135 delete aggInst.aggregate;
136 delete aggInst.params;
137 // delete aggInst;
138 break;
139 case Enum:
140 delete enumeration.name;
141 delete enumeration.constants;
142 // delete enumeration;
143 break;
144 case Symbolic:
145 case SymbolicInst:
146 delete symbolic.name;
147 delete symbolic.params;
148 delete symbolic.actuals;
149 delete symbolic.assertions;
150 // delete symbolic;
151 break;
152 case Tuple:
153 // delete tuple->members;
154 delete tuple;
155 break;
156 case Typeof:
157 // delete typeexpr->expr;
158 delete typeexpr;
159 break;
160 case Builtin:
161 // delete builtin;
162 break;
163 } // switch
164} // TypeData::~TypeData
165
166
167TypeData * TypeData::clone() const {
168 TypeData * newtype = new TypeData( kind );
169 newtype->qualifiers = qualifiers;
170 newtype->base = maybeClone( base );
171 newtype->forall = maybeClone( forall );
172
173 switch ( kind ) {
174 case Unknown:
175 case EnumConstant:
176 case Pointer:
177 case Reference:
178 // nothing else to copy
179 break;
180 case Basic:
181 newtype->basictype = basictype;
182 newtype->complextype = complextype;
183 newtype->signedness = signedness;
184 newtype->length = length;
185 break;
186 case Array:
187 newtype->array.dimension = maybeClone( array.dimension );
188 newtype->array.isVarLen = array.isVarLen;
189 newtype->array.isStatic = array.isStatic;
190 break;
191 case Function:
192 newtype->function.params = maybeClone( function.params );
193 newtype->function.idList = maybeClone( function.idList );
194 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
195 newtype->function.body = maybeClone( function.body );
196 newtype->function.withExprs = maybeClone( function.withExprs );
197 break;
198 case Aggregate:
199 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
200 newtype->aggregate.params = maybeClone( aggregate.params );
201 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
202 newtype->aggregate.fields = maybeClone( aggregate.fields );
203 newtype->aggregate.kind = aggregate.kind;
204 newtype->aggregate.body = aggregate.body;
205 newtype->aggregate.tagged = aggregate.tagged;
206 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
207 break;
208 case AggregateInst:
209 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
210 newtype->aggInst.params = maybeClone( aggInst.params );
211 newtype->aggInst.hoistType = aggInst.hoistType;
212 break;
213 case Enum:
214 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
215 newtype->enumeration.constants = maybeClone( enumeration.constants );
216 newtype->enumeration.body = enumeration.body;
217 break;
218 case Symbolic:
219 case SymbolicInst:
220 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
221 newtype->symbolic.params = maybeClone( symbolic.params );
222 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
223 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
224 newtype->symbolic.isTypedef = symbolic.isTypedef;
225 break;
226 case Tuple:
227 newtype->tuple = maybeClone( tuple );
228 break;
229 case Typeof:
230 newtype->typeexpr = maybeClone( typeexpr );
231 break;
232 case Builtin:
233 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
234 newtype->builtintype = builtintype;
235 break;
236 } // switch
237 return newtype;
238} // TypeData::clone
239
240
241void TypeData::print( ostream &os, int indent ) const {
242 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
243 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
244 } // for
245
246 if ( forall ) {
247 os << "forall " << endl;
248 forall->printList( os, indent + 4 );
249 } // if
250
251 switch ( kind ) {
252 case Unknown:
253 os << "entity of unknown type ";
254 break;
255 case Pointer:
256 os << "pointer ";
257 if ( base ) {
258 os << "to ";
259 base->print( os, indent );
260 } // if
261 break;
262 case EnumConstant:
263 os << "enumeration constant ";
264 break;
265 case Basic:
266 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
267 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
268 assert( basictype != DeclarationNode::NoBasicType );
269 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
270 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
271 break;
272 case Array:
273 if ( array.isStatic ) {
274 os << "static ";
275 } // if
276 if ( array.dimension ) {
277 os << "array of ";
278 array.dimension->printOneLine( os, indent );
279 } else if ( array.isVarLen ) {
280 os << "variable-length array of ";
281 } else {
282 os << "open array of ";
283 } // if
284 if ( base ) {
285 base->print( os, indent );
286 } // if
287 break;
288 case Function:
289 os << "function" << endl;
290 if ( function.params ) {
291 os << string( indent + 2, ' ' ) << "with parameters " << endl;
292 function.params->printList( os, indent + 4 );
293 } else {
294 os << string( indent + 2, ' ' ) << "with no parameters " << endl;
295 } // if
296 if ( function.idList ) {
297 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
298 function.idList->printList( os, indent + 4 );
299 } // if
300 if ( function.oldDeclList ) {
301 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
302 function.oldDeclList->printList( os, indent + 4 );
303 } // if
304 os << string( indent + 2, ' ' ) << "returning ";
305 if ( base ) {
306 base->print( os, indent + 4 );
307 } else {
308 os << "nothing ";
309 } // if
310 os << endl;
311 if ( function.body ) {
312 os << string( indent + 2, ' ' ) << "with body " << endl;
313 function.body->printList( os, indent + 2 );
314 } // if
315 break;
316 case Aggregate:
317 os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
318 if ( aggregate.params ) {
319 os << string( indent + 2, ' ' ) << "with type parameters " << endl;
320 aggregate.params->printList( os, indent + 4 );
321 } // if
322 if ( aggregate.actuals ) {
323 os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
324 aggregate.actuals->printList( os, indent + 4 );
325 } // if
326 if ( aggregate.fields ) {
327 os << string( indent + 2, ' ' ) << "with members " << endl;
328 aggregate.fields->printList( os, indent + 4 );
329 } // if
330 if ( aggregate.body ) {
331 os << string( indent + 2, ' ' ) << " with body " << endl;
332 } // if
333 break;
334 case AggregateInst:
335 if ( aggInst.aggregate ) {
336 os << "instance of " ;
337 aggInst.aggregate->print( os, indent );
338 } else {
339 os << "instance of an unspecified aggregate ";
340 } // if
341 if ( aggInst.params ) {
342 os << string( indent + 2, ' ' ) << "with parameters " << endl;
343 aggInst.params->printList( os, indent + 2 );
344 } // if
345 break;
346 case Enum:
347 os << "enumeration ";
348 if ( enumeration.constants ) {
349 os << "with constants" << endl;
350 enumeration.constants->printList( os, indent + 2 );
351 } // if
352 if ( enumeration.body ) {
353 os << string( indent + 2, ' ' ) << " with body " << endl;
354 } // if
355 break;
356 case SymbolicInst:
357 os << "instance of type " << *symbolic.name;
358 if ( symbolic.actuals ) {
359 os << " with parameters" << endl;
360 symbolic.actuals->printList( os, indent + 2 );
361 } // if
362 break;
363 case Symbolic:
364 if ( symbolic.isTypedef ) {
365 os << "typedef definition ";
366 } else {
367 os << "type definition ";
368 } // if
369 if ( symbolic.params ) {
370 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
371 symbolic.params->printList( os, indent + 2 );
372 } // if
373 if ( symbolic.assertions ) {
374 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
375 symbolic.assertions->printList( os, indent + 4 );
376 os << string( indent + 2, ' ' );
377 } // if
378 if ( base ) {
379 os << "for ";
380 base->print( os, indent + 2 );
381 } // if
382 break;
383 case Tuple:
384 os << "tuple ";
385 if ( tuple ) {
386 os << "with members " << endl;
387 tuple->printList( os, indent + 2 );
388 } // if
389 break;
390 case Typeof:
391 os << "type-of expression ";
392 if ( typeexpr ) {
393 typeexpr->print( os, indent + 2 );
394 } // if
395 break;
396 case Builtin:
397 os << "gcc builtin type";
398 break;
399 default:
400 os << "internal error: TypeData::print " << kind << endl;
401 assert( false );
402 } // switch
403} // TypeData::print
404
405
406template< typename ForallList >
407void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
408 buildList( firstNode, outputList );
409 auto n = firstNode;
410 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
411 TypeDecl * td = static_cast<TypeDecl *>(*i);
412 if ( n->variable.tyClass == DeclarationNode::Otype ) {
413 // add assertion parameters to `type' tyvars in reverse order
414 // add dtor: void ^?{}(T *)
415 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
416 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
417 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
418
419 // add copy ctor: void ?{}(T *, T)
420 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
421 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
422 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
423 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
424
425 // add default ctor: void ?{}(T *)
426 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
427 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
428 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
429
430 // add assignment operator: T * ?=?(T *, T)
431 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
432 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
433 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
434 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
435 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
436 } // if
437 } // for
438} // buildForall
439
440
441Type * typebuild( const TypeData * td ) {
442 assert( td );
443 switch ( td->kind ) {
444 case TypeData::Unknown:
445 // fill in implicit int
446 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
447 case TypeData::Basic:
448 return buildBasicType( td );
449 case TypeData::Pointer:
450 return buildPointer( td );
451 case TypeData::Array:
452 return buildArray( td );
453 case TypeData::Reference:
454 return buildReference( td );
455 case TypeData::Function:
456 return buildFunction( td );
457 case TypeData::AggregateInst:
458 return buildAggInst( td );
459 case TypeData::EnumConstant:
460 // the name gets filled in later -- by SymTab::Validate
461 return new EnumInstType( buildQualifiers( td ), "" );
462 case TypeData::SymbolicInst:
463 return buildSymbolicInst( td );;
464 case TypeData::Tuple:
465 return buildTuple( td );
466 case TypeData::Typeof:
467 return buildTypeof( td );
468 case TypeData::Builtin:
469 if(td->builtintype == DeclarationNode::Zero) {
470 return new ZeroType( noQualifiers );
471 }
472 else if(td->builtintype == DeclarationNode::One) {
473 return new OneType( noQualifiers );
474 }
475 else {
476 return new VarArgsType( buildQualifiers( td ) );
477 }
478 case TypeData::Symbolic:
479 case TypeData::Enum:
480 case TypeData::Aggregate:
481 assert( false );
482 } // switch
483 return nullptr;
484} // typebuild
485
486
487TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
488 TypeData * ret = nullptr;
489
490 switch ( td->kind ) {
491 case TypeData::Aggregate:
492 if ( ! toplevel && td->aggregate.fields ) {
493 ret = td->clone();
494 } // if
495 break;
496 case TypeData::Enum:
497 if ( ! toplevel && td->enumeration.constants ) {
498 ret = td->clone();
499 } // if
500 break;
501 case TypeData::AggregateInst:
502 if ( td->aggInst.aggregate ) {
503 ret = typeextractAggregate( td->aggInst.aggregate, false );
504 } // if
505 break;
506 default:
507 if ( td->base ) {
508 ret = typeextractAggregate( td->base, false );
509 } // if
510 } // switch
511 return ret;
512} // typeextractAggregate
513
514
515Type::Qualifiers buildQualifiers( const TypeData * td ) {
516 return td->qualifiers;
517} // buildQualifiers
518
519
520static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
521 SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
522} // genTSError
523
524Type * buildBasicType( const TypeData * td ) {
525 BasicType::Kind ret;
526
527 switch ( td->basictype ) {
528 case DeclarationNode::Void:
529 if ( td->signedness != DeclarationNode::NoSignedness ) {
530 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
531 } // if
532 if ( td->length != DeclarationNode::NoLength ) {
533 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
534 } // if
535 return new VoidType( buildQualifiers( td ) );
536 break;
537
538 case DeclarationNode::Bool:
539 if ( td->signedness != DeclarationNode::NoSignedness ) {
540 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
541 } // if
542 if ( td->length != DeclarationNode::NoLength ) {
543 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
544 } // if
545
546 ret = BasicType::Bool;
547 break;
548
549 case DeclarationNode::Char:
550 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
551 // character types. The implementation shall define char to have the same range, representation, and behavior as
552 // either signed char or unsigned char.
553 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
554
555 if ( td->length != DeclarationNode::NoLength ) {
556 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
557 } // if
558
559 ret = chartype[ td->signedness ];
560 break;
561
562 case DeclarationNode::Int:
563 static BasicType::Kind inttype[2][4] = {
564 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
565 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
566 };
567
568 Integral: ;
569 if ( td->signedness == DeclarationNode::NoSignedness ) {
570 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
571 } // if
572 ret = inttype[ td->signedness ][ td->length ];
573 break;
574
575 case DeclarationNode::Int128:
576 ret = td->signedness == 1 ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
577 if ( td->length != DeclarationNode::NoLength ) {
578 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
579 } // if
580 break;
581
582 case DeclarationNode::Float:
583 case DeclarationNode::Float80:
584 case DeclarationNode::Float128:
585 case DeclarationNode::Double:
586 case DeclarationNode::LongDouble: // not set until below
587 static BasicType::Kind floattype[3][3] = {
588 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
589 { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
590 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
591 };
592
593 FloatingPoint: ;
594 if ( td->signedness != DeclarationNode::NoSignedness ) {
595 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
596 } // if
597 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
598 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
599 } // if
600 if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) {
601 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
602 } // if
603 if ( td->length == DeclarationNode::Long ) {
604 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
605 } // if
606
607 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
608 break;
609
610 case DeclarationNode::NoBasicType:
611 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
612 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
613 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
614 goto FloatingPoint;
615 } // if
616
617 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
618 goto Integral;
619 default:
620 assertf( false, "unknown basic type" );
621 return nullptr;
622 } // switch
623
624 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
625 buildForall( td->forall, bt->get_forall() );
626 return bt;
627} // buildBasicType
628
629
630PointerType * buildPointer( const TypeData * td ) {
631 PointerType * pt;
632 if ( td->base ) {
633 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
634 } else {
635 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
636 } // if
637 buildForall( td->forall, pt->get_forall() );
638 return pt;
639} // buildPointer
640
641
642ArrayType * buildArray( const TypeData * td ) {
643 ArrayType * at;
644 if ( td->base ) {
645 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
646 td->array.isVarLen, td->array.isStatic );
647 } else {
648 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
649 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
650 } // if
651 buildForall( td->forall, at->get_forall() );
652 return at;
653} // buildArray
654
655
656ReferenceType * buildReference( const TypeData * td ) {
657 ReferenceType * rt;
658 if ( td->base ) {
659 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
660 } else {
661 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
662 } // if
663 buildForall( td->forall, rt->get_forall() );
664 return rt;
665} // buildReference
666
667
668AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
669 assert( td->kind == TypeData::Aggregate );
670 AggregateDecl * at;
671 switch ( td->aggregate.kind ) {
672 case DeclarationNode::Struct:
673 case DeclarationNode::Coroutine:
674 case DeclarationNode::Monitor:
675 case DeclarationNode::Thread:
676 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
677 buildForall( td->aggregate.params, at->get_parameters() );
678 break;
679 case DeclarationNode::Union:
680 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
681 buildForall( td->aggregate.params, at->get_parameters() );
682 break;
683 case DeclarationNode::Trait:
684 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
685 buildList( td->aggregate.params, at->get_parameters() );
686 break;
687 default:
688 assert( false );
689 } // switch
690
691 buildList( td->aggregate.fields, at->get_members() );
692 at->set_body( td->aggregate.body );
693
694 return at;
695} // buildAggregate
696
697
698ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
699 switch ( type->kind ) {
700 case TypeData::Enum: {
701 if ( type->enumeration.body ) {
702 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
703 return new EnumInstType( buildQualifiers( type ), typedecl );
704 } else {
705 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
706 } // if
707 }
708 case TypeData::Aggregate: {
709 ReferenceToType * ret;
710 if ( type->aggregate.body ) {
711 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
712 switch ( type->aggregate.kind ) {
713 case DeclarationNode::Struct:
714 case DeclarationNode::Coroutine:
715 case DeclarationNode::Monitor:
716 case DeclarationNode::Thread:
717 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
718 break;
719 case DeclarationNode::Union:
720 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
721 break;
722 case DeclarationNode::Trait:
723 assert( false );
724 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
725 break;
726 default:
727 assert( false );
728 } // switch
729 } else {
730 switch ( type->aggregate.kind ) {
731 case DeclarationNode::Struct:
732 case DeclarationNode::Coroutine:
733 case DeclarationNode::Monitor:
734 case DeclarationNode::Thread:
735 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
736 break;
737 case DeclarationNode::Union:
738 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
739 break;
740 case DeclarationNode::Trait:
741 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
742 break;
743 default:
744 assert( false );
745 } // switch
746 } // if
747 return ret;
748 }
749 default:
750 assert( false );
751 } // switch
752} // buildAggInst
753
754
755ReferenceToType * buildAggInst( const TypeData * td ) {
756 assert( td->kind == TypeData::AggregateInst );
757
758 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
759 ReferenceToType * ret = nullptr;
760 TypeData * type = td->aggInst.aggregate;
761 switch ( type->kind ) {
762 case TypeData::Enum: {
763 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
764 }
765 case TypeData::Aggregate: {
766 switch ( type->aggregate.kind ) {
767 case DeclarationNode::Struct:
768 case DeclarationNode::Coroutine:
769 case DeclarationNode::Monitor:
770 case DeclarationNode::Thread:
771 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
772 break;
773 case DeclarationNode::Union:
774 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
775 break;
776 case DeclarationNode::Trait:
777 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
778 break;
779 default:
780 assert( false );
781 } // switch
782 }
783 break;
784 default:
785 assert( false );
786 } // switch
787
788 ret->set_hoistType( td->aggInst.hoistType );
789 buildList( td->aggInst.params, ret->get_parameters() );
790 buildForall( td->forall, ret->get_forall() );
791 return ret;
792} // buildAggInst
793
794
795NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
796 assert( td->kind == TypeData::Symbolic );
797 NamedTypeDecl * ret;
798 assert( td->base );
799 if ( td->symbolic.isTypedef ) {
800 ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
801 } else {
802 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
803 } // if
804 buildList( td->symbolic.params, ret->get_parameters() );
805 buildList( td->symbolic.assertions, ret->get_assertions() );
806 ret->base->attributes.splice( ret->base->attributes.end(), attributes );
807 return ret;
808} // buildSymbolic
809
810
811EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
812 assert( td->kind == TypeData::Enum );
813 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
814 buildList( td->enumeration.constants, ret->get_members() );
815 list< Declaration * >::iterator members = ret->get_members().begin();
816 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
817 if ( cur->has_enumeratorValue() ) {
818 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
819 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
820 } // if
821 } // for
822 ret->set_body( td->enumeration.body );
823 return ret;
824} // buildEnum
825
826
827TypeInstType * buildSymbolicInst( const TypeData * td ) {
828 assert( td->kind == TypeData::SymbolicInst );
829 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
830 buildList( td->symbolic.actuals, ret->get_parameters() );
831 buildForall( td->forall, ret->get_forall() );
832 return ret;
833} // buildSymbolicInst
834
835
836TupleType * buildTuple( const TypeData * td ) {
837 assert( td->kind == TypeData::Tuple );
838 std::list< Type * > types;
839 buildTypeList( td->tuple, types );
840 TupleType * ret = new TupleType( buildQualifiers( td ), types );
841 buildForall( td->forall, ret->get_forall() );
842 return ret;
843} // buildTuple
844
845
846TypeofType * buildTypeof( const TypeData * td ) {
847 assert( td->kind == TypeData::Typeof );
848 assert( td->typeexpr );
849 // assert( td->typeexpr->expr );
850 return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
851} // buildTypeof
852
853
854Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, Expression *asmName, Initializer * init, std::list< Attribute * > attributes ) {
855 if ( td->kind == TypeData::Function ) {
856 if ( td->function.idList ) { // KR function ?
857 buildKRFunction( td->function ); // transform into C11 function
858 } // if
859
860 FunctionDecl * decl;
861 Statement * stmt = maybeBuild<Statement>( td->function.body );
862 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
863 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
864 buildList( td->function.withExprs, decl->withExprs );
865 return decl->set_asmName( asmName );
866 } else if ( td->kind == TypeData::Aggregate ) {
867 return buildAggregate( td, attributes, linkage );
868 } else if ( td->kind == TypeData::Enum ) {
869 return buildEnum( td, attributes, linkage );
870 } else if ( td->kind == TypeData::Symbolic ) {
871 return buildSymbolic( td, attributes, name, scs, linkage );
872 } else {
873 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
874 } // if
875 return nullptr;
876} // buildDecl
877
878
879FunctionType * buildFunction( const TypeData * td ) {
880 assert( td->kind == TypeData::Function );
881 FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
882 buildList( td->function.params, ft->get_parameters() );
883 buildForall( td->forall, ft->get_forall() );
884 if ( td->base ) {
885 switch ( td->base->kind ) {
886 case TypeData::Tuple:
887 buildList( td->base->tuple, ft->get_returnVals() );
888 break;
889 default:
890 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
891 } // switch
892 } else {
893 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
894 } // if
895 return ft;
896} // buildFunction
897
898
899// Transform KR routine declarations into C99 routine declarations:
900//
901// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
902//
903// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
904// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
905// extra names are disallowed.
906//
907// Note, there is no KR routine-prototype syntax:
908//
909// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
910// rtn(); // valid KR prototype
911
912void buildKRFunction( const TypeData::Function_t & function ) {
913 assert( ! function.params );
914 // loop over declaration first as it is easier to spot errors
915 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
916 // scan ALL parameter names for each declaration name to check for duplicates
917 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
918 if ( *decl->name == *param->name ) {
919 // type set => parameter name already transformed by a declaration names so there is a duplicate
920 // declaration name attempting a second transformation
921 if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
922 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
923 // parameter name attempting a second transformation
924 if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
925 param->type = decl->type; // set copy declaration type to parameter type
926 decl->type = nullptr; // reset declaration type
927 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
928 } // if
929 } // for
930 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
931 if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
932 } // for
933
934 // Parameter names without a declaration default to type int:
935 //
936 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
937
938 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
939 if ( ! param->type ) { // generate type int for empty parameter type
940 param->type = new TypeData( TypeData::Basic );
941 param->type->basictype = DeclarationNode::Int;
942 } // if
943 } // for
944
945 function.params = function.idList; // newly modified idList becomes parameters
946 function.idList = nullptr; // idList now empty
947 delete function.oldDeclList; // deletes entire list
948 function.oldDeclList = nullptr; // reset
949} // buildKRFunction
950
951// Local Variables: //
952// tab-width: 4 //
953// mode: c++ //
954// compile-command: "make install" //
955// End: //
Note: See TracBrowser for help on using the repository browser.