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