source: src/Parser/ExpressionNode.cc @ 51d6d6a

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 51d6d6a was 8135d4c, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 17.0 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//
[0caaa6a]7// ExpressionNode.cc --
8//
[b87a5ed]9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 13:17:07 2015
[65cdc1e]11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Aug  2 11:12:00 2017
13// Update Count     : 568
[0caaa6a]14//
[b87a5ed]15
[ea6332d]16#include <cassert>                 // for assert
[d180746]17#include <stdio.h>                 // for sscanf, size_t
18#include <climits>                 // for LLONG_MAX, LONG_MAX, INT_MAX, UINT...
19#include <list>                    // for list
20#include <sstream>                 // for basic_istream::operator>>, basic_i...
21#include <string>                  // for string, operator+, operator==
[51b7345]22
[d180746]23#include "Common/SemanticError.h"  // for SemanticError
24#include "Common/utility.h"        // for maybeMoveBuild, maybeBuild, CodeLo...
25#include "ParseNode.h"             // for ExpressionNode, maybeMoveBuildType
26#include "SynTree/Constant.h"      // for Constant
27#include "SynTree/Declaration.h"   // for EnumDecl, StructDecl, UnionDecl
28#include "SynTree/Expression.h"    // for Expression, ConstantExpr, NameExpr
29#include "SynTree/Statement.h"     // for CompoundStmt, Statement
30#include "SynTree/Type.h"          // for BasicType, Type, Type::Qualifiers
31#include "parserutility.h"         // for notZeroExpr
32
33class Initializer;
[51b7345]34
35using namespace std;
36
[cd623a4]37//##############################################################################
38
[7bf7fb9]39// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
40//
41//              prefix action constant action suffix
42//
43// Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:
44//
45//              constant BEGIN CONT ...
46//              <CONT>(...)? BEGIN 0 ... // possible empty suffix
47//
48// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
49// type.
50
[65cdc1e]51extern const Type::Qualifiers noQualifiers;             // no qualifiers on constants
[7bf7fb9]52
53static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
54static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
55static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
56static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
57static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
58static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
59
[a2e0687]60Expression * build_constantInteger( const std::string & str ) {
[7bf7fb9]61        static const BasicType::Kind kind[2][3] = {
62                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
63                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
64        };
65        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
66        int size;                                                                                       // 0 => int, 1 => long, 2 => long long
[6165ce7]67        unsigned long long int v;                                                       // converted integral value
[7bf7fb9]68        size_t last = str.length() - 1;                                         // last character of constant
[6165ce7]69        Expression * ret;
[7bf7fb9]70
[6165ce7]71        // special constants
72        if ( str == "0" ) {
73                ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
74                goto CLEANUP;
75        } // if
76        if ( str == "1" ) {
77                ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
78                goto CLEANUP;
79        } // if
[ea6332d]80
[7bf7fb9]81        if ( str[0] == '0' ) {                                                          // octal/hex constant ?
82                dec = false;
83                if ( last != 0 && checkX( str[1] ) ) {                  // hex constant ?
84                        sscanf( (char *)str.c_str(), "%llx", &v );
85                        //printf( "%llx %llu\n", v, v );
86                } else {                                                                                // octal constant
87                        sscanf( (char *)str.c_str(), "%llo", &v );
88                        //printf( "%llo %llu\n", v, v );
89                } // if
90        } else {                                                                                        // decimal constant ?
91                sscanf( (char *)str.c_str(), "%llu", &v );
92                //printf( "%llu %llu\n", v, v );
93        } // if
94
95        if ( v <= INT_MAX ) {                                                           // signed int
96                size = 0;
97        } else if ( v <= UINT_MAX && ! dec ) {                          // unsigned int
98                size = 0;
99                Unsigned = true;                                                                // unsigned
100        } else if ( v <= LONG_MAX ) {                                           // signed long int
101                size = 1;
102        } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
103                size = 1;
104                Unsigned = true;                                                                // unsigned long int
105        } else if ( v <= LLONG_MAX ) {                                          // signed long long int
106                size = 2;
107        } else {                                                                                        // unsigned long long int
108                size = 2;
109                Unsigned = true;                                                                // unsigned long long int
110        } // if
111
112        if ( checkU( str[last] ) ) {                                            // suffix 'u' ?
113                Unsigned = true;
114                if ( last > 0 && checkL( str[last - 1] ) ) {    // suffix 'l' ?
115                        size = 1;
116                        if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ?
117                                size = 2;
118                        } // if
119                } // if
120        } else if ( checkL( str[ last ] ) ) {                           // suffix 'l' ?
121                size = 1;
122                if ( last > 0 && checkL( str[last - 1] ) ) {    // suffix 'll' ?
123                        size = 2;
124                        if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ?
125                                Unsigned = true;
126                        } // if
127                } else {
128                        if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?
129                                Unsigned = true;
130                        } // if
131                } // if
132        } // if
133
[6165ce7]134        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
135  CLEANUP:
[ab57786]136        delete &str;                                                                            // created by lex
137        return ret;
[7bf7fb9]138} // build_constantInteger
[51b7345]139
[a2e0687]140Expression * build_constantFloat( const std::string & str ) {
[7bf7fb9]141        static const BasicType::Kind kind[2][3] = {
142                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
143                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
144        };
[59c24b6]145
[7bf7fb9]146        bool complx = false;                                                            // real, complex
147        int size = 1;                                                                           // 0 => float, 1 => double (default), 2 => long double
148        // floating-point constant has minimum of 2 characters: 1. or .1
149        size_t last = str.length() - 1;
[d56e5bc]150        double v;
151
152        sscanf( str.c_str(), "%lg", &v );
[51b7345]153
[7bf7fb9]154        if ( checkI( str[last] ) ) {                                            // imaginary ?
155                complx = true;
156                last -= 1;                                                                              // backup one character
157        } // if
[0caaa6a]158
[7bf7fb9]159        if ( checkF( str[last] ) ) {                                            // float ?
160                size = 0;
161        } else if ( checkD( str[last] ) ) {                                     // double ?
162                size = 1;
163        } else if ( checkL( str[last] ) ) {                                     // long double ?
164                size = 2;
165        } // if
166        if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
167                complx = true;
168        } // if
[51b7345]169
[ac10576]170        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
[ab57786]171        delete &str;                                                                            // created by lex
172        return ret;
[7bf7fb9]173} // build_constantFloat
[51b7345]174
[a2e0687]175Expression * build_constantChar( const std::string & str ) {
[ac10576]176        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
[ab57786]177        delete &str;                                                                            // created by lex
178        return ret;
[7bf7fb9]179} // build_constantChar
[51b7345]180
[a2e0687]181ConstantExpr * build_constantStr( const std::string & str ) {
[7bf7fb9]182        // string should probably be a primitive type
[a2e0687]183        ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
184                                                                   new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
[7bf7fb9]185                                                                   false, false );
[a2e0687]186        ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
[ab57786]187        delete &str;                                                                            // created by lex
188        return ret;
[7bf7fb9]189} // build_constantStr
[51b7345]190
[8780e30]191Expression * build_field_name_FLOATINGconstant( const std::string & str ) {
192        // str is of the form A.B -> separate at the . and return member expression
193        int a, b;
194        char dot;
195        std::stringstream ss( str );
196        ss >> a >> dot >> b;
[d56e5bc]197        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
[8780e30]198        delete &str;
199        return ret;
200} // build_field_name_FLOATINGconstant
201
202Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
203        if ( fracts ) {
204                if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
205                        memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
206                        return memberExpr;
207                } else {
208                        return new UntypedMemberExpr( fracts, fieldName );
209                }
210        }
211        return fieldName;
212} // make_field_name_fraction_constants
213
214Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
215        return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
216} // build_field_name_fraction_constants
217
218Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) {
[0213af6]219        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
[8780e30]220        Expression * ret = build_constantInteger( *new std::string( str.substr(1) ) );
221        delete &str;
222        return ret;
223} // build_field_name_REALFRACTIONconstant
224
225Expression * build_field_name_REALDECIMALconstant( const std::string & str ) {
[0213af6]226        if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
[8780e30]227        Expression * ret = build_constantInteger( *new std::string( str.substr( 0, str.size()-1 ) ) );
228        delete &str;
229        return ret;
230} // build_field_name_REALDECIMALconstant
231
[a2e0687]232NameExpr * build_varref( const string * name ) {
233        NameExpr * expr = new NameExpr( *name, nullptr );
[7ecbb7e]234        delete name;
235        return expr;
[a2e0687]236} // build_varref
[51b1202]237
[a2e0687]238
239static const char * OperName[] = {                                              // must harmonize with OperKinds
[5721a6d]240        // diadic
[e5f2a67]241        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&",
[5721a6d]242        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
[e5f2a67]243        "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
[d9e2280]244        "?[?]", "...",
[5721a6d]245        // monadic
246        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
[a2e0687]247}; // OperName
[5721a6d]248
[a2e0687]249Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
250        Type * targetType = maybeMoveBuildType( decl_node );
[d1625f8]251        if ( dynamic_cast< VoidType * >( targetType ) ) {
[064e3ff]252                delete targetType;
[7ecbb7e]253                return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
[064e3ff]254        } else {
[7ecbb7e]255                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
[064e3ff]256        } // if
[a2e0687]257} // build_cast
[a5f0529]258
[a2e0687]259Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
260        Type * targetType = maybeMoveBuildType( decl_node );
261        Expression * castArg = maybeMoveBuild< Expression >( expr_node );
[a5f0529]262        return new VirtualCastExpr( castArg, targetType );
[a2e0687]263} // build_virtual_cast
[064e3ff]264
[a2e0687]265Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
266        UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
[064e3ff]267        return ret;
[a2e0687]268} // build_fieldSel
[064e3ff]269
[a2e0687]270Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
271        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
[64ac636]272        deref->location = expr_node->location;
[7ecbb7e]273        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
[a2e0687]274        UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
[064e3ff]275        return ret;
[a2e0687]276} // build_pfieldSel
[064e3ff]277
[a2e0687]278Expression * build_addressOf( ExpressionNode * expr_node ) {
[7ecbb7e]279                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
[a2e0687]280} // build_addressOf
281
282Expression * build_sizeOfexpr( ExpressionNode * expr_node ) {
[7ecbb7e]283        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
[a2e0687]284} // build_sizeOfexpr
285
286Expression * build_sizeOftype( DeclarationNode * decl_node ) {
[4f147cc]287        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
[a2e0687]288} // build_sizeOftype
289
290Expression * build_alignOfexpr( ExpressionNode * expr_node ) {
[7ecbb7e]291        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
[a2e0687]292} // build_alignOfexpr
293
294Expression * build_alignOftype( DeclarationNode * decl_node ) {
[4f147cc]295        return new AlignofExpr( maybeMoveBuildType( decl_node) );
[a2e0687]296} // build_alignOftype
297
298Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
[a7c90d4]299        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
[ac71a86]300        delete member;
301        return ret;
[a2e0687]302} // build_offsetOf
[064e3ff]303
[a2e0687]304Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
[7ecbb7e]305        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
[a2e0687]306} // build_and_or
[51e076e]307
[a2e0687]308Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
[7880579]309        std::list< Expression * > args;
[7ecbb7e]310        args.push_back( maybeMoveBuild< Expression >(expr_node) );
[d9e2280]311        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]312} // build_unary_val
313
314Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
[7880579]315        std::list< Expression * > args;
[cda7889]316        args.push_back(  maybeMoveBuild< Expression >(expr_node) ); // xxx
[d9e2280]317        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]318} // build_unary_ptr
319
320Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[7880579]321        std::list< Expression * > args;
[7ecbb7e]322        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
323        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
[d9e2280]324        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]325} // build_binary_val
326
327Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[7880579]328        std::list< Expression * > args;
[cda7889]329        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
[7ecbb7e]330        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
[d9e2280]331        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[a2e0687]332} // build_binary_ptr
[51e076e]333
[a2e0687]334Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
[7ecbb7e]335        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
[a2e0687]336} // build_cond
[51e076e]337
[a2e0687]338Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
[7ecbb7e]339        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
[a2e0687]340} // build_comma
[51e076e]341
[a2e0687]342Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ) {
[7ecbb7e]343        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
[a2e0687]344} // build_attrexpr
345
346Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) {
[4f147cc]347        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
[a2e0687]348} // build_attrtype
[9706554]349
[a2e0687]350Expression * build_tuple( ExpressionNode * expr_node ) {
[907eccb]351        std::list< Expression * > exprs;
352        buildMoveList( expr_node, exprs );
353        return new UntypedTupleExpr( exprs );;
[a2e0687]354} // build_tuple
[9706554]355
[a2e0687]356Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
[7880579]357        std::list< Expression * > args;
[7ecbb7e]358        buildMoveList( expr_node, args );
359        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
[a2e0687]360} // build_func
[9706554]361
[a2e0687]362Expression * build_range( ExpressionNode * low, ExpressionNode * high ) {
[7ecbb7e]363        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
[a2e0687]364} // build_range
[51b7345]365
[a2e0687]366Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ) {
[7ecbb7e]367        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
[a2e0687]368} // build_asmexpr
[7f5566b]369
[a2e0687]370Expression * build_valexpr( StatementNode * s ) {
[af5c204a]371        return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) );
[a2e0687]372} // build_valexpr
373
374Expression * build_typevalue( DeclarationNode * decl ) {
[4f147cc]375        return new TypeExpr( maybeMoveBuildType( decl ) );
[a2e0687]376} // build_typevalue
[630a82a]377
[a2e0687]378Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
[7880579]379        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
[630a82a]380        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
[7ecbb7e]381                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
[630a82a]382        // these types do not have associated type information
383        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
[fbcde64]384                if ( newDeclStructDecl->has_body() ) {
385                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
386                } else {
387                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
388                } // if
[630a82a]389        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
[fbcde64]390                if ( newDeclUnionDecl->has_body() ) {
391                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
392                } else {
393                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
394                } // if
[630a82a]395        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
[fbcde64]396                if ( newDeclEnumDecl->has_body() ) {
397                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
398                } else {
399                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
400                } // if
[630a82a]401        } else {
402                assert( false );
403        } // if
[a2e0687]404} // build_compoundLiteral
[630a82a]405
[b87a5ed]406// Local Variables: //
407// tab-width: 4 //
408// mode: c++ //
409// compile-command: "make install" //
410// End: //
Note: See TracBrowser for help on using the repository browser.