source: src/Parser/TypeData.cc @ 5802a4f

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 5802a4f was 7756647, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into tuples

Conflicts:

src/Parser/TypeData.cc
src/Parser/parser.cc
src/Parser/parser.yy

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