[0dd3a2f] | 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 | // |
---|
[3be261a] | 7 | // Expression.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[e04ef3a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[a5f0529] | 12 | // Last Modified On : Tue Jul 25 14:15:47 2017 |
---|
| 13 | // Update Count : 54 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[ea6332d] | 16 | #include "SynTree/Expression.h" |
---|
| 17 | |
---|
| 18 | #include <cassert> // for assert, assertf |
---|
| 19 | #include <iostream> // for ostream, operator<<, basic_ostream |
---|
| 20 | #include <list> // for list, _List_iterator, list<>::co... |
---|
| 21 | |
---|
| 22 | #include "Common/utility.h" // for maybeClone, cloneAll, deleteAll |
---|
| 23 | #include "Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
| 24 | #include "Expression.h" // for Expression, ImplicitCopyCtorExpr |
---|
| 25 | #include "InitTweak/InitTweak.h" // for getCallArg, getPointerBase |
---|
| 26 | #include "Initializer.h" // for Designation, Initializer |
---|
| 27 | #include "Statement.h" // for CompoundStmt, ExprStmt, Statement |
---|
| 28 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
| 29 | #include "SynTree/Constant.h" // for Constant |
---|
| 30 | #include "Type.h" // for Type, BasicType, Type::Qualifiers |
---|
| 31 | #include "TypeSubstitution.h" // for TypeSubstitution |
---|
[51b7345] | 32 | |
---|
[b0440b7] | 33 | #include "GenPoly/Lvalue.h" |
---|
[51b7345] | 34 | |
---|
[68195a6] | 35 | void printInferParams( const InferredParams & inferParams, std::ostream &os, Indenter indent, int level ) { |
---|
| 36 | if ( ! inferParams.empty() ) { |
---|
| 37 | os << indent << "with inferred parameters " << level << ":" << std::endl; |
---|
| 38 | for ( InferredParams::const_iterator i = inferParams.begin(); i != inferParams.end(); ++i ) { |
---|
| 39 | os << indent+1; |
---|
| 40 | Declaration::declFromId( i->second.decl )->printShort( os, indent+1 ); |
---|
| 41 | os << std::endl; |
---|
| 42 | printInferParams( *i->second.inferParams, os, indent+1, level+1 ); |
---|
| 43 | } // for |
---|
| 44 | } // if |
---|
| 45 | } |
---|
| 46 | |
---|
[bf4b4cf] | 47 | Expression::Expression() : result( 0 ), env( 0 ) {} |
---|
[0dd3a2f] | 48 | |
---|
[df626eb] | 49 | Expression::Expression( const Expression &other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), extension( other.extension ), inferParams( other.inferParams ) { |
---|
[0dd3a2f] | 50 | } |
---|
| 51 | |
---|
| 52 | Expression::~Expression() { |
---|
| 53 | delete env; |
---|
[51b7345] | 54 | } |
---|
| 55 | |
---|
[50377a4] | 56 | void Expression::print( std::ostream &os, Indenter indent ) const { |
---|
[68195a6] | 57 | printInferParams( inferParams, os, indent+1, 0 ); |
---|
| 58 | |
---|
[0dd3a2f] | 59 | if ( env ) { |
---|
[50377a4] | 60 | os << std::endl << indent << "... with environment:" << std::endl; |
---|
| 61 | env->print( os, indent+1 ); |
---|
[0dd3a2f] | 62 | } // if |
---|
[e04ef3a] | 63 | |
---|
| 64 | if ( extension ) { |
---|
[50377a4] | 65 | os << std::endl << indent << "... with extension:"; |
---|
[e04ef3a] | 66 | } // if |
---|
[51b7345] | 67 | } |
---|
| 68 | |
---|
[bf4b4cf] | 69 | ConstantExpr::ConstantExpr( Constant _c ) : Expression(), constant( _c ) { |
---|
[906e24d] | 70 | set_result( constant.get_type()->clone() ); |
---|
[51b7345] | 71 | } |
---|
| 72 | |
---|
[0dd3a2f] | 73 | ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) { |
---|
| 74 | } |
---|
[51b7345] | 75 | |
---|
[50377a4] | 76 | void ConstantExpr::print( std::ostream &os, Indenter indent ) const { |
---|
[60089f4] | 77 | os << "constant expression " ; |
---|
[cf0941d] | 78 | constant.print( os ); |
---|
[0dd3a2f] | 79 | Expression::print( os, indent ); |
---|
[51b7345] | 80 | } |
---|
| 81 | |
---|
[ddb80bd] | 82 | long long int ConstantExpr::intValue() const { |
---|
| 83 | if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) { |
---|
| 84 | if ( basicType->isInteger() ) { |
---|
| 85 | return get_constant()->get_ival(); |
---|
| 86 | } |
---|
| 87 | } else if ( dynamic_cast< OneType * >( result ) ) { |
---|
| 88 | return 1; |
---|
| 89 | } else if ( dynamic_cast< ZeroType * >( result ) ) { |
---|
| 90 | return 0; |
---|
| 91 | } |
---|
[a16764a6] | 92 | SemanticError( this, "Constant expression of non-integral type " ); |
---|
[ddb80bd] | 93 | } |
---|
| 94 | |
---|
[bf4b4cf] | 95 | VariableExpr::VariableExpr( DeclarationWithType *_var ) : Expression(), var( _var ) { |
---|
[db4ecc5] | 96 | assert( var ); |
---|
| 97 | assert( var->get_type() ); |
---|
[906e24d] | 98 | Type * type = var->get_type()->clone(); |
---|
[615a096] | 99 | type->set_lvalue( true ); |
---|
[3de176d] | 100 | |
---|
| 101 | // xxx - doesn't quite work yet - get different alternatives with the same cost |
---|
| 102 | |
---|
| 103 | // // enumerators are not lvalues |
---|
| 104 | // if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( var->get_type() ) ) { |
---|
| 105 | // assert( inst->baseEnum ); |
---|
| 106 | // EnumDecl * decl = inst->baseEnum; |
---|
[0690350] | 107 | // long long int value; |
---|
| 108 | // if ( decl->valueOf( var, value ) ) { |
---|
| 109 | // type->set_lvalue( false ); |
---|
[3de176d] | 110 | // } |
---|
| 111 | // } |
---|
| 112 | |
---|
[906e24d] | 113 | set_result( type ); |
---|
[51b7345] | 114 | } |
---|
| 115 | |
---|
[0dd3a2f] | 116 | VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { |
---|
[51b7345] | 117 | } |
---|
| 118 | |
---|
[8a6cf7e] | 119 | VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) { |
---|
| 120 | VariableExpr * funcExpr = new VariableExpr( func ); |
---|
| 121 | funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) ); |
---|
| 122 | return funcExpr; |
---|
| 123 | } |
---|
| 124 | |
---|
[50377a4] | 125 | void VariableExpr::print( std::ostream &os, Indenter indent ) const { |
---|
[89231bc] | 126 | os << "Variable Expression: "; |
---|
[50377a4] | 127 | var->printShort(os, indent); |
---|
[0dd3a2f] | 128 | Expression::print( os, indent ); |
---|
[51b7345] | 129 | } |
---|
| 130 | |
---|
[bf4b4cf] | 131 | SizeofExpr::SizeofExpr( Expression *expr_ ) : |
---|
| 132 | Expression(), expr(expr_), type(0), isType(false) { |
---|
[906e24d] | 133 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[51b7345] | 134 | } |
---|
| 135 | |
---|
[bf4b4cf] | 136 | SizeofExpr::SizeofExpr( Type *type_ ) : |
---|
| 137 | Expression(), expr(0), type(type_), isType(true) { |
---|
[906e24d] | 138 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[51b7345] | 139 | } |
---|
| 140 | |
---|
[0dd3a2f] | 141 | SizeofExpr::SizeofExpr( const SizeofExpr &other ) : |
---|
| 142 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
[51b7345] | 143 | } |
---|
| 144 | |
---|
[50377a4] | 145 | void SizeofExpr::print( std::ostream &os, Indenter indent) const { |
---|
[89231bc] | 146 | os << "Sizeof Expression on: "; |
---|
[50377a4] | 147 | if (isType) type->print(os, indent+1); |
---|
| 148 | else expr->print(os, indent+1); |
---|
[47534159] | 149 | Expression::print( os, indent ); |
---|
| 150 | } |
---|
| 151 | |
---|
[bf4b4cf] | 152 | AlignofExpr::AlignofExpr( Expression *expr_ ) : |
---|
| 153 | Expression(), expr(expr_), type(0), isType(false) { |
---|
[906e24d] | 154 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[47534159] | 155 | } |
---|
| 156 | |
---|
[bf4b4cf] | 157 | AlignofExpr::AlignofExpr( Type *type_ ) : |
---|
| 158 | Expression(), expr(0), type(type_), isType(true) { |
---|
[906e24d] | 159 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[47534159] | 160 | } |
---|
| 161 | |
---|
| 162 | AlignofExpr::AlignofExpr( const AlignofExpr &other ) : |
---|
| 163 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
| 164 | } |
---|
| 165 | |
---|
[50377a4] | 166 | void AlignofExpr::print( std::ostream &os, Indenter indent) const { |
---|
[f19339e] | 167 | os << "Alignof Expression on: "; |
---|
[50377a4] | 168 | if (isType) type->print(os, indent+1); |
---|
| 169 | else expr->print(os, indent+1); |
---|
[0dd3a2f] | 170 | Expression::print( os, indent ); |
---|
[51b7345] | 171 | } |
---|
| 172 | |
---|
[bf4b4cf] | 173 | UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type, const std::string &member ) : |
---|
| 174 | Expression(), type(type), member(member) { |
---|
[50377a4] | 175 | assert( type ); |
---|
[906e24d] | 176 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[2a4b088] | 177 | } |
---|
| 178 | |
---|
| 179 | UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) : |
---|
| 180 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
| 181 | |
---|
[50377a4] | 182 | void UntypedOffsetofExpr::print( std::ostream &os, Indenter indent) const { |
---|
| 183 | os << "Untyped Offsetof Expression on member " << member << " of "; |
---|
| 184 | type->print(os, indent+1); |
---|
[2a4b088] | 185 | Expression::print( os, indent ); |
---|
| 186 | } |
---|
| 187 | |
---|
[bf4b4cf] | 188 | OffsetofExpr::OffsetofExpr( Type *type, DeclarationWithType *member ) : |
---|
| 189 | Expression(), type(type), member(member) { |
---|
[50377a4] | 190 | assert( member ); |
---|
| 191 | assert( type ); |
---|
[906e24d] | 192 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[25a054f] | 193 | } |
---|
| 194 | |
---|
| 195 | OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) : |
---|
[79970ed] | 196 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
[25a054f] | 197 | |
---|
[50377a4] | 198 | void OffsetofExpr::print( std::ostream &os, Indenter indent) const { |
---|
| 199 | os << "Offsetof Expression on member " << member->name << " of "; |
---|
| 200 | type->print(os, indent+1); |
---|
[25a054f] | 201 | Expression::print( os, indent ); |
---|
| 202 | } |
---|
| 203 | |
---|
[bf4b4cf] | 204 | OffsetPackExpr::OffsetPackExpr( StructInstType *type ) : Expression(), type( type ) { |
---|
[50377a4] | 205 | assert( type ); |
---|
[906e24d] | 206 | set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) ); |
---|
[afc1045] | 207 | } |
---|
| 208 | |
---|
| 209 | OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {} |
---|
| 210 | |
---|
[50377a4] | 211 | void OffsetPackExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 212 | os << "Offset pack expression on "; |
---|
| 213 | type->print(os, indent+1); |
---|
[25a054f] | 214 | Expression::print( os, indent ); |
---|
| 215 | } |
---|
| 216 | |
---|
[bf4b4cf] | 217 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_ ) : |
---|
| 218 | Expression(), attr( attr ), expr(expr_), type(0), isType(false) { |
---|
[51b7345] | 219 | } |
---|
| 220 | |
---|
[bf4b4cf] | 221 | AttrExpr::AttrExpr( Expression *attr, Type *type_ ) : |
---|
| 222 | Expression(), attr( attr ), expr(0), type(type_), isType(true) { |
---|
[51b7345] | 223 | } |
---|
| 224 | |
---|
[0dd3a2f] | 225 | AttrExpr::AttrExpr( const AttrExpr &other ) : |
---|
| 226 | Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
[51b7345] | 227 | } |
---|
| 228 | |
---|
[50377a4] | 229 | void AttrExpr::print( std::ostream &os, Indenter indent) const { |
---|
[f19339e] | 230 | os << "Attr "; |
---|
[50377a4] | 231 | attr->print( os, indent+1); |
---|
[0dd3a2f] | 232 | if ( isType || expr ) { |
---|
| 233 | os << "applied to: "; |
---|
[50377a4] | 234 | if (isType) type->print(os, indent+1); |
---|
| 235 | else expr->print(os, indent+1); |
---|
[0dd3a2f] | 236 | } // if |
---|
| 237 | Expression::print( os, indent ); |
---|
[51b7345] | 238 | } |
---|
| 239 | |
---|
[c0bf94e] | 240 | CastExpr::CastExpr( Expression *arg, Type *toType, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) { |
---|
[906e24d] | 241 | set_result(toType); |
---|
[51b7345] | 242 | } |
---|
| 243 | |
---|
[c0bf94e] | 244 | CastExpr::CastExpr( Expression *arg, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) { |
---|
[906e24d] | 245 | set_result( new VoidType( Type::Qualifiers() ) ); |
---|
[51b7345] | 246 | } |
---|
| 247 | |
---|
[c0bf94e] | 248 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), isGenerated( other.isGenerated ) { |
---|
[51b7345] | 249 | } |
---|
| 250 | |
---|
[50377a4] | 251 | void CastExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 252 | os << "Cast of:" << std::endl << indent+1; |
---|
| 253 | arg->print(os, indent+1); |
---|
| 254 | os << std::endl << indent << "... to:"; |
---|
[906e24d] | 255 | if ( result->isVoid() ) { |
---|
[50377a4] | 256 | os << " nothing"; |
---|
[0dd3a2f] | 257 | } else { |
---|
[50377a4] | 258 | os << std::endl << indent+1; |
---|
| 259 | result->print( os, indent+1 ); |
---|
[0dd3a2f] | 260 | } // if |
---|
| 261 | Expression::print( os, indent ); |
---|
| 262 | } |
---|
| 263 | |
---|
[9a705dc8] | 264 | KeywordCastExpr::KeywordCastExpr( Expression *arg, Target target ) : Expression(), arg(arg), target( target ) { |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) { |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | const std::string & KeywordCastExpr::targetString() const { |
---|
| 271 | static const std::string targetStrs[] = { |
---|
| 272 | "coroutine", "thread", "monitor" |
---|
| 273 | }; |
---|
| 274 | static_assert( |
---|
| 275 | (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS), |
---|
| 276 | "Each KeywordCastExpr::Target should have a corresponding string representation" |
---|
| 277 | ); |
---|
| 278 | return targetStrs[(unsigned long)target]; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | void KeywordCastExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 282 | os << "Keyword Cast of:" << std::endl << indent+1; |
---|
| 283 | arg->print(os, indent+1); |
---|
| 284 | os << std::endl << indent << "... to: "; |
---|
| 285 | os << targetString(); |
---|
| 286 | Expression::print( os, indent ); |
---|
[0dd3a2f] | 287 | } |
---|
| 288 | |
---|
[a5f0529] | 289 | VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { |
---|
| 290 | set_result(toType); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
| 294 | } |
---|
| 295 | |
---|
[50377a4] | 296 | void VirtualCastExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 297 | os << "Virtual Cast of:" << std::endl << indent+1; |
---|
| 298 | arg->print(os, indent+1); |
---|
| 299 | os << std::endl << indent << "... to:"; |
---|
[a5f0529] | 300 | if ( ! result ) { |
---|
[50377a4] | 301 | os << " unknown"; |
---|
[a5f0529] | 302 | } else { |
---|
[50377a4] | 303 | os << std::endl << indent+1; |
---|
| 304 | result->print( os, indent+1 ); |
---|
[a5f0529] | 305 | } // if |
---|
| 306 | Expression::print( os, indent ); |
---|
| 307 | } |
---|
| 308 | |
---|
[bf4b4cf] | 309 | UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression *aggregate ) : |
---|
| 310 | Expression(), member(member), aggregate(aggregate) { |
---|
[50377a4] | 311 | assert( aggregate ); |
---|
| 312 | } |
---|
[51b7345] | 313 | |
---|
[0dd3a2f] | 314 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : |
---|
[3b58d91] | 315 | Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { |
---|
[51b7345] | 316 | } |
---|
| 317 | |
---|
[50377a4] | 318 | void UntypedMemberExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 319 | os << "Untyped Member Expression, with field: " << std::endl << indent+1; |
---|
| 320 | member->print(os, indent+1 ); |
---|
| 321 | os << indent << "... from aggregate: " << std::endl << indent+1; |
---|
| 322 | aggregate->print(os, indent+1); |
---|
[0dd3a2f] | 323 | Expression::print( os, indent ); |
---|
[51b7345] | 324 | } |
---|
| 325 | |
---|
[bf4b4cf] | 326 | MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) : |
---|
| 327 | Expression(), member(member), aggregate(aggregate) { |
---|
[50377a4] | 328 | assert( member ); |
---|
| 329 | assert( aggregate ); |
---|
[9bfc9da] | 330 | assert( aggregate->result ); |
---|
[d9fa60a] | 331 | |
---|
[9bfc9da] | 332 | TypeSubstitution sub = aggregate->result->genericSubstitution(); |
---|
[d9fa60a] | 333 | Type * res = member->get_type()->clone(); |
---|
| 334 | sub.apply( res ); |
---|
[487845d7] | 335 | result = res; |
---|
| 336 | result->set_lvalue( true ); |
---|
| 337 | result->get_qualifiers() |= aggregate->result->get_qualifiers(); |
---|
[51b7345] | 338 | } |
---|
| 339 | |
---|
[0dd3a2f] | 340 | MemberExpr::MemberExpr( const MemberExpr &other ) : |
---|
[39f84a4] | 341 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { |
---|
[51b7345] | 342 | } |
---|
| 343 | |
---|
[50377a4] | 344 | void MemberExpr::print( std::ostream &os, Indenter indent ) const { |
---|
[89231bc] | 345 | os << "Member Expression, with field: " << std::endl; |
---|
[50377a4] | 346 | os << indent+1; |
---|
| 347 | member->print( os, indent+1 ); |
---|
| 348 | os << std::endl << indent << "... from aggregate: " << std::endl << indent+1; |
---|
| 349 | aggregate->print(os, indent + 1); |
---|
[0dd3a2f] | 350 | Expression::print( os, indent ); |
---|
[51b7345] | 351 | } |
---|
| 352 | |
---|
[bf4b4cf] | 353 | UntypedExpr::UntypedExpr( Expression *function, const std::list<Expression *> &args ) : |
---|
| 354 | Expression(), function(function), args(args) {} |
---|
[51b7345] | 355 | |
---|
[0dd3a2f] | 356 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) : |
---|
| 357 | Expression( other ), function( maybeClone( other.function ) ) { |
---|
| 358 | cloneAll( other.args, args ); |
---|
[51b7345] | 359 | } |
---|
| 360 | |
---|
[b3b2077] | 361 | UntypedExpr * UntypedExpr::createDeref( Expression * expr ) { |
---|
| 362 | UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } ); |
---|
| 363 | if ( Type * type = expr->get_result() ) { |
---|
| 364 | Type * base = InitTweak::getPointerBase( type ); |
---|
[0698aa1] | 365 | assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() ); |
---|
[b0440b7] | 366 | ret->set_result( base->clone() ); |
---|
| 367 | if ( GenPoly::referencesPermissable() ) { |
---|
| 368 | // if references are still allowed in the AST, dereference returns a reference |
---|
| 369 | ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) ); |
---|
| 370 | } else { |
---|
| 371 | // references have been removed, in which case dereference returns an lvalue of the base type. |
---|
[0690350] | 372 | ret->result->set_lvalue( true ); |
---|
[b0440b7] | 373 | } |
---|
[b3b2077] | 374 | } |
---|
| 375 | return ret; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) { |
---|
| 379 | assert( arg1 && arg2 ); |
---|
| 380 | UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } ); |
---|
| 381 | if ( arg1->get_result() && arg2->get_result() ) { |
---|
| 382 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment, |
---|
| 383 | // so the result is the type of the RHS |
---|
| 384 | ret->set_result( arg2->get_result()->clone() ); |
---|
| 385 | } |
---|
| 386 | return ret; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | |
---|
[50377a4] | 390 | void UntypedExpr::print( std::ostream &os, Indenter indent ) const { |
---|
[89231bc] | 391 | os << "Applying untyped: " << std::endl; |
---|
[50377a4] | 392 | os << indent+1; |
---|
| 393 | function->print(os, indent+1); |
---|
| 394 | os << std::endl << indent << "...to: " << std::endl; |
---|
| 395 | printAll(args, os, indent+1); |
---|
[0dd3a2f] | 396 | Expression::print( os, indent ); |
---|
[51b7345] | 397 | } |
---|
| 398 | |
---|
[bf4b4cf] | 399 | NameExpr::NameExpr( std::string name ) : Expression(), name(name) { |
---|
[50377a4] | 400 | assertf(name != "0", "Zero is not a valid name"); |
---|
| 401 | assertf(name != "1", "One is not a valid name"); |
---|
[4cb935e] | 402 | } |
---|
[51b7345] | 403 | |
---|
[0dd3a2f] | 404 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { |
---|
[51b7345] | 405 | } |
---|
| 406 | |
---|
[50377a4] | 407 | void NameExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 408 | os << "Name: " << get_name(); |
---|
[0dd3a2f] | 409 | Expression::print( os, indent ); |
---|
[51b7345] | 410 | } |
---|
| 411 | |
---|
[bf4b4cf] | 412 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp ) : |
---|
| 413 | Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) { |
---|
[906e24d] | 414 | set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
[51b7345] | 415 | } |
---|
| 416 | |
---|
[0dd3a2f] | 417 | LogicalExpr::LogicalExpr( const LogicalExpr &other ) : |
---|
| 418 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { |
---|
[51b7345] | 419 | } |
---|
| 420 | |
---|
[50377a4] | 421 | void LogicalExpr::print( std::ostream &os, Indenter indent )const { |
---|
| 422 | os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: "; |
---|
[0dd3a2f] | 423 | arg1->print(os); |
---|
| 424 | os << " and "; |
---|
| 425 | arg2->print(os); |
---|
| 426 | Expression::print( os, indent ); |
---|
[51b7345] | 427 | } |
---|
| 428 | |
---|
[bf4b4cf] | 429 | ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) : |
---|
| 430 | Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {} |
---|
[51b7345] | 431 | |
---|
[0dd3a2f] | 432 | ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : |
---|
| 433 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { |
---|
| 434 | } |
---|
[51b7345] | 435 | |
---|
[50377a4] | 436 | void ConditionalExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 437 | os << "Conditional expression on: " << std::endl << indent+1; |
---|
| 438 | arg1->print( os, indent+1 ); |
---|
| 439 | os << indent << "First alternative:" << std::endl << indent+1; |
---|
| 440 | arg2->print( os, indent+1 ); |
---|
| 441 | os << indent << "Second alternative:" << std::endl << indent+1; |
---|
| 442 | arg3->print( os, indent+1 ); |
---|
[0dd3a2f] | 443 | Expression::print( os, indent ); |
---|
| 444 | } |
---|
| 445 | |
---|
[e6cee92] | 446 | AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {} |
---|
[3be261a] | 447 | |
---|
| 448 | |
---|
[50377a4] | 449 | void AsmExpr::print( std::ostream &os, Indenter indent ) const { |
---|
[7f5566b] | 450 | os << "Asm Expression: " << std::endl; |
---|
[50377a4] | 451 | if ( inout ) inout->print( os, indent+1 ); |
---|
| 452 | if ( constraint ) constraint->print( os, indent+1 ); |
---|
| 453 | if ( operand ) operand->print( os, indent+1 ); |
---|
[7f5566b] | 454 | } |
---|
| 455 | |
---|
[db4ecc5] | 456 | |
---|
| 457 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) { |
---|
| 458 | assert( callExpr ); |
---|
[50377a4] | 459 | assert( callExpr->result ); |
---|
[906e24d] | 460 | set_result( callExpr->get_result()->clone() ); |
---|
[db4ecc5] | 461 | } |
---|
| 462 | |
---|
| 463 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
| 464 | cloneAll( other.tempDecls, tempDecls ); |
---|
| 465 | cloneAll( other.returnDecls, returnDecls ); |
---|
| 466 | cloneAll( other.dtors, dtors ); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() { |
---|
[d5556a3] | 470 | set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment |
---|
[db4ecc5] | 471 | } |
---|
| 472 | |
---|
[50377a4] | 473 | void ImplicitCopyCtorExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 474 | os << "Implicit Copy Constructor Expression: " << std::endl << indent+1; |
---|
| 475 | callExpr->print( os, indent+1 ); |
---|
| 476 | os << std::endl << indent << "... with temporaries:" << std::endl; |
---|
| 477 | printAll( tempDecls, os, indent+1 ); |
---|
| 478 | os << std::endl << indent << "... with return temporaries:" << std::endl; |
---|
| 479 | printAll( returnDecls, os, indent+1 ); |
---|
[db4ecc5] | 480 | Expression::print( os, indent ); |
---|
| 481 | } |
---|
| 482 | |
---|
[3be261a] | 483 | |
---|
[b6fe7e6] | 484 | ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) { |
---|
| 485 | // allow resolver to type a constructor used as an expression as if it has the same type as its first argument |
---|
| 486 | assert( callExpr ); |
---|
| 487 | Expression * arg = InitTweak::getCallArg( callExpr, 0 ); |
---|
| 488 | assert( arg ); |
---|
[50377a4] | 489 | set_result( maybeClone( arg->result ) ); |
---|
[b6fe7e6] | 490 | } |
---|
[3be261a] | 491 | |
---|
[b6fe7e6] | 492 | ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
| 493 | } |
---|
| 494 | |
---|
[50377a4] | 495 | void ConstructorExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 496 | os << "Constructor Expression: " << std::endl << indent+1; |
---|
[b6fe7e6] | 497 | callExpr->print( os, indent + 2 ); |
---|
| 498 | Expression::print( os, indent ); |
---|
[0dd3a2f] | 499 | } |
---|
| 500 | |
---|
[3be261a] | 501 | |
---|
[fbcde64] | 502 | CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) { |
---|
[3c13c03] | 503 | assert( type && initializer ); |
---|
[5ccb10d] | 504 | type->set_lvalue( true ); |
---|
[fbcde64] | 505 | set_result( type ); |
---|
[630a82a] | 506 | } |
---|
| 507 | |
---|
[fbcde64] | 508 | CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {} |
---|
[630a82a] | 509 | |
---|
[50377a4] | 510 | void CompoundLiteralExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 511 | os << "Compound Literal Expression: " << std::endl << indent+1; |
---|
| 512 | result->print( os, indent+1 ); |
---|
| 513 | os << indent+1; |
---|
| 514 | initializer->print( os, indent+1 ); |
---|
[d5556a3] | 515 | Expression::print( os, indent ); |
---|
[630a82a] | 516 | } |
---|
| 517 | |
---|
[d9e2280] | 518 | RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {} |
---|
[3c13c03] | 519 | RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {} |
---|
[50377a4] | 520 | void RangeExpr::print( std::ostream &os, Indenter indent ) const { |
---|
[3c13c03] | 521 | os << "Range Expression: "; |
---|
[8688ce1] | 522 | low->print( os, indent ); |
---|
| 523 | os << " ... "; |
---|
| 524 | high->print( os, indent ); |
---|
[d5556a3] | 525 | Expression::print( os, indent ); |
---|
[8688ce1] | 526 | } |
---|
[3be261a] | 527 | |
---|
[6eb8948] | 528 | StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) { |
---|
[5e2c348] | 529 | computeResult(); |
---|
| 530 | } |
---|
| 531 | StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) { |
---|
| 532 | cloneAll( other.returnDecls, returnDecls ); |
---|
| 533 | cloneAll( other.dtors, dtors ); |
---|
| 534 | } |
---|
| 535 | void StmtExpr::computeResult() { |
---|
[6eb8948] | 536 | assert( statements ); |
---|
[5e2c348] | 537 | std::list< Statement * > & body = statements->kids; |
---|
| 538 | result = nullptr; |
---|
[0992849] | 539 | if ( ! returnDecls.empty() ) { |
---|
| 540 | // prioritize return decl for result type, since if a return decl exists, then |
---|
| 541 | // the StmtExpr is currently in an intermediate state where the body will always |
---|
| 542 | // give a void result type. |
---|
| 543 | result = returnDecls.front()->get_type()->clone(); |
---|
| 544 | } else if ( ! body.empty() ) { |
---|
[6eb8948] | 545 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) { |
---|
[9fe33947] | 546 | result = maybeClone( exprStmt->expr->result ); |
---|
[6eb8948] | 547 | } |
---|
| 548 | } |
---|
[07516b56] | 549 | // ensure that StmtExpr has a result type |
---|
| 550 | if ( ! result ) { |
---|
[9fe33947] | 551 | result = new VoidType( Type::Qualifiers() ); |
---|
[07516b56] | 552 | } |
---|
[6eb8948] | 553 | } |
---|
[50377a4] | 554 | void StmtExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 555 | os << "Statement Expression: " << std::endl << indent+1; |
---|
| 556 | statements->print( os, indent+1 ); |
---|
[d5556a3] | 557 | if ( ! returnDecls.empty() ) { |
---|
[50377a4] | 558 | os << indent+1 << "... with returnDecls: "; |
---|
| 559 | printAll( returnDecls, os, indent+1 ); |
---|
[d5556a3] | 560 | } |
---|
| 561 | if ( ! dtors.empty() ) { |
---|
[50377a4] | 562 | os << indent+1 << "... with dtors: "; |
---|
| 563 | printAll( dtors, os, indent+1 ); |
---|
[d5556a3] | 564 | } |
---|
| 565 | Expression::print( os, indent ); |
---|
[6eb8948] | 566 | } |
---|
| 567 | |
---|
[3c13c03] | 568 | |
---|
[bf32bb8] | 569 | long long UniqueExpr::count = 0; |
---|
[141b786] | 570 | UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) { |
---|
| 571 | assert( expr ); |
---|
[bf32bb8] | 572 | assert( count != -1 ); |
---|
| 573 | if ( id == -1 ) id = count++; |
---|
| 574 | if ( expr->get_result() ) { |
---|
| 575 | set_result( expr->get_result()->clone() ); |
---|
| 576 | } |
---|
[3c13c03] | 577 | } |
---|
[141b786] | 578 | UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) { |
---|
[3c13c03] | 579 | } |
---|
[68f9c43] | 580 | |
---|
[50377a4] | 581 | void UniqueExpr::print( std::ostream &os, Indenter indent ) const { |
---|
| 582 | os << "Unique Expression with id:" << id << std::endl << indent+1; |
---|
| 583 | expr->print( os, indent+1 ); |
---|
| 584 | if ( object ) { |
---|
| 585 | os << indent << "... with decl: "; |
---|
| 586 | get_object()->printShort( os, indent+1 ); |
---|
[77971f6] | 587 | } |
---|
[d5556a3] | 588 | Expression::print( os, indent ); |
---|
[3c13c03] | 589 | } |
---|
| 590 | |
---|
[e4d829b] | 591 | InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {} |
---|
| 592 | InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {} |
---|
| 593 | |
---|
| 594 | UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {} |
---|
| 595 | UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {} |
---|
| 596 | |
---|
[50377a4] | 597 | void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const { |
---|
| 598 | os << "Untyped Init Expression" << std::endl << indent+1; |
---|
| 599 | expr->print( os, indent+1 ); |
---|
[e4d829b] | 600 | if ( ! initAlts.empty() ) { |
---|
| 601 | for ( const InitAlternative & alt : initAlts ) { |
---|
[50377a4] | 602 | os << indent+1 << "InitAlternative: "; |
---|
| 603 | alt.type->print( os, indent+1 ); |
---|
| 604 | alt.designation->print( os, indent+1 ); |
---|
[e4d829b] | 605 | } |
---|
| 606 | } |
---|
| 607 | } |
---|
| 608 | |
---|
[62423350] | 609 | InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) { |
---|
| 610 | set_result( expr->get_result()->clone() ); |
---|
[e4d829b] | 611 | } |
---|
| 612 | InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {} |
---|
| 613 | |
---|
[50377a4] | 614 | void InitExpr::print( std::ostream & os, Indenter indent ) const { |
---|
| 615 | os << "Init Expression" << std::endl << indent+1; |
---|
| 616 | expr->print( os, indent+1 ); |
---|
| 617 | os << indent+1 << "... with designation: "; |
---|
| 618 | designation->print( os, indent+1 ); |
---|
[e4d829b] | 619 | } |
---|
| 620 | |
---|
[44b4114] | 621 | DeletedExpr::DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) { |
---|
| 622 | assert( expr->result ); |
---|
| 623 | result = expr->result->clone(); |
---|
| 624 | } |
---|
| 625 | DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {} |
---|
| 626 | |
---|
| 627 | void DeletedExpr::print( std::ostream & os, Indenter indent ) const { |
---|
| 628 | os << "Deleted Expression" << std::endl << indent+1; |
---|
| 629 | expr->print( os, indent+1 ); |
---|
| 630 | os << std::endl << indent+1 << "... deleted by: "; |
---|
| 631 | deleteStmt->print( os, indent+1 ); |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | |
---|
[0dd3a2f] | 635 | // Local Variables: // |
---|
| 636 | // tab-width: 4 // |
---|
| 637 | // mode: c++ // |
---|
| 638 | // compile-command: "make install" // |
---|
| 639 | // End: // |
---|