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