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 <map> |
---|
20 | #include <utility> // for move |
---|
21 | #include <vector> |
---|
22 | |
---|
23 | #include "Fwd.hpp" // for UniqueId |
---|
24 | #include "ParseNode.hpp" |
---|
25 | #include "Visitor.hpp" |
---|
26 | |
---|
27 | namespace ast { |
---|
28 | |
---|
29 | /// Contains the ID of a declaration and a type that is derived from that declaration, |
---|
30 | /// but subject to decay-to-pointer and type parameter renaming |
---|
31 | struct ParamEntry { |
---|
32 | UniqueId decl; |
---|
33 | ptr<Type> actualType; |
---|
34 | ptr<Type> formalType; |
---|
35 | ptr<Expr> expr; |
---|
36 | |
---|
37 | ParamEntry() : decl( 0 ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {} |
---|
38 | ParamEntry( UniqueId id, Type* actual, Type* formal, Expr* e ) |
---|
39 | : decl( id ), actualType( actual ), formalType( formal ), expr( e ) {} |
---|
40 | }; |
---|
41 | |
---|
42 | /// Pre-resolution list of parameters to infer |
---|
43 | using ResnSlots = std::vector<UniqueId>; |
---|
44 | /// Post-resolution map of inferred parameters |
---|
45 | using InferredParams = std::map< UniqueId, ParamEntry >; |
---|
46 | |
---|
47 | /// Base node for expressions |
---|
48 | class Expr : public ParseNode { |
---|
49 | public: |
---|
50 | /// Saves space (~16 bytes) by combining ResnSlots and InferredParams |
---|
51 | struct InferUnion { |
---|
52 | enum { Empty, Slots, Params } mode; |
---|
53 | union data_t { |
---|
54 | char def; |
---|
55 | ResnSlots resnSlots; |
---|
56 | InferredParams inferParams; |
---|
57 | |
---|
58 | data_t() : def('\0') {} |
---|
59 | ~data_t() {} |
---|
60 | } data; |
---|
61 | |
---|
62 | /// initializes from other InferUnion |
---|
63 | void init_from( const InferUnion& o ) { |
---|
64 | switch ( o.mode ) { |
---|
65 | case Empty: return; |
---|
66 | case Slots: new(&data.resnSlots) ResnSlots{ o.data.resnSlots }; return; |
---|
67 | case Params: new(&data.inferParams) InferredParams{ o.data.inferParams }; return; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /// initializes from other InferUnion (move semantics) |
---|
72 | void init_from( InferUnion&& o ) { |
---|
73 | switch ( o.mode ) { |
---|
74 | case Empty: return; |
---|
75 | case Slots: new(&data.resnSlots) ResnSlots{ std::move(o.data.resnSlots) }; return; |
---|
76 | case Params: |
---|
77 | new(&data.inferParams) InferredParams{ std::move(o.data.inferParams) }; return; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | /// clears variant fields |
---|
82 | void reset() { |
---|
83 | switch( mode ) { |
---|
84 | case Empty: return; |
---|
85 | case Slots: data.resnSlots.~ResnSlots(); return; |
---|
86 | case Params: data.inferParams.~InferredParams(); return; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | InferUnion() : mode(Empty), data() {} |
---|
91 | InferUnion( const InferUnion& o ) : mode( o.mode ), data() { init_from( o ); } |
---|
92 | InferUnion( InferUnion&& o ) : mode( o.mode ), data() { init_from( std::move(o) ); } |
---|
93 | InferUnion& operator= ( const InferUnion& ) = delete; |
---|
94 | InferUnion& operator= ( InferUnion&& ) = delete; |
---|
95 | ~InferUnion() { reset(); } |
---|
96 | |
---|
97 | ResnSlots& resnSlots() { |
---|
98 | switch (mode) { |
---|
99 | case Empty: new(&data.resnSlots) ResnSlots{}; mode = Slots; // fallthrough |
---|
100 | case Slots: return data.resnSlots; |
---|
101 | case Params: assert(!"Cannot return to resnSlots from Params"); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | InferredParams& inferParams() { |
---|
106 | switch (mode) { |
---|
107 | case Slots: data.resnSlots.~ResnSlots(); // fallthrough |
---|
108 | case Empty: new(&data.inferParams) InferredParams{}; mode = Params; // fallthrough |
---|
109 | case Params: return data.inferParams; |
---|
110 | } |
---|
111 | } |
---|
112 | }; |
---|
113 | |
---|
114 | ptr<Type> result; |
---|
115 | ptr<TypeSubstitution> env; |
---|
116 | InferUnion inferred; |
---|
117 | bool extension = false; |
---|
118 | |
---|
119 | Expr(const CodeLocation & loc ) : ParseNode( loc ), result(), env(), inferred() {} |
---|
120 | |
---|
121 | Expr * set_extension( bool ex ) { extension = ex; return this; } |
---|
122 | |
---|
123 | virtual Expr * accept( Visitor & v ) override = 0; |
---|
124 | private: |
---|
125 | virtual Expr * clone() const override = 0; |
---|
126 | }; |
---|
127 | |
---|
128 | /// A type used as an expression (e.g. a type generator parameter) |
---|
129 | class TypeExpr final : public Expr { |
---|
130 | public: |
---|
131 | ptr<Type> type; |
---|
132 | |
---|
133 | TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {} |
---|
134 | |
---|
135 | Expr * accept( Visitor & v ) override { return v.visit( this ); } |
---|
136 | private: |
---|
137 | TypeExpr * clone() const override { return new TypeExpr{ *this }; } |
---|
138 | }; |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | // Local Variables: // |
---|
143 | // tab-width: 4 // |
---|
144 | // mode: c++ // |
---|
145 | // compile-command: "make install" // |
---|
146 | // End: // |
---|