source: src/Parser/TypeData.cc@ 2e9aed4

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 resolv-new with_gc
Last change on this file since 2e9aed4 was 1f370451, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Carry attributes through typedefs

  • 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 : Mon Sep 25 18:33:41 2017
13// Update Count : 587
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 ) : 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.newStyle = false;
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;
125 break;
126 case Aggregate:
127 delete aggregate.name;
128 delete aggregate.params;
129 delete aggregate.actuals;
130 delete aggregate.fields;
131 // delete aggregate;
132 break;
133 case AggregateInst:
134 delete aggInst.aggregate;
135 delete aggInst.params;
136 // delete aggInst;
137 break;
138 case Enum:
139 delete enumeration.name;
140 delete enumeration.constants;
141 // delete enumeration;
142 break;
143 case Symbolic:
144 case SymbolicInst:
145 delete symbolic.name;
146 delete symbolic.params;
147 delete symbolic.actuals;
148 delete symbolic.assertions;
149 // delete symbolic;
150 break;
151 case Tuple:
152 // delete tuple->members;
153 delete tuple;
154 break;
155 case Typeof:
156 // delete typeexpr->expr;
157 delete typeexpr;
158 break;
159 case Builtin:
160 // delete builtin;
161 break;
162 } // switch
163} // TypeData::~TypeData
164
165
166TypeData * TypeData::clone() const {
167 TypeData * newtype = new TypeData( kind );
168 newtype->qualifiers = qualifiers;
169 newtype->base = maybeClone( base );
170 newtype->forall = maybeClone( forall );
171
172 switch ( kind ) {
173 case Unknown:
174 case EnumConstant:
175 case Pointer:
176 case Reference:
177 // nothing else to copy
178 break;
179 case Basic:
180 newtype->basictype = basictype;
181 newtype->complextype = complextype;
182 newtype->signedness = signedness;
183 newtype->length = length;
184 break;
185 case Array:
186 newtype->array.dimension = maybeClone( array.dimension );
187 newtype->array.isVarLen = array.isVarLen;
188 newtype->array.isStatic = array.isStatic;
189 break;
190 case Function:
191 newtype->function.params = maybeClone( function.params );
192 newtype->function.idList = maybeClone( function.idList );
193 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
194 newtype->function.body = maybeClone( function.body );
195 newtype->function.newStyle = function.newStyle;
196 break;
197 case Aggregate:
198 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
199 newtype->aggregate.params = maybeClone( aggregate.params );
200 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
201 newtype->aggregate.fields = maybeClone( aggregate.fields );
202 newtype->aggregate.kind = aggregate.kind;
203 newtype->aggregate.body = aggregate.body;
204 newtype->aggregate.tagged = aggregate.tagged;
205 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
206 break;
207 case AggregateInst:
208 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
209 newtype->aggInst.params = maybeClone( aggInst.params );
210 newtype->aggInst.hoistType = aggInst.hoistType;
211 break;
212 case Enum:
213 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
214 newtype->enumeration.constants = maybeClone( enumeration.constants );
215 newtype->enumeration.body = enumeration.body;
216 break;
217 case Symbolic:
218 case SymbolicInst:
219 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
220 newtype->symbolic.params = maybeClone( symbolic.params );
221 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
222 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
223 newtype->symbolic.isTypedef = symbolic.isTypedef;
224 break;
225 case Tuple:
226 newtype->tuple = maybeClone( tuple );
227 break;
228 case Typeof:
229 newtype->typeexpr = maybeClone( typeexpr );
230 break;
231 case Builtin:
232 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
233 newtype->builtintype = builtintype;
234 break;
235 } // switch
236 return newtype;
237} // TypeData::clone
238
239
240void TypeData::print( ostream &os, int indent ) const {
241 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
242 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
243 } // for
244
245 if ( forall ) {
246 os << "forall " << endl;
247 forall->printList( os, indent + 4 );
248 } // if
249
250 switch ( kind ) {
251 case Unknown:
252 os << "entity of unknown type ";
253 break;
254 case Pointer:
255 os << "pointer ";
256 if ( base ) {
257 os << "to ";
258 base->print( os, indent );
259 } // if
260 break;
261 case EnumConstant:
262 os << "enumeration constant ";
263 break;
264 case Basic:
265 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
266 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
267 assert( basictype != DeclarationNode::NoBasicType );
268 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
269 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
270 break;
271 case Array:
272 if ( array.isStatic ) {
273 os << "static ";
274 } // if
275 if ( array.dimension ) {
276 os << "array of ";
277 array.dimension->printOneLine( os, indent );
278 } else if ( array.isVarLen ) {
279 os << "variable-length array of ";
280 } else {
281 os << "open array of ";
282 } // if
283 if ( base ) {
284 base->print( os, indent );
285 } // if
286 break;
287 case Function:
288 os << "function" << endl;
289 if ( function.params ) {
290 os << string( indent + 2, ' ' ) << "with parameters " << endl;
291 function.params->printList( os, indent + 4 );
292 } else {
293 os << string( indent + 2, ' ' ) << "with no parameters " << endl;
294 } // if
295 if ( function.idList ) {
296 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
297 function.idList->printList( os, indent + 4 );
298 } // if
299 if ( function.oldDeclList ) {
300 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
301 function.oldDeclList->printList( os, indent + 4 );
302 } // if
303 os << string( indent + 2, ' ' ) << "returning ";
304 if ( base ) {
305 base->print( os, indent + 4 );
306 } else {
307 os << "nothing ";
308 } // if
309 os << endl;
310 if ( function.body ) {
311 os << string( indent + 2, ' ' ) << "with body " << endl;
312 function.body->printList( os, indent + 2 );
313 } // if
314 break;
315 case Aggregate:
316 os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
317 if ( aggregate.params ) {
318 os << string( indent + 2, ' ' ) << "with type parameters " << endl;
319 aggregate.params->printList( os, indent + 4 );
320 } // if
321 if ( aggregate.actuals ) {
322 os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
323 aggregate.actuals->printList( os, indent + 4 );
324 } // if
325 if ( aggregate.fields ) {
326 os << string( indent + 2, ' ' ) << "with members " << endl;
327 aggregate.fields->printList( os, indent + 4 );
328 } // if
329 if ( aggregate.body ) {
330 os << string( indent + 2, ' ' ) << " with body " << endl;
331 } // if
332 break;
333 case AggregateInst:
334 if ( aggInst.aggregate ) {
335 os << "instance of " ;
336 aggInst.aggregate->print( os, indent );
337 } else {
338 os << "instance of an unspecified aggregate ";
339 } // if
340 if ( aggInst.params ) {
341 os << string( indent + 2, ' ' ) << "with parameters " << endl;
342 aggInst.params->printList( os, indent + 2 );
343 } // if
344 break;
345 case Enum:
346 os << "enumeration ";
347 if ( enumeration.constants ) {
348 os << "with constants" << endl;
349 enumeration.constants->printList( os, indent + 2 );
350 } // if
351 if ( enumeration.body ) {
352 os << string( indent + 2, ' ' ) << " with body " << endl;
353 } // if
354 break;
355 case SymbolicInst:
356 os << "instance of type " << *symbolic.name;
357 if ( symbolic.actuals ) {
358 os << " with parameters" << endl;
359 symbolic.actuals->printList( os, indent + 2 );
360 } // if
361 break;
362 case Symbolic:
363 if ( symbolic.isTypedef ) {
364 os << "typedef definition ";
365 } else {
366 os << "type definition ";
367 } // if
368 if ( symbolic.params ) {
369 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
370 symbolic.params->printList( os, indent + 2 );
371 } // if
372 if ( symbolic.assertions ) {
373 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
374 symbolic.assertions->printList( os, indent + 4 );
375 os << string( indent + 2, ' ' );
376 } // if
377 if ( base ) {
378 os << "for ";
379 base->print( os, indent + 2 );
380 } // if
381 break;
382 case Tuple:
383 os << "tuple ";
384 if ( tuple ) {
385 os << "with members " << endl;
386 tuple->printList( os, indent + 2 );
387 } // if
388 break;
389 case Typeof:
390 os << "type-of expression ";
391 if ( typeexpr ) {
392 typeexpr->print( os, indent + 2 );
393 } // if
394 break;
395 case Builtin:
396 os << "gcc builtin type";
397 break;
398 default:
399 os << "internal error: TypeData::print " << kind << endl;
400 assert( false );
401 } // switch
402} // TypeData::print
403
404
405template< typename ForallList >
406void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
407 buildList( firstNode, outputList );
408 auto n = firstNode;
409 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
410 TypeDecl * td = static_cast<TypeDecl *>(*i);
411 if ( n->variable.tyClass == DeclarationNode::Otype ) {
412 // add assertion parameters to `type' tyvars in reverse order
413 // add dtor: void ^?{}(T *)
414 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
415 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 ) );
416 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
417
418 // add copy ctor: void ?{}(T *, T)
419 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
420 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 ) );
421 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
422 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
423
424 // add default ctor: void ?{}(T *)
425 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
426 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 ) );
427 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
428
429 // add assignment operator: T * ?=?(T *, T)
430 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
431 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 ) );
432 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
433 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
434 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
435 } // if
436 } // for
437} // buildForall
438
439
440Type * typebuild( const TypeData * td ) {
441 assert( td );
442 switch ( td->kind ) {
443 case TypeData::Unknown:
444 // fill in implicit int
445 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
446 case TypeData::Basic:
447 return buildBasicType( td );
448 case TypeData::Pointer:
449 return buildPointer( td );
450 case TypeData::Array:
451 return buildArray( td );
452 case TypeData::Reference:
453 return buildReference( td );
454 case TypeData::Function:
455 return buildFunction( td );
456 case TypeData::AggregateInst:
457 return buildAggInst( td );
458 case TypeData::EnumConstant:
459 // the name gets filled in later -- by SymTab::Validate
460 return new EnumInstType( buildQualifiers( td ), "" );
461 case TypeData::SymbolicInst:
462 return buildSymbolicInst( td );;
463 case TypeData::Tuple:
464 return buildTuple( td );
465 case TypeData::Typeof:
466 return buildTypeof( td );
467 case TypeData::Builtin:
468 if(td->builtintype == DeclarationNode::Zero) {
469 return new ZeroType( noQualifiers );
470 }
471 else if(td->builtintype == DeclarationNode::One) {
472 return new OneType( noQualifiers );
473 }
474 else {
475 return new VarArgsType( buildQualifiers( td ) );
476 }
477 case TypeData::Symbolic:
478 case TypeData::Enum:
479 case TypeData::Aggregate:
480 assert( false );
481 } // switch
482 return nullptr;
483} // typebuild
484
485
486TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
487 TypeData * ret = nullptr;
488
489 switch ( td->kind ) {
490 case TypeData::Aggregate:
491 if ( ! toplevel && td->aggregate.fields ) {
492 ret = td->clone();
493 } // if
494 break;
495 case TypeData::Enum:
496 if ( ! toplevel && td->enumeration.constants ) {
497 ret = td->clone();
498 } // if
499 break;
500 case TypeData::AggregateInst:
501 if ( td->aggInst.aggregate ) {
502 ret = typeextractAggregate( td->aggInst.aggregate, false );
503 } // if
504 break;
505 default:
506 if ( td->base ) {
507 ret = typeextractAggregate( td->base, false );
508 } // if
509 } // switch
510 return ret;
511} // typeextractAggregate
512
513
514Type::Qualifiers buildQualifiers( const TypeData * td ) {
515 return td->qualifiers;
516} // buildQualifiers
517
518
519static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
520 throw SemanticError( string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
521} // genTSError
522
523Type * buildBasicType( const TypeData * td ) {
524 BasicType::Kind ret;
525
526 switch ( td->basictype ) {
527 case DeclarationNode::Void:
528 if ( td->signedness != DeclarationNode::NoSignedness ) {
529 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
530 } // if
531 if ( td->length != DeclarationNode::NoLength ) {
532 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
533 } // if
534 return new VoidType( buildQualifiers( td ) );
535 break;
536
537 case DeclarationNode::Bool:
538 if ( td->signedness != DeclarationNode::NoSignedness ) {
539 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
540 } // if
541 if ( td->length != DeclarationNode::NoLength ) {
542 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
543 } // if
544
545 ret = BasicType::Bool;
546 break;
547
548 case DeclarationNode::Char:
549 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
550 // character types. The implementation shall define char to have the same range, representation, and behavior as
551 // either signed char or unsigned char.
552 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
553
554 if ( td->length != DeclarationNode::NoLength ) {
555 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
556 } // if
557
558 ret = chartype[ td->signedness ];
559 break;
560
561 case DeclarationNode::Int:
562 static BasicType::Kind inttype[2][4] = {
563 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
564 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
565 };
566
567 Integral: ;
568 if ( td->signedness == DeclarationNode::NoSignedness ) {
569 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
570 } // if
571 ret = inttype[ td->signedness ][ td->length ];
572 break;
573
574 case DeclarationNode::Int128:
575 ret = td->signedness == 1 ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
576 if ( td->length != DeclarationNode::NoLength ) {
577 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
578 } // if
579 break;
580
581 case DeclarationNode::Float:
582 case DeclarationNode::Float80:
583 case DeclarationNode::Float128:
584 case DeclarationNode::Double:
585 case DeclarationNode::LongDouble: // not set until below
586 static BasicType::Kind floattype[3][3] = {
587 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
588 { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
589 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
590 };
591
592 FloatingPoint: ;
593 if ( td->signedness != DeclarationNode::NoSignedness ) {
594 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
595 } // if
596 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
597 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
598 } // if
599 if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) {
600 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
601 } // if
602 if ( td->length == DeclarationNode::Long ) {
603 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
604 } // if
605
606 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
607 break;
608
609 case DeclarationNode::NoBasicType:
610 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
611 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
612 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
613 goto FloatingPoint;
614 } // if
615
616 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
617 goto Integral;
618 default:
619 assertf( false, "unknown basic type" );
620 return nullptr;
621 } // switch
622
623 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
624 buildForall( td->forall, bt->get_forall() );
625 return bt;
626} // buildBasicType
627
628
629PointerType * buildPointer( const TypeData * td ) {
630 PointerType * pt;
631 if ( td->base ) {
632 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
633 } else {
634 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
635 } // if
636 buildForall( td->forall, pt->get_forall() );
637 return pt;
638} // buildPointer
639
640
641ArrayType * buildArray( const TypeData * td ) {
642 ArrayType * at;
643 if ( td->base ) {
644 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
645 td->array.isVarLen, td->array.isStatic );
646 } else {
647 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
648 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
649 } // if
650 buildForall( td->forall, at->get_forall() );
651 return at;
652} // buildArray
653
654
655ReferenceType * buildReference( const TypeData * td ) {
656 ReferenceType * rt;
657 if ( td->base ) {
658 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
659 } else {
660 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
661 } // if
662 buildForall( td->forall, rt->get_forall() );
663 return rt;
664} // buildReference
665
666
667AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
668 assert( td->kind == TypeData::Aggregate );
669 AggregateDecl * at;
670 switch ( td->aggregate.kind ) {
671 case DeclarationNode::Struct:
672 case DeclarationNode::Coroutine:
673 case DeclarationNode::Monitor:
674 case DeclarationNode::Thread:
675 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
676 buildForall( td->aggregate.params, at->get_parameters() );
677 break;
678 case DeclarationNode::Union:
679 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
680 buildForall( td->aggregate.params, at->get_parameters() );
681 break;
682 case DeclarationNode::Trait:
683 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
684 buildList( td->aggregate.params, at->get_parameters() );
685 break;
686 default:
687 assert( false );
688 } // switch
689
690 buildList( td->aggregate.fields, at->get_members() );
691 at->set_body( td->aggregate.body );
692
693 return at;
694} // buildAggregate
695
696
697ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
698 switch ( type->kind ) {
699 case TypeData::Enum: {
700 if ( type->enumeration.body ) {
701 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
702 return new EnumInstType( buildQualifiers( type ), typedecl );
703 } else {
704 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
705 } // if
706 }
707 case TypeData::Aggregate: {
708 ReferenceToType * ret;
709 if ( type->aggregate.body ) {
710 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
711 switch ( type->aggregate.kind ) {
712 case DeclarationNode::Struct:
713 case DeclarationNode::Coroutine:
714 case DeclarationNode::Monitor:
715 case DeclarationNode::Thread:
716 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
717 break;
718 case DeclarationNode::Union:
719 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
720 break;
721 case DeclarationNode::Trait:
722 assert( false );
723 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
724 break;
725 default:
726 assert( false );
727 } // switch
728 } else {
729 switch ( type->aggregate.kind ) {
730 case DeclarationNode::Struct:
731 case DeclarationNode::Coroutine:
732 case DeclarationNode::Monitor:
733 case DeclarationNode::Thread:
734 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
735 break;
736 case DeclarationNode::Union:
737 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
738 break;
739 case DeclarationNode::Trait:
740 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
741 break;
742 default:
743 assert( false );
744 } // switch
745 } // if
746 return ret;
747 }
748 default:
749 assert( false );
750 } // switch
751} // buildAggInst
752
753
754ReferenceToType * buildAggInst( const TypeData * td ) {
755 assert( td->kind == TypeData::AggregateInst );
756
757 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
758 ReferenceToType * ret = nullptr;
759 TypeData * type = td->aggInst.aggregate;
760 switch ( type->kind ) {
761 case TypeData::Enum: {
762 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
763 }
764 case TypeData::Aggregate: {
765 switch ( type->aggregate.kind ) {
766 case DeclarationNode::Struct:
767 case DeclarationNode::Coroutine:
768 case DeclarationNode::Monitor:
769 case DeclarationNode::Thread:
770 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
771 break;
772 case DeclarationNode::Union:
773 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
774 break;
775 case DeclarationNode::Trait:
776 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
777 break;
778 default:
779 assert( false );
780 } // switch
781 }
782 break;
783 default:
784 assert( false );
785 } // switch
786
787 ret->set_hoistType( td->aggInst.hoistType );
788 buildList( td->aggInst.params, ret->get_parameters() );
789 buildForall( td->forall, ret->get_forall() );
790 return ret;
791} // buildAggInst
792
793
794NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
795 assert( td->kind == TypeData::Symbolic );
796 NamedTypeDecl * ret;
797 assert( td->base );
798 if ( td->symbolic.isTypedef ) {
799 ret = new TypedefDecl( name, scs, typebuild( td->base ), linkage );
800 } else {
801 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
802 } // if
803 buildList( td->symbolic.params, ret->get_parameters() );
804 buildList( td->symbolic.assertions, ret->get_assertions() );
805 ret->base->attributes.splice( ret->base->attributes.end(), attributes );
806 return ret;
807} // buildSymbolic
808
809
810EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
811 assert( td->kind == TypeData::Enum );
812 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
813 buildList( td->enumeration.constants, ret->get_members() );
814 list< Declaration * >::iterator members = ret->get_members().begin();
815 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
816 if ( cur->has_enumeratorValue() ) {
817 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
818 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
819 } // if
820 } // for
821 ret->set_body( td->enumeration.body );
822 return ret;
823} // buildEnum
824
825
826TypeInstType * buildSymbolicInst( const TypeData * td ) {
827 assert( td->kind == TypeData::SymbolicInst );
828 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
829 buildList( td->symbolic.actuals, ret->get_parameters() );
830 buildForall( td->forall, ret->get_forall() );
831 return ret;
832} // buildSymbolicInst
833
834
835TupleType * buildTuple( const TypeData * td ) {
836 assert( td->kind == TypeData::Tuple );
837 std::list< Type * > types;
838 buildTypeList( td->tuple, types );
839 TupleType * ret = new TupleType( buildQualifiers( td ), types );
840 buildForall( td->forall, ret->get_forall() );
841 return ret;
842} // buildTuple
843
844
845TypeofType * buildTypeof( const TypeData * td ) {
846 assert( td->kind == TypeData::Typeof );
847 assert( td->typeexpr );
848 // assert( td->typeexpr->expr );
849 return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
850} // buildTypeof
851
852
853Declaration * 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 ) {
854 if ( td->kind == TypeData::Function ) {
855 if ( td->function.idList ) { // KR function ?
856 buildKRFunction( td->function ); // transform into C11 function
857 } // if
858
859 FunctionDecl * decl;
860 Statement * stmt = maybeBuild<Statement>( td->function.body );
861 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
862 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
863 return decl->set_asmName( asmName );
864 } else if ( td->kind == TypeData::Aggregate ) {
865 return buildAggregate( td, attributes, linkage );
866 } else if ( td->kind == TypeData::Enum ) {
867 return buildEnum( td, attributes, linkage );
868 } else if ( td->kind == TypeData::Symbolic ) {
869 return buildSymbolic( td, attributes, name, scs, linkage );
870 } else {
871 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
872 } // if
873 return nullptr;
874} // buildDecl
875
876
877FunctionType * buildFunction( const TypeData * td ) {
878 assert( td->kind == TypeData::Function );
879 bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
880 if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
881 FunctionType * ft = new FunctionType( buildQualifiers( td ), 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 ) throw SemanticError( 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 ) throw SemanticError( 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 ) throw SemanticError( 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.