Changes in src/AST/Expr.cpp [312029a:d5631b3]
- File:
-
- 1 edited
-
src/AST/Expr.cpp (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Expr.cpp
r312029a rd5631b3 20 20 #include <vector> 21 21 22 #include "Copy.hpp" // for shallowCopy 23 #include "Eval.hpp" // for call 22 24 #include "GenericSubstitution.hpp" 25 #include "LinkageSpec.hpp" 23 26 #include "Stmt.hpp" 24 27 #include "Type.hpp" … … 27 30 #include "Common/SemanticError.h" 28 31 #include "GenPoly/Lvalue.h" // for referencesPermissable 29 #include "InitTweak/InitTweak.h" // for get PointerBase32 #include "InitTweak/InitTweak.h" // for getFunction, getPointerBase 30 33 #include "ResolvExpr/typeops.h" // for extractResultType 31 34 #include "Tuples/Tuples.h" // for makeTupleType 32 35 33 36 namespace ast { 37 38 namespace { 39 std::set<std::string> const lvalueFunctionNames = {"*?", "?[?]"}; 40 } 41 42 // --- Expr 43 bool Expr::get_lvalue() const { 44 return false; 45 } 34 46 35 47 // --- ApplicationExpr … … 46 58 } 47 59 60 bool ApplicationExpr::get_lvalue() const { 61 if ( const DeclWithType * func = InitTweak::getFunction( this ) ) { 62 return func->linkage == Linkage::Intrinsic && lvalueFunctionNames.count( func->name ); 63 } 64 return false; 65 } 66 48 67 // --- UntypedExpr 49 68 … … 51 70 assert( arg ); 52 71 53 UntypedExpr * ret = new UntypedExpr{ 54 loc, new NameExpr{loc, "*?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ arg } } 55 }; 72 UntypedExpr * ret = call( loc, "*?", arg ); 56 73 if ( const Type * ty = arg->result ) { 57 74 const Type * base = InitTweak::getPointerBase( ty ); … … 65 82 // base type 66 83 ret->result = base; 67 add_qualifiers( ret->result, CV::Lvalue );68 84 } 69 85 } 70 86 return ret; 87 } 88 89 bool UntypedExpr::get_lvalue() const { 90 std::string fname = InitTweak::getFunctionName( this ); 91 return lvalueFunctionNames.count( fname ); 71 92 } 72 93 … … 74 95 assert( lhs && rhs ); 75 96 76 UntypedExpr * ret = new UntypedExpr{ 77 loc, new NameExpr{loc, "?=?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ lhs }, ptr<Expr>{ rhs } } 78 }; 97 UntypedExpr * ret = call( loc, "?=?", lhs, rhs ); 79 98 if ( lhs->result && rhs->result ) { 80 99 // if both expressions are typed, assumes that this assignment is a C bitwise assignment, … … 83 102 } 84 103 return ret; 104 } 105 106 // --- VariableExpr 107 108 VariableExpr::VariableExpr( const CodeLocation & loc ) 109 : Expr( loc ), var( nullptr ) {} 110 111 VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v ) 112 : Expr( loc ), var( v ) { 113 assert( var ); 114 assert( var->get_type() ); 115 result = shallowCopy( var->get_type() ); 116 } 117 118 bool VariableExpr::get_lvalue() const { 119 // It isn't always an lvalue, but it is never an rvalue. 120 return true; 121 } 122 123 VariableExpr * VariableExpr::functionPointer( 124 const CodeLocation & loc, const FunctionDecl * decl ) { 125 // wrap usually-determined result type in a pointer 126 VariableExpr * funcExpr = new VariableExpr{ loc, decl }; 127 funcExpr->result = new PointerType{ funcExpr->result }; 128 return funcExpr; 85 129 } 86 130 … … 108 152 AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) { 109 153 if ( arg->result ) { 110 if ( arg-> result->is_lvalue() ) {154 if ( arg->get_lvalue() ) { 111 155 // lvalue, retains all levels of reference, and gains a pointer inside the references 112 156 Type * res = addrType( arg->result ); 113 res->set_lvalue( false ); // result of & is never an lvalue114 157 result = res; 115 158 } else { … … 118 161 dynamic_cast< const ReferenceType * >( arg->result.get() ) ) { 119 162 Type * res = addrType( refType->base ); 120 res->set_lvalue( false ); // result of & is never an lvalue121 163 result = res; 122 164 } else { … … 139 181 : Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {} 140 182 183 bool CastExpr::get_lvalue() const { 184 // This is actually wrong by C, but it works with our current set-up. 185 return arg->get_lvalue(); 186 } 187 141 188 // --- KeywordCastExpr 142 189 143 190 const char * KeywordCastExpr::targetString() const { 144 191 return AggregateDecl::aggrString( target ); 192 } 193 194 // --- UntypedMemberExpr 195 196 bool UntypedMemberExpr::get_lvalue() const { 197 return aggregate->get_lvalue(); 145 198 } 146 199 … … 153 206 assert( aggregate->result ); 154 207 155 // take ownership of member type 156 result = mem->get_type(); 208 // Deep copy on result type avoids mutation on transitively multiply referenced object. 209 // 210 // Example, adapted from parts of builtins and bootloader: 211 // 212 // forall(dtype T) 213 // struct __Destructor { 214 // T * object; 215 // void (*dtor)(T *); 216 // }; 217 // 218 // forall(dtype S) 219 // void foo(__Destructor(S) &d) { 220 // if (d.dtor) { // here 221 // } 222 // } 223 // 224 // Let e be the "d.dtor" guard espression, which is MemberExpr after resolve. Let d be the 225 // declaration of member __Destructor.dtor (an ObjectDecl), as accessed via the top-level 226 // declaration of __Destructor. Consider the types e.result and d.type. In the old AST, one 227 // is a clone of the other. Ordinary new-AST use would set them up as a multiply-referenced 228 // object. 229 // 230 // e.result: PointerType 231 // .base: FunctionType 232 // .params.front(): ObjectDecl, the anonymous parameter of type T* 233 // .type: PointerType 234 // .base: TypeInstType 235 // let x = that 236 // let y = similar, except start from d.type 237 // 238 // Consider two code lines down, genericSubstitution(...).apply(result). 239 // 240 // Applying this chosen-candidate's type substitution means modifying x, substituting 241 // S for T. This mutation should affect x and not y. 242 243 result = deepCopy(mem->get_type()); 244 157 245 // substitute aggregate generic parameters into member type 158 246 genericSubstitution( aggregate->result ).apply( result ); 159 // ensure lvalue and appropriate restrictions from aggregate type 160 add_qualifiers( result, aggregate->result->qualifiers | CV::Lvalue ); 161 } 162 163 // --- VariableExpr 164 165 VariableExpr::VariableExpr( const CodeLocation & loc ) 166 : Expr( loc ), var( nullptr ) {} 167 168 VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v ) 169 : Expr( loc ), var( v ) { 170 assert( var ); 171 assert( var->get_type() ); 172 result = var->get_type(); 173 add_qualifiers( result, CV::Lvalue ); 174 } 175 176 VariableExpr * VariableExpr::functionPointer( 177 const CodeLocation & loc, const FunctionDecl * decl ) { 178 // wrap usually-determined result type in a pointer 179 VariableExpr * funcExpr = new VariableExpr{ loc, decl }; 180 funcExpr->result = new PointerType{ funcExpr->result }; 181 return funcExpr; 247 // ensure appropriate restrictions from aggregate type 248 add_qualifiers( result, aggregate->result->qualifiers ); 249 } 250 251 MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg, 252 MemberExpr::NoOpConstruction overloadSelector ) 253 : Expr( loc ), member( mem ), aggregate( agg ) { 254 assert( member ); 255 assert( aggregate ); 256 assert( aggregate->result ); 257 (void) overloadSelector; 258 } 259 260 bool MemberExpr::get_lvalue() const { 261 // This is actually wrong by C, but it works with our current set-up. 262 return true; 182 263 } 183 264 … … 258 339 : Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {} 259 340 341 // --- CommaExpr 342 bool CommaExpr::get_lvalue() const { 343 // This is wrong by C, but the current implementation uses it. 344 // (ex: Specialize, Lvalue and Box) 345 return arg2->get_lvalue(); 346 } 347 260 348 // --- ConstructorExpr 261 349 … … 276 364 assert( t && i ); 277 365 result = t; 278 add_qualifiers( result, CV::Lvalue ); 366 } 367 368 bool CompoundLiteralExpr::get_lvalue() const { 369 return true; 279 370 } 280 371 … … 293 384 // like MemberExpr, TupleIndexExpr is always an lvalue 294 385 result = type->types[ index ]; 295 add_qualifiers( result, CV::Lvalue ); 386 } 387 388 bool TupleIndexExpr::get_lvalue() const { 389 return tuple->get_lvalue(); 296 390 } 297 391
Note:
See TracChangeset
for help on using the changeset viewer.