source: src/AST/Expr.hpp@ c86b08d

ADT ast-experimental
Last change on this file since c86b08d was 5408b59, checked in by JiadaL <j82liang@…>, 3 years ago

Remove var in QualifiedNameExpr

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