source: src/AST/Expr.hpp@ a6f26ca

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since a6f26ca was ae265b55, checked in by Michael Brooks <mlbrooks@…>, 6 years ago

Two fixes of MemberExpr handling on new-AST.

Each bug on its own, and also both bugs together, produce the same symptom: Expressions that use function-pointer typed members of structs, where the function has a generically-typed argument, were lacking casts (vs old-AST) in the output of:

./driver/cfa-cpp --prelude-dir ./libcfa/x64-debug/prelude -tm ../cfa-cc/libcfa/prelude/bootloader.cf bootloader.c

First bug: a conversion inaccuracy, which is a regression of convert-convert-post-resolve, introduced at Jun 28 rev 55b647. The constructor of a MemberExpr, that Convert old-to-new uses, does type-variable substitution. This is intended behaviour when it occurs during resolve, but is incorrect when done twice. In absence of this fix, the old-to-new converter runs this substitution a second time, both in: oldresolve-old2new-new2old and old2new-newresolve-new2old. The fix is offering/using a no-op constructor to/at the converter.

Second bug: a mutation of a transitively-multiply-referenced object. The type-variable substitution just mentioned, which should be happening to the result type of the MemberExpr and not to the top-level struct-member declaration, actually happens as:

  • old ast: as should; all type-value intentions are clones
  • new ast pre this fix: incorrectly to both types; ref-counted sharing means theExpr.result == theDecl.type, substitution happens on sub-object (see details in code comment)
  • new ast with this fix: as should; deep copy of the type is explicitly forced upfront

All bootloader output differences (new-AST's resolver vs old-AST's resolver) that remain after this fix are: four constructor-destructor calls in builtin function bodies, and a missing copy-constructor call on invoke-main. These differences were present ahead of this change.

This change has been tested for a clean convert-convert difference (vs no new-AST at all), for convert-convert at: pre-resolve, post-resolve, post-parse. In the case of cvcv post-resolve, this clean difference is being restored from the first-bug regression.

This change has been tested for, and causes a small-to-questionable regression of, cfa-cpp speed of compiling bootloader. Old AST = 6.04 s, New AST before this change = 4.64 s, New ast after this change 4.76 s. All numbers are mean of 5 samples with sample SD ~= 0.3 sec. New-AST improvement before this fix = 23% = 4.2 SDs. New-AST improvement after this fix = 21% = 3.9 SDs.

  • Property mode set to 100644
File size: 26.3 KB
Line 
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// Expr.hpp --
8//
9// Author : Aaron B. Moss
10// Created On : Fri May 10 10:30:00 2019
11// Last Modified By : Aaron B. Moss
12// Created On : Fri May 10 10:30:00 2019
13// Update Count : 1
14//
15
16#pragma once
17
18#include <cassert>
19#include <deque>
20#include <map>
21#include <string>
22#include <utility> // for move
23#include <vector>
24#include <optional>
25
26#include "Fwd.hpp" // for UniqueId
27#include "Label.hpp"
28#include "ParseNode.hpp"
29#include "Visitor.hpp"
30
31// Must be included in *all* AST classes; should be #undef'd at the end of the file
32#define MUTATE_FRIEND \
33 template<typename node_t> friend node_t * mutate(const node_t * node); \
34 template<typename node_t> friend node_t * shallowCopy(const node_t * node);
35
36
37class ConverterOldToNew;
38class ConverterNewToOld;
39
40namespace ast {
41
42/// Contains the ID of a declaration and a type that is derived from that declaration,
43/// but subject to decay-to-pointer and type parameter renaming
44struct ParamEntry {
45 UniqueId decl;
46 ptr<Decl> declptr;
47 ptr<Type> actualType;
48 ptr<Type> formalType;
49 ptr<Expr> expr;
50
51 ParamEntry() : decl( 0 ), declptr( nullptr ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {}
52 ParamEntry(
53 UniqueId id, const Decl * declptr, const Type * actual, const Type * formal,
54 const Expr * e )
55 : decl( id ), declptr( declptr ), actualType( actual ), formalType( formal ), expr( e ) {}
56};
57
58/// Pre-resolution list of parameters to infer
59using ResnSlots = std::vector<UniqueId>;
60/// Post-resolution map of inferred parameters
61using InferredParams = std::map< UniqueId, ParamEntry >;
62
63/// Base node for expressions
64class Expr : public ParseNode {
65public:
66 /// Saves space (~16 bytes) by combining ResnSlots and InferredParams
67 struct InferUnion {
68 enum { Empty, Slots, Params } mode;
69 union data_t {
70 char def;
71 ResnSlots resnSlots;
72 InferredParams inferParams;
73
74 data_t() : def('\0') {}
75 ~data_t() {}
76 } data;
77
78 /// initializes from other InferUnion
79 void init_from( const InferUnion& o ) {
80 switch ( o.mode ) {
81 case Empty: return;
82 case Slots: new(&data.resnSlots) ResnSlots{ o.data.resnSlots }; return;
83 case Params: new(&data.inferParams) InferredParams{ o.data.inferParams }; return;
84 }
85 }
86
87 /// initializes from other InferUnion (move semantics)
88 void init_from( InferUnion&& o ) {
89 switch ( o.mode ) {
90 case Empty: return;
91 case Slots: new(&data.resnSlots) ResnSlots{ std::move(o.data.resnSlots) }; return;
92 case Params:
93 new(&data.inferParams) InferredParams{ std::move(o.data.inferParams) }; return;
94 }
95 }
96
97 /// clears variant fields
98 void reset() {
99 switch( mode ) {
100 case Empty: return;
101 case Slots: data.resnSlots.~ResnSlots(); return;
102 case Params: data.inferParams.~InferredParams(); return;
103 }
104 }
105
106 InferUnion() : mode(Empty), data() {}
107 InferUnion( const InferUnion& o ) : mode( o.mode ), data() { init_from( o ); }
108 InferUnion( InferUnion&& o ) : mode( o.mode ), data() { init_from( std::move(o) ); }
109 InferUnion& operator= ( const InferUnion& ) = delete;
110 InferUnion& operator= ( InferUnion&& ) = delete;
111 ~InferUnion() { reset(); }
112
113 ResnSlots& resnSlots() {
114 switch (mode) {
115 case Empty: new(&data.resnSlots) ResnSlots{}; mode = Slots; // fallthrough
116 case Slots: return data.resnSlots;
117 case Params: assertf(false, "Cannot return to resnSlots from Params"); abort();
118 }
119 assertf(false, "unreachable");
120 }
121
122 const ResnSlots& resnSlots() const {
123 if (mode == Slots) {
124 return data.resnSlots;
125 }
126 assertf(false, "Mode was not already resnSlots");
127 abort();
128 }
129
130 InferredParams& inferParams() {
131 switch (mode) {
132 case Slots: data.resnSlots.~ResnSlots(); // fallthrough
133 case Empty: new(&data.inferParams) InferredParams{}; mode = Params; // fallthrough
134 case Params: return data.inferParams;
135 }
136 assertf(false, "unreachable");
137 }
138
139 const InferredParams& inferParams() const {
140 if (mode == Params) {
141 return data.inferParams;
142 }
143 assertf(false, "Mode was not already Params");
144 abort();
145 }
146
147 void set_inferParams( InferredParams && ps ) {
148 switch(mode) {
149 case Slots:
150 data.resnSlots.~ResnSlots();
151 // fallthrough
152 case Empty:
153 new(&data.inferParams) InferredParams{ std::move( ps ) };
154 mode = Params;
155 break;
156 case Params:
157 data.inferParams = std::move( ps );
158 break;
159 }
160 }
161
162 /// splices other InferUnion into this one. Will fail if one union is in `Slots` mode
163 /// and the other is in `Params`.
164 void splice( InferUnion && o ) {
165 if ( o.mode == Empty ) return;
166 if ( mode == Empty ) { init_from( o ); return; }
167 assert( mode == o.mode && "attempt to splice incompatible InferUnion" );
168
169 if ( mode == Slots ){
170 data.resnSlots.insert(
171 data.resnSlots.end(), o.data.resnSlots.begin(), o.data.resnSlots.end() );
172 } else if ( mode == Params ) {
173 for ( const auto & p : o.data.inferParams ) {
174 data.inferParams[p.first] = std::move(p.second);
175 }
176 } else assertf(false, "invalid mode");
177 }
178 };
179
180 ptr<Type> result;
181 ptr<TypeSubstitution> env;
182 InferUnion inferred;
183 bool extension = false;
184
185 Expr( const CodeLocation & loc, const Type * res = nullptr )
186 : ParseNode( loc ), result( res ), env(), inferred() {}
187
188 Expr * set_extension( bool ex ) { extension = ex; return this; }
189
190 virtual const Expr * accept( Visitor & v ) const override = 0;
191private:
192 Expr * clone() const override = 0;
193 MUTATE_FRIEND
194};
195
196/// The application of a function to a set of parameters.
197/// Post-resolver form of `UntypedExpr`
198class ApplicationExpr final : public Expr {
199public:
200 ptr<Expr> func;
201 std::vector<ptr<Expr>> args;
202
203 ApplicationExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} );
204
205 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
206private:
207 ApplicationExpr * clone() const override { return new ApplicationExpr{ *this }; }
208 MUTATE_FRIEND
209};
210
211/// The application of a function to a set of parameters, pre-overload resolution.
212class UntypedExpr final : public Expr {
213public:
214 ptr<Expr> func;
215 std::vector<ptr<Expr>> args;
216
217 UntypedExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} )
218 : Expr( loc ), func( f ), args( std::move(as) ) {}
219
220 /// Creates a new dereference expression
221 static UntypedExpr * createDeref( const CodeLocation & loc, Expr * arg );
222 /// Creates a new assignment expression
223 static UntypedExpr * createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs );
224
225 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
226private:
227 UntypedExpr * clone() const override { return new UntypedExpr{ *this }; }
228 MUTATE_FRIEND
229};
230
231/// A name whose name is as-yet undetermined.
232/// May also be used to avoid name mangling in codegen phase.
233class NameExpr final : public Expr {
234public:
235 std::string name;
236
237 NameExpr( const CodeLocation & loc, const std::string & n ) : Expr( loc ), name( n ) {}
238
239 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
240private:
241 NameExpr * clone() const override { return new NameExpr{ *this }; }
242 MUTATE_FRIEND
243};
244
245/// Address-of expression `&e`
246class AddressExpr final : public Expr {
247public:
248 ptr<Expr> arg;
249
250 AddressExpr( const CodeLocation & loc, const Expr * a );
251
252 /// Generate AddressExpr wrapping given expression at same location
253 AddressExpr( const Expr * a ) : AddressExpr( a->location, a ) {}
254
255 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
256private:
257 AddressExpr * clone() const override { return new AddressExpr{ *this }; }
258 MUTATE_FRIEND
259};
260
261/// GCC &&label
262/// https://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Labels-as-Values.html
263class LabelAddressExpr final : public Expr {
264public:
265 Label arg;
266
267 LabelAddressExpr( const CodeLocation & loc, Label && a );
268
269 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
270private:
271 LabelAddressExpr * clone() const override { return new LabelAddressExpr{ *this }; }
272 MUTATE_FRIEND
273};
274
275/// Whether a cast existed in the program source or not
276enum GeneratedFlag { ExplicitCast, GeneratedCast };
277
278/// A type cast, e.g. `(int)e`
279class CastExpr final : public Expr {
280public:
281 ptr<Expr> arg;
282 GeneratedFlag isGenerated;
283
284 CastExpr( const CodeLocation & loc, const Expr * a, const Type * to,
285 GeneratedFlag g = GeneratedCast ) : Expr( loc, to ), arg( a ), isGenerated( g ) {}
286 /// Cast-to-void
287 CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g = GeneratedCast );
288
289 /// Wrap a cast expression around an existing expression (always generated)
290 CastExpr( const Expr * a, const Type * to ) : CastExpr( a->location, a, to, GeneratedCast ) {}
291
292 /// Wrap a cast-to-void expression around an existing expression (always generated)
293 CastExpr( const Expr * a ) : CastExpr( a->location, a, GeneratedCast ) {}
294
295 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
296private:
297 CastExpr * clone() const override { return new CastExpr{ *this }; }
298 MUTATE_FRIEND
299};
300
301/// A cast to "keyword types", e.g. `(thread &)t`
302class KeywordCastExpr final : public Expr {
303public:
304 ptr<Expr> arg;
305 enum Target { Coroutine, Thread, Monitor, NUMBER_OF_TARGETS } target;
306
307 KeywordCastExpr( const CodeLocation & loc, const Expr * a, Target t )
308 : Expr( loc ), arg( a ), target( t ) {}
309
310 /// Get a name for the target type
311 const std::string& targetString() const;
312
313 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
314private:
315 KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; }
316 MUTATE_FRIEND
317};
318
319/// A virtual dynamic cast, e.g. `(virtual exception)e`
320class VirtualCastExpr final : public Expr {
321public:
322 ptr<Expr> arg;
323
324 VirtualCastExpr( const CodeLocation & loc, const Expr * a, const Type * to )
325 : Expr( loc, to ), arg( a ) {}
326
327 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
328private:
329 VirtualCastExpr * clone() const override { return new VirtualCastExpr{ *this }; }
330 MUTATE_FRIEND
331};
332
333/// A member selection operation before expression resolution, e.g. `q.p`
334class UntypedMemberExpr final : public Expr {
335public:
336 ptr<Expr> member;
337 ptr<Expr> aggregate;
338
339 UntypedMemberExpr( const CodeLocation & loc, const Expr * mem, const Expr * agg )
340 : Expr( loc ), member( mem ), aggregate( agg ) { assert( aggregate ); }
341
342 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
343private:
344 UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; }
345 MUTATE_FRIEND
346};
347
348/// A member selection operation after expression resolution, e.g. `q.p`
349class MemberExpr final : public Expr {
350public:
351 readonly<DeclWithType> member;
352 ptr<Expr> aggregate;
353
354 MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg );
355
356 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
357private:
358 MemberExpr * clone() const override { return new MemberExpr{ *this }; }
359 MUTATE_FRIEND
360
361 // Custructor overload meant only for AST conversion
362 enum NoOpConstruction { NoOpConstructionChosen };
363 MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg,
364 NoOpConstruction overloadSelector );
365 friend class ::ConverterOldToNew;
366 friend class ::ConverterNewToOld;
367};
368
369/// A reference to a named variable.
370class VariableExpr final : public Expr {
371public:
372 readonly<DeclWithType> var;
373
374 VariableExpr( const CodeLocation & loc );
375 VariableExpr( const CodeLocation & loc, const DeclWithType * v );
376
377 /// generates a function pointer for a given function
378 static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl );
379
380 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
381private:
382 VariableExpr * clone() const override { return new VariableExpr{ *this }; }
383 MUTATE_FRIEND
384};
385
386/// A compile-time constant.
387/// Mostly carries C-source text from parse to code-gen, without interpretation. E.g. strings keep their outer quotes and never have backslashes interpreted.
388/// Integer constants get special treatment, e.g. for verifying array operations, when an integer constant occurs as the length of an array.
389class ConstantExpr final : public Expr {
390public:
391 // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
392 std::string rep;
393
394 ConstantExpr(
395 const CodeLocation & loc, const Type * ty, const std::string & r,
396 std::optional<unsigned long long> i )
397 : Expr( loc, ty ), rep( r ), ival( i ) {}
398
399 /// Gets the integer value of this constant, if one is appropriate to its type.
400 /// Throws a SemanticError if the type is not appropriate for value-as-integer.
401 /// Suffers an assertion failure the type is appropriate but no integer value was supplied to the constructor.
402 long long int intValue() const;
403
404 /// generates a boolean constant of the given bool
405 static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
406 /// generates an integer constant of the given int
407 static ConstantExpr * from_int( const CodeLocation & loc, int i );
408 /// generates an integer constant of the given unsigned long int
409 static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
410 /// generates a null pointer value for the given type. void * if omitted.
411 static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
412
413 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
414private:
415 ConstantExpr * clone() const override { return new ConstantExpr{ *this }; }
416 MUTATE_FRIEND
417
418 std::optional<unsigned long long> ival;
419
420 // Intended only for legacy support of roundtripping the old AST.
421 // Captures the very-locally inferred type, before the resolver modifies the type of this ConstantExpression.
422 // In the old AST it's constExpr->constant.type
423 ptr<Type> underlyer;
424 friend class ::ConverterOldToNew;
425 friend class ::ConverterNewToOld;
426};
427
428/// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4`
429class SizeofExpr final : public Expr {
430public:
431 ptr<Expr> expr;
432 ptr<Type> type;
433
434 SizeofExpr( const CodeLocation & loc, const Expr * e );
435 SizeofExpr( const CodeLocation & loc, const Type * t );
436 // deliberately no disambiguating overload for nullptr_t
437
438 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
439private:
440 SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
441 MUTATE_FRIEND
442};
443
444/// alignof expression, e.g. `alignof(int)`, `alignof 3+4`
445class AlignofExpr final : public Expr {
446public:
447 ptr<Expr> expr;
448 ptr<Type> type;
449
450 AlignofExpr( const CodeLocation & loc, const Expr * e );
451 AlignofExpr( const CodeLocation & loc, const Type * t );
452 // deliberately no disambiguating overload for nullptr_t
453
454 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
455private:
456 AlignofExpr * clone() const override { return new AlignofExpr{ *this }; }
457 MUTATE_FRIEND
458};
459
460/// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)`
461class UntypedOffsetofExpr final : public Expr {
462public:
463 ptr<Type> type;
464 std::string member;
465
466 UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem )
467 : Expr( loc ), type( ty ), member( mem ) {}
468
469 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
470private:
471 UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; }
472 MUTATE_FRIEND
473};
474
475/// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)`
476class OffsetofExpr final : public Expr {
477public:
478 ptr<Type> type;
479 readonly<DeclWithType> member;
480
481 OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );
482
483 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
484private:
485 OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; }
486 MUTATE_FRIEND
487};
488
489/// a pack of field-offsets for a generic type
490class OffsetPackExpr final : public Expr {
491public:
492 ptr<StructInstType> type;
493
494 OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty );
495
496 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
497private:
498 OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; }
499 MUTATE_FRIEND
500};
501
502/// Variants of short-circuiting logical expression
503enum LogicalFlag { OrExpr, AndExpr };
504
505/// Short-circuiting boolean expression (`&&` or `||`)
506class LogicalExpr final : public Expr {
507public:
508 ptr<Expr> arg1;
509 ptr<Expr> arg2;
510 LogicalFlag isAnd;
511
512 LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia );
513
514 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
515private:
516 LogicalExpr * clone() const override { return new LogicalExpr{ *this }; }
517 MUTATE_FRIEND
518};
519
520/// Three-argument conditional e.g. `p ? a : b`
521class ConditionalExpr final : public Expr {
522public:
523 ptr<Expr> arg1;
524 ptr<Expr> arg2;
525 ptr<Expr> arg3;
526
527 ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 )
528 : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {}
529
530 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
531private:
532 ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; }
533 MUTATE_FRIEND
534};
535
536/// Comma expression e.g. `( a , b )`
537class CommaExpr final : public Expr {
538public:
539 ptr<Expr> arg1;
540 ptr<Expr> arg2;
541
542 CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
543 : Expr( loc ), arg1( a1 ), arg2( a2 ) {}
544
545 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
546private:
547 CommaExpr * clone() const override { return new CommaExpr{ *this }; }
548 MUTATE_FRIEND
549};
550
551/// A type used as an expression (e.g. a type generator parameter)
552class TypeExpr final : public Expr {
553public:
554 ptr<Type> type;
555
556 TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {}
557
558 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
559private:
560 TypeExpr * clone() const override { return new TypeExpr{ *this }; }
561 MUTATE_FRIEND
562};
563
564/// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`.
565/// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
566class AsmExpr final : public Expr {
567public:
568 ptr<Expr> inout;
569 ptr<Expr> constraint;
570 ptr<Expr> operand;
571
572 AsmExpr( const CodeLocation & loc, const Expr * io, const Expr * con, const Expr * op )
573 : Expr( loc ), inout( io ), constraint( con ), operand( op ) {}
574
575 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
576private:
577 AsmExpr * clone() const override { return new AsmExpr{ *this }; }
578 MUTATE_FRIEND
579};
580
581/// The application of a function to a set of parameters, along with a set of copy constructor
582/// calls, one for each argument
583class ImplicitCopyCtorExpr final : public Expr {
584public:
585 ptr<ApplicationExpr> callExpr;
586
587 ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
588 : Expr( loc, call->result ) { assert( call ); }
589
590 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
591private:
592 ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; }
593 MUTATE_FRIEND
594};
595
596/// Constructor in expression context, e.g. `int * x = alloc() { 42 };`
597class ConstructorExpr final : public Expr {
598public:
599 ptr<Expr> callExpr;
600
601 ConstructorExpr( const CodeLocation & loc, const Expr * call );
602
603 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
604private:
605 ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; }
606 MUTATE_FRIEND
607};
608
609/// A C99 compound literal, e.g. `(MyType){ a, b, c }`
610class CompoundLiteralExpr final : public Expr {
611public:
612 ptr<Init> init;
613
614 CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i );
615
616 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
617private:
618 CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; }
619 MUTATE_FRIEND
620};
621
622/// A range, e.g. `3 ... 5` or `1~10`
623class RangeExpr final : public Expr {
624public:
625 ptr<Expr> low;
626 ptr<Expr> high;
627
628 RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h )
629 : Expr( loc ), low( l ), high( h ) {}
630
631 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
632private:
633 RangeExpr * clone() const override { return new RangeExpr{ *this }; }
634 MUTATE_FRIEND
635};
636
637/// A tuple expression before resolution, e.g. `[a, b, c]`
638class UntypedTupleExpr final : public Expr {
639public:
640 std::vector<ptr<Expr>> exprs;
641
642 UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
643 : Expr( loc ), exprs( std::move(xs) ) {}
644
645 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
646private:
647 UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; }
648 MUTATE_FRIEND
649};
650
651/// A tuple expression after resolution, e.g. `[a, b, c]`
652class TupleExpr final : public Expr {
653public:
654 std::vector<ptr<Expr>> exprs;
655
656 TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs );
657
658 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
659private:
660 TupleExpr * clone() const override { return new TupleExpr{ *this }; }
661 MUTATE_FRIEND
662};
663
664/// An element selection operation on a tuple value, e.g. `t.3` after analysis
665class TupleIndexExpr final : public Expr {
666public:
667 ptr<Expr> tuple;
668 unsigned index;
669
670 TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i );
671
672 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
673private:
674 TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; }
675 MUTATE_FRIEND
676};
677
678/// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
679/// multiple-assignment: both sides of the assignment have tuple type,
680/// e.g. `[a, b, c] = [d, e, f];`
681/// mass-assignment: left-hand side has tuple type and right-hand side does not:
682/// e.g. `[a, b, c] = 42;`
683class TupleAssignExpr final : public Expr {
684public:
685 ptr<StmtExpr> stmtExpr;
686
687 TupleAssignExpr(
688 const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
689 std::vector<ptr<ObjectDecl>> && tempDecls );
690
691 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
692
693 friend class ::ConverterOldToNew;
694
695private:
696 TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
697 TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
698
699 MUTATE_FRIEND
700};
701
702/// A GCC "statement expression", e.g. `({ int x = 5; x })`
703class StmtExpr final : public Expr {
704public:
705 ptr<CompoundStmt> stmts;
706 std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression
707 std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s)
708
709 StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
710
711 /// Set the result type of this StmtExpr based on its body
712 void computeResult();
713
714 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
715private:
716 StmtExpr * clone() const override { return new StmtExpr{ *this }; }
717 MUTATE_FRIEND
718};
719
720/// An expression which must only be evaluated once
721class UniqueExpr final : public Expr {
722 static unsigned long long nextId;
723public:
724 ptr<Expr> expr;
725 ptr<ObjectDecl> object;
726 ptr<VariableExpr> var;
727 unsigned long long id;
728
729 UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull );
730
731 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
732private:
733 UniqueExpr * clone() const override { return new UniqueExpr{ *this }; }
734 MUTATE_FRIEND
735};
736
737/// One option for resolving an initializer expression
738struct InitAlternative {
739 ptr<Type> type;
740 ptr<Designation> designation;
741
742 InitAlternative() = default;
743 InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {}
744};
745
746/// Pre-resolution initializer expression
747class UntypedInitExpr final : public Expr {
748public:
749 ptr<Expr> expr;
750 std::deque<InitAlternative> initAlts;
751
752 UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as )
753 : Expr( loc ), expr( e ), initAlts( std::move(as) ) {}
754
755 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
756private:
757 UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; }
758 MUTATE_FRIEND
759};
760
761/// Post-resolution initializer expression
762class InitExpr final : public Expr {
763public:
764 ptr<Expr> expr;
765 ptr<Designation> designation;
766
767 InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des )
768 : Expr( loc, e->result ), expr( e ), designation( des ) {}
769
770 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
771private:
772 InitExpr * clone() const override { return new InitExpr{ *this }; }
773 MUTATE_FRIEND
774};
775
776/// Expression containing a deleted identifier.
777/// Internal to resolver.
778class DeletedExpr final : public Expr {
779public:
780 ptr<Expr> expr;
781 readonly<Decl> deleteStmt;
782
783 DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del )
784 : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); }
785
786 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
787private:
788 DeletedExpr * clone() const override { return new DeletedExpr{ *this }; }
789 MUTATE_FRIEND
790};
791
792/// Use of a default argument.
793/// Internal to resolver.
794class DefaultArgExpr final : public Expr {
795public:
796 ptr<Expr> expr;
797
798 DefaultArgExpr( const CodeLocation & loc, const Expr * e )
799 : Expr( loc, e->result ), expr( e ) { assert( e->result ); }
800
801 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
802private:
803 DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; }
804 MUTATE_FRIEND
805};
806
807/// C11 _Generic expression
808class GenericExpr final : public Expr {
809public:
810 /// One arm of the _Generic expr
811 struct Association {
812 ptr<Type> type;
813 ptr<Expr> expr;
814
815 Association() = default;
816 // default case
817 Association( const Expr * e ) : type(), expr( e ) {}
818 // non-default case
819 Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {}
820 };
821
822 ptr<Expr> control;
823 std::vector<Association> associations;
824
825 GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns )
826 : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {}
827
828 const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
829private:
830 GenericExpr * clone() const override { return new GenericExpr{ *this }; }
831 MUTATE_FRIEND
832};
833
834
835}
836
837#undef MUTATE_FRIEND
838
839// Local Variables: //
840// tab-width: 4 //
841// mode: c++ //
842// compile-command: "make install" //
843// End: //
Note: See TracBrowser for help on using the repository browser.