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