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