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