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