SeExpr

ExprType.h
Go to the documentation of this file.
1 /*
2 Copyright Disney Enterprises, Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License
6 and the following modification to it: Section 6 Trademarks.
7 deleted and replaced with:
8
9 6. Trademarks. This License does not grant permission to use the
10 trade names, trademarks, service marks, or product names of the
11 Licensor and its affiliates, except as required for reproducing
12 the content of the NOTICE file.
13
14 You may obtain a copy of the License at
15 http://www.apache.org/licenses/LICENSE-2.0
16 */
17 #ifndef ExprType_h
18 #define ExprType_h
19
20 #include <vector>
21 #include <string>
22 #include <map>
23 #include <cassert>
24 #include <sstream>
25
26 #pragma push_macro("None")
27 #undef None
28
29 namespace SeExpr2 {
39 class ExprType {
40 public:
42 enum Type {
43 tERROR = 0,
44 tFP,
45 tSTRING,
46 tNONE
47 };
48
50 enum Lifetime {
51 ltERROR = 0,
52 ltVARYING,
53 ltUNIFORM,
54 ltCONSTANT
55 };
56
58 ExprType() : _type(tERROR), _n(1), _lifetime(ltERROR) {}
59
61 ExprType(Type type, int n, Lifetime lifetime) : _type(type), _n(n), _lifetime(lifetime) {
62 assert(_n >= 1);
63 assert(_type == tFP || _n == 1);
64 }
65
67 ExprType(const ExprType& other) : _type(other.type()), _n(other.dim()), _lifetime(other.lifetime()) {
68 assert(_n >= 1);
69 assert(_type == tFP || _n == 1);
70 }
71
73 bool operator!=(const ExprType& other) const { return !(*this == other); }
74
76 bool operator==(const ExprType& other) const {
77 return (type() == other.type() && dim() == other.dim() && lifetime() == other.lifetime());
78 }
79
81
83 // TODO: "None" is bad name, /usr/include/X11/X.h potentially text replaces it.
84 ExprType& None() {
85 _type = tNONE;
86 _n = 1;
87 return *this;
88 }
90 ExprType& FP(int d) {
91 _type = tFP;
92 _n = d;
93 return *this;
94 }
96 ExprType& String() {
97 _type = tSTRING;
98 _n = 1;
99 return *this;
100 }
102 ExprType& Error() {
103 _type = tERROR;
104 _n = 1;
105 return *this;
106 }
107
110
112 ExprType& Constant() {
113 _lifetime = ltCONSTANT;
114 return *this;
115 }
117 ExprType& Uniform() {
118 _lifetime = ltUNIFORM;
119 return *this;
120 }
122 ExprType& Varying() {
123 _lifetime = ltVARYING;
124 return *this;
125 }
127 ExprType& LifeError() {
128 _lifetime = ltERROR;
129 return *this;
130 }
132
134
136 ExprType& setLifetime(const ExprType& a) {
137 _lifetime = a.lifetime();
138 return *this;
139 }
140
142 ExprType& setLifetime(const ExprType& a, const ExprType& b) {
143 a.lifetime() < b.lifetime() ? setLifetime(a) : setLifetime(b);
144 return *this;
145 }
146
148 ExprType& setLifetime(const ExprType& a, const ExprType& b, const ExprType& c) {
149 setLifetime(a, b);
150 setLifetime(*this, c);
151 return *this;
152 }
154
155 //####################################################################
157
158 // accessors
159 Type type() const { return _type; }
160 int dim() const { return _n; }
161 Lifetime lifetime() const { return _lifetime; }
162
164 bool isFP() const { return _type == tFP; }
165 bool isFP(int d) const { return _type == tFP && _n == d; }
166 bool isValue() const { return _type == tFP || _type == tSTRING; }
167 bool isValid() const { return !isError() && !isLifetimeError(); }
168 bool isError() const { return type() == tERROR; }
169 bool isString() const { return type() == tSTRING; }
170 bool isNone() const { return type() == tNONE; }
171
173 static bool valuesCompatible(const ExprType& a, const ExprType& b) {
174 return (a.isString() && b.isString()) ||
175 (a._type == tFP && b._type == tFP && (a._n == 1 || b._n == 1 || a._n == b._n));
176 }
177
179
181
182 // lifetime matchers
183 bool isLifetimeConstant() const { return lifetime() == ltCONSTANT; }
184 bool isLifetimeUniform() const { return lifetime() == ltUNIFORM; }
185 bool isLifetimeVarying() const { return lifetime() == ltVARYING; }
186 bool isLifetimeError() const { return lifetime() == ltERROR; }
187
188 bool isLifeCompatible(const ExprType& o) const { return o.lifetime() >= lifetime(); }
189
191 std::string toString() const {
192 std::stringstream ss;
193
194 if (isLifetimeConstant())
195 ss << "constant ";
196 else if (isLifetimeUniform())
197 ss << "uniform ";
198 else if (isLifetimeVarying())
199 ss << "varying ";
200 else if (isLifetimeError())
201 ss << "lifetime_error ";
202 else
203 ss << "Invalid_Lifetime ";
204
205 if (isError())
206 ss << "Error";
207 else if (isFP(1))
208 ss << "Float";
209 else if (isFP())
210 ss << "Float[" << dim() << "]";
211 else if (isString())
212 ss << "String";
213 else if (isNone())
214 ss << "None";
215 else
216 ss << "Invalid_Type";
217 return ss.str();
218 }
219
220 private:
222 Type _type;
224 int _n;
226 Lifetime _lifetime;
227 };
228
230 inline ExprType TypeVec(int n) { return ExprType().FP(n).Varying(); }
231
232 } // namespace
233
234 #pragma pop_macro("None")
235
236 #endif
SeExpr2::ExprType::isString
bool isString() const
Definition: ExprType.h:169
SeExpr2::ExprType::_n
int _n
Dimension of type _n==1 ignored if _type != FP.
Definition: ExprType.h:224
SeExpr2::ExprType::isError
bool isError() const
Definition: ExprType.h:168
SeExpr2::ExprType::tNONE
Definition: ExprType.h:46
SeExpr2::ExprType::setLifetime
ExprType & setLifetime(const ExprType &a, const ExprType &b)
Combine the lifetimes (min wins) of a and b and then assign them to this.
Definition: ExprType.h:142
SeExpr2::ExprType::FP
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition: ExprType.h:90
SeExpr2::ExprType::ExprType
ExprType()
Default constructor for a type (error and lifetime error)
Definition: ExprType.h:58
SeExpr2::ExprType::None
ExprType & None()
Mutate this into a none type.
Definition: ExprType.h:84
SeExpr2::ExprType::isLifeCompatible
bool isLifeCompatible(const ExprType &o) const
Definition: ExprType.h:188
SeExpr2::ExprType::Varying
ExprType & Varying()
Mutate this into a varying lifetime.
Definition: ExprType.h:122
SeExpr2::ExprType::type
Type type() const
Definition: ExprType.h:159
SeExpr2::ExprType::isLifetimeUniform
bool isLifetimeUniform() const
Definition: ExprType.h:184
SeExpr2::ExprType::Constant
ExprType & Constant()
Mutate this into a constant lifetime.
Definition: ExprType.h:112
SeExpr2::ExprType::isValue
bool isValue() const
Definition: ExprType.h:166
SeExpr2::ExprType::_type
Type _type
Class of type)
Definition: ExprType.h:222
SeExpr2::ExprType::dim
int dim() const
Definition: ExprType.h:160
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::ExprType::tSTRING
String type.
Definition: ExprType.h:45
SeExpr2::ExprType::String
ExprType & String()
Mutate this into a string type.
Definition: ExprType.h:96
SeExpr2::ExprType::ltERROR
Error in lifetime (uniform data depending on varying etc.)
Definition: ExprType.h:51
SeExpr2::ExprType::operator!=
bool operator!=(const ExprType &other) const
Returns true if this and other do not match on type and dimension.
Definition: ExprType.h:73
SeExpr2::ExprType::isValid
bool isValid() const
Definition: ExprType.h:167
SeExpr2::ExprType::_lifetime
Lifetime _lifetime
lifetime of type
Definition: ExprType.h:226
SeExpr2
Definition: Context.h:22
SeExpr2::ExprType::isLifetimeConstant
bool isLifetimeConstant() const
validity check: type is not an error
Definition: ExprType.h:183
SeExpr2::ExprType::lifetime
Lifetime lifetime() const
Definition: ExprType.h:161
SeExpr2::ExprType::isNone
bool isNone() const
Definition: ExprType.h:170
SeExpr2::ExprType::LifeError
ExprType & LifeError()
Mutate this into a lifetime error.
Definition: ExprType.h:127
SeExpr2::ExprType::tERROR
Error type (bad things happened here or upstream in tree)
Definition: ExprType.h:43
SeExpr2::ExprType::operator==
bool operator==(const ExprType &other) const
Returns true if this and other match type and dimension.
Definition: ExprType.h:76
SeExpr2::ExprType::Error
ExprType & Error()
Mutate this into an error type.
Definition: ExprType.h:102
SeExpr2::ExprType::ExprType
ExprType(Type type, int n, Lifetime lifetime)
Fully specified type.
Definition: ExprType.h:61
SeExpr2::ExprType::setLifetime
ExprType & setLifetime(const ExprType &a, const ExprType &b, const ExprType &c)
Combine the lifetimes (min wins) of a and b and then assign them to this.
Definition: ExprType.h:148
SeExpr2::ExprType::isFP
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:164
SeExpr2::ExprType::Lifetime
Lifetime
Lifetimes that are possible for type, note the order is from highest to lowest priority for promotion...
Definition: ExprType.h:50
SeExpr2::ExprType::toString
std::string toString() const
Stringify the type into a printable string.
Definition: ExprType.h:191
SeExpr2::ExprType::ExprType
ExprType(const ExprType &other)
Copy constructor.
Definition: ExprType.h:67
SeExpr2::TypeVec
ExprType TypeVec(int n)
Quick way to get a vector type i.e. 3 vec is TypeVec(3)
Definition: ExprType.h:230
a
Defined as a *alpha b *alpha< br ></div >< br > float< b > float a
Definition: userdoc.txt:174
SeExpr2::ExprType::isLifetimeError
bool isLifetimeError() const
Definition: ExprType.h:186
SeExpr2::ExprType::Type
Type
Possible types.
Definition: ExprType.h:42
SeExpr2::ExprType::setLifetime
ExprType & setLifetime(const ExprType &a)
Assign the lifetime from type a to be my type.
Definition: ExprType.h:136
SeExpr2::ExprType::isFP
bool isFP(int d) const
Definition: ExprType.h:165
SeExpr2::ExprType::isLifetimeVarying
bool isLifetimeVarying() const
Definition: ExprType.h:185
SeExpr2::ExprType::valuesCompatible
static bool valuesCompatible(const ExprType &a, const ExprType &b)
Checks if value types are compatible.
Definition: ExprType.h:173
SeExpr2::ExprType::Uniform
ExprType & Uniform()
Mutate this into a uniform lifetime.
Definition: ExprType.h:117
SeExpr2::ExprType::ltUNIFORM
Uniform data (i.e. changes only on grids or pixel tiles, depending on how expr used)
Definition: ExprType.h:53
SeExpr2::ExprType::ltCONSTANT
Constant data (i.e. sub parts of the tree that need only be computed once)
Definition: ExprType.h:54
b
Between a and b
Definition: userdoc.txt:180
SeExpr2::ExprType::ltVARYING
Varying data (i.e. changes per evaluation point)
Definition: ExprType.h:52
SeExpr2::ExprType::tFP
Floating point type (this combines with _d member to make vectors)
Definition: ExprType.h:44