source: src/Parser/ExpressionNode.cc@ aefcc3b

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since aefcc3b was 3b58d91, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

add AST nodes TupleIndexExpr, MemberTupleExpr, MassAssignExpr, and MultipleAssignExpr, modify parser to produce nodes for field tuples, modify UntypedMemberExpr to contain a list of members

  • Property mode set to 100644
File size: 12.8 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
[097e2b0]11// Last Modified By : Peter A. Buhr
[a839867]12// Last Modified On : Thu Aug 25 21:39:40 2016
13// Update Count : 503
[0caaa6a]14//
[b87a5ed]15
[51b73452]16#include <cassert>
17#include <cctype>
[7bf7fb9]18#include <climits>
19#include <cstdio>
[51b73452]20#include <algorithm>
[59db689]21#include <sstream>
[51b73452]22
23#include "ParseNode.h"
[630a82a]24#include "TypeData.h"
[51b73452]25#include "SynTree/Constant.h"
26#include "SynTree/Expression.h"
[630a82a]27#include "SynTree/Declaration.h"
[d3b7937]28#include "Common/UnimplementedError.h"
[51b73452]29#include "parseutility.h"
[d3b7937]30#include "Common/utility.h"
[51b73452]31
32using namespace std;
33
[7880579]34ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.get_name() ), extension( other.extension ) {}
[51b73452]35
[cd623a4]36//##############################################################################
37
[7bf7fb9]38// Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:
39//
40// prefix action constant action suffix
41//
42// Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:
43//
44// constant BEGIN CONT ...
45// <CONT>(...)? BEGIN 0 ... // possible empty suffix
46//
47// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
48// type.
49
50static Type::Qualifiers emptyQualifiers; // no qualifiers on constants
51
52static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
53static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }
54static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }
55static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }
56static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }
57static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
58
[ac71a86]59Expression *build_constantInteger( const std::string & str ) {
[7bf7fb9]60 static const BasicType::Kind kind[2][3] = {
61 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
62 { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
63 };
64 bool dec = true, Unsigned = false; // decimal, unsigned constant
65 int size; // 0 => int, 1 => long, 2 => long long
66 unsigned long long v; // converted integral value
67 size_t last = str.length() - 1; // last character of constant
68
69 if ( str[0] == '0' ) { // octal/hex constant ?
70 dec = false;
71 if ( last != 0 && checkX( str[1] ) ) { // hex constant ?
72 sscanf( (char *)str.c_str(), "%llx", &v );
73 //printf( "%llx %llu\n", v, v );
74 } else { // octal constant
75 sscanf( (char *)str.c_str(), "%llo", &v );
76 //printf( "%llo %llu\n", v, v );
77 } // if
78 } else { // decimal constant ?
79 sscanf( (char *)str.c_str(), "%llu", &v );
80 //printf( "%llu %llu\n", v, v );
81 } // if
82
83 if ( v <= INT_MAX ) { // signed int
84 size = 0;
85 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int
86 size = 0;
87 Unsigned = true; // unsigned
88 } else if ( v <= LONG_MAX ) { // signed long int
89 size = 1;
90 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
91 size = 1;
92 Unsigned = true; // unsigned long int
93 } else if ( v <= LLONG_MAX ) { // signed long long int
94 size = 2;
95 } else { // unsigned long long int
96 size = 2;
97 Unsigned = true; // unsigned long long int
98 } // if
99
100 if ( checkU( str[last] ) ) { // suffix 'u' ?
101 Unsigned = true;
102 if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'l' ?
103 size = 1;
104 if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ?
105 size = 2;
106 } // if
107 } // if
108 } else if ( checkL( str[ last ] ) ) { // suffix 'l' ?
109 size = 1;
110 if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'll' ?
111 size = 2;
112 if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ?
113 Unsigned = true;
114 } // if
115 } else {
116 if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?
117 Unsigned = true;
118 } // if
119 } // if
120 } // if
121
[ab57786]122 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ) );
123 delete &str; // created by lex
124 return ret;
[7bf7fb9]125} // build_constantInteger
[51b73452]126
[ac71a86]127Expression *build_constantFloat( const std::string & str ) {
[7bf7fb9]128 static const BasicType::Kind kind[2][3] = {
129 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
130 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
131 };
[59c24b6]132
[7bf7fb9]133 bool complx = false; // real, complex
134 int size = 1; // 0 => float, 1 => double (default), 2 => long double
135 // floating-point constant has minimum of 2 characters: 1. or .1
136 size_t last = str.length() - 1;
[51b73452]137
[7bf7fb9]138 if ( checkI( str[last] ) ) { // imaginary ?
139 complx = true;
140 last -= 1; // backup one character
141 } // if
[0caaa6a]142
[7bf7fb9]143 if ( checkF( str[last] ) ) { // float ?
144 size = 0;
145 } else if ( checkD( str[last] ) ) { // double ?
146 size = 1;
147 } else if ( checkL( str[last] ) ) { // long double ?
148 size = 2;
149 } // if
150 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
151 complx = true;
152 } // if
[51b73452]153
[ab57786]154 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) );
155 delete &str; // created by lex
156 return ret;
[7bf7fb9]157} // build_constantFloat
[51b73452]158
[ac71a86]159Expression *build_constantChar( const std::string & str ) {
[ab57786]160 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) );
161 delete &str; // created by lex
162 return ret;
[7bf7fb9]163} // build_constantChar
[51b73452]164
[ac71a86]165ConstantExpr *build_constantStr( const std::string & str ) {
[7bf7fb9]166 // string should probably be a primitive type
167 ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ),
168 new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ),
169 toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"'
170 false, false );
[ab57786]171 ConstantExpr * ret = new ConstantExpr( Constant( at, str ) );
172 delete &str; // created by lex
173 return ret;
[7bf7fb9]174} // build_constantStr
[51b73452]175
[d1625f8]176NameExpr * build_varref( const string *name, bool labelp ) {
[7ecbb7e]177 NameExpr *expr = new NameExpr( *name, nullptr );
178 delete name;
179 return expr;
[51b1202]180}
181
[d9e2280]182static const char *OperName[] = {
[5721a6d]183 // diadic
[7bf7fb9]184 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",
[5721a6d]185 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
[a839867]186 "?=?", "?@=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
[d9e2280]187 "?[?]", "...",
[5721a6d]188 // monadic
189 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
190};
191
[d1625f8]192Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {
[4f147cc]193 Type *targetType = maybeMoveBuildType( decl_node );
[d1625f8]194 if ( dynamic_cast< VoidType * >( targetType ) ) {
[064e3ff]195 delete targetType;
[7ecbb7e]196 return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
[064e3ff]197 } else {
[7ecbb7e]198 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
[064e3ff]199 } // if
200}
201
[3b58d91]202Expression *build_fieldSel( ExpressionNode *expr_node, Expression *member ) {
203 UntypedMemberExpr *ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
[064e3ff]204 return ret;
205}
206
[3b58d91]207Expression *build_pfieldSel( ExpressionNode *expr_node, Expression *member ) {
[064e3ff]208 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
[7ecbb7e]209 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
[3b58d91]210 UntypedMemberExpr *ret = new UntypedMemberExpr( member, deref );
[064e3ff]211 return ret;
212}
213
214Expression *build_addressOf( ExpressionNode *expr_node ) {
[7ecbb7e]215 return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
[064e3ff]216}
[d1625f8]217Expression *build_sizeOfexpr( ExpressionNode *expr_node ) {
[7ecbb7e]218 return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
[064e3ff]219}
[d1625f8]220Expression *build_sizeOftype( DeclarationNode *decl_node ) {
[4f147cc]221 return new SizeofExpr( maybeMoveBuildType( decl_node ) );
[d1625f8]222}
223Expression *build_alignOfexpr( ExpressionNode *expr_node ) {
[7ecbb7e]224 return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
[d1625f8]225}
226Expression *build_alignOftype( DeclarationNode *decl_node ) {
[4f147cc]227 return new AlignofExpr( maybeMoveBuildType( decl_node) );
[064e3ff]228}
[d1625f8]229Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) {
[4f147cc]230 Expression* ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
[ac71a86]231 delete member;
232 return ret;
[064e3ff]233}
234
[51e076e]235Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {
[7ecbb7e]236 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
[51e076e]237}
238
[d9e2280]239Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
[7880579]240 std::list< Expression * > args;
[7ecbb7e]241 args.push_back( maybeMoveBuild< Expression >(expr_node) );
[d9e2280]242 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[9706554]243}
[d9e2280]244Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
[7880579]245 std::list< Expression * > args;
[7ecbb7e]246 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
[d9e2280]247 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[51e076e]248}
[d9e2280]249Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
[7880579]250 std::list< Expression * > args;
[7ecbb7e]251 args.push_back( maybeMoveBuild< Expression >(expr_node1) );
252 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
[d9e2280]253 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[51e076e]254}
[d9e2280]255Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
[7880579]256 std::list< Expression * > args;
[7ecbb7e]257 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
258 args.push_back( maybeMoveBuild< Expression >(expr_node2) );
[d9e2280]259 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
[9706554]260}
[51e076e]261
262Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {
[7ecbb7e]263 return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
[51e076e]264}
265
266Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
[7ecbb7e]267 return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
[51e076e]268}
269
[d1625f8]270Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {
[7ecbb7e]271 return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
[d1625f8]272}
273Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) {
[4f147cc]274 return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
[9706554]275}
276
[d1625f8]277Expression *build_tuple( ExpressionNode * expr_node ) {
[9706554]278 TupleExpr *ret = new TupleExpr();
[7ecbb7e]279 buildMoveList( expr_node, ret->get_exprs() );
[9706554]280 return ret;
281}
282
[d1625f8]283Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
[7880579]284 std::list< Expression * > args;
[7ecbb7e]285 buildMoveList( expr_node, args );
286 return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
[9706554]287}
288
[d9e2280]289Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
[7ecbb7e]290 return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
[51b73452]291}
292
[e82aa9df]293Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
[7ecbb7e]294 return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
[7f5566b]295}
296
[d1625f8]297Expression *build_valexpr( StatementNode *s ) {
[7ecbb7e]298 return new UntypedValofExpr( maybeMoveBuild< Statement >(s), nullptr );
[3848e0e]299}
[d1625f8]300Expression *build_typevalue( DeclarationNode *decl ) {
[4f147cc]301 return new TypeExpr( maybeMoveBuildType( decl ) );
[630a82a]302}
303
[d1625f8]304Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {
[7880579]305 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
[630a82a]306 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
[7ecbb7e]307 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
[630a82a]308 // these types do not have associated type information
309 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) {
[7ecbb7e]310 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
[630a82a]311 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) {
[7ecbb7e]312 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
[630a82a]313 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) {
[7ecbb7e]314 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
[630a82a]315 } else {
316 assert( false );
317 } // if
318}
319
[b87a5ed]320// Local Variables: //
321// tab-width: 4 //
322// mode: c++ //
323// compile-command: "make install" //
324// End: //
Note: See TracBrowser for help on using the repository browser.