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