source: src/Parser/TypeData.cc @ 5b639ee

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 5b639ee was 5b639ee, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

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