source: src/Parser/TypeData.cc @ 43c89a7

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 43c89a7 was 43c89a7, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

add hoistType flag (currently unused)

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