source: src/Parser/TypeData.cc @ 4fbdfae0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4fbdfae0 was cda7889, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Parser no longer takes address of parameters to reference functions, implicit otype functions take references

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