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,
47  };
48 
50  enum Lifetime {
51  ltERROR = 0,
55  };
56 
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.
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  }
97  _type = tSTRING;
98  _n = 1;
99  return *this;
100  }
103  _type = tERROR;
104  _n = 1;
105  return *this;
106  }
107 
110 
114  return *this;
115  }
119  return *this;
120  }
124  return *this;
125  }
128  _lifetime = ltERROR;
129  return *this;
130  }
132 
134 
137  _lifetime = a.lifetime();
138  return *this;
139  }
140 
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:
224  int _n;
227 };
228 
230 inline ExprType TypeVec(int n){
231  return ExprType().FP(n).Varying();
232 }
233 
234 } // namespace
235 
236 #pragma pop_macro("None")
237 
238 #endif
int _n
Dimension of type _n==1 ignored if _type != FP.
Definition: ExprType.h:224
int dim() const
Definition: ExprType.h:160
ExprType & Varying()
Mutate this into a varying lifetime.
Definition: ExprType.h:122
ExprType()
Default constructor for a type (error and lifetime error)
Definition: ExprType.h:58
Varying data (i.e. changes per evaluation point)
Definition: ExprType.h:52
bool operator==(const ExprType &other) const
Returns true if this and other match type and dimension.
Definition: ExprType.h:76
ExprType & Uniform()
Mutate this into a uniform lifetime.
Definition: ExprType.h:117
Type type() const
Definition: ExprType.h:159
Type _type
Class of type)
Definition: ExprType.h:222
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:164
Uniform data (i.e. changes only on grids or pixel tiles, depending on how expr used) ...
Definition: ExprType.h:53
Constant data (i.e. sub parts of the tree that need only be computed once)
Definition: ExprType.h:54
bool isString() const
Definition: ExprType.h:169
ExprType & String()
Mutate this into a string type.
Definition: ExprType.h:96
std::string toString() const
Stringify the type into a printable string.
Definition: ExprType.h:191
bool isValue() const
Definition: ExprType.h:166
bool isNone() const
Definition: ExprType.h:170
bool isFP(int d) const
Definition: ExprType.h:165
bool isLifetimeError() const
Definition: ExprType.h:186
ExprType & setLifetime(const ExprType &a)
Assign the lifetime from type a to be my type.
Definition: ExprType.h:136
Lifetime _lifetime
lifetime of type
Definition: ExprType.h:226
Lifetime
Lifetimes that are possible for type, note the order is from highest to lowest priority for promotion...
Definition: ExprType.h:50
bool isLifetimeUniform() const
Definition: ExprType.h:184
Error in lifetime (uniform data depending on varying etc.)
Definition: ExprType.h:51
ExprType TypeVec(int n)
Quick way to get a vector type i.e. 3 vec is TypeVec(3)
Definition: ExprType.h:230
Between a and b
Definition: userdoc.txt:180
ExprType(const ExprType &other)
Copy constructor.
Definition: ExprType.h:67
bool isLifetimeConstant() const
validity check: type is not an error
Definition: ExprType.h:183
Lifetime lifetime() const
Definition: ExprType.h:161
ExprType & Constant()
Mutate this into a constant lifetime.
Definition: ExprType.h:112
static bool valuesCompatible(const ExprType &a, const ExprType &b)
Checks if value types are compatible.
Definition: ExprType.h:173
ExprType(Type type, int n, Lifetime lifetime)
Fully specified type.
Definition: ExprType.h:61
bool operator!=(const ExprType &other) const
Returns true if this and other do not match on type and dimension.
Definition: ExprType.h:73
Error type (bad things happened here or upstream in tree)
Definition: ExprType.h:43
bool isValid() const
Definition: ExprType.h:167
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
ExprType & LifeError()
Mutate this into a lifetime error.
Definition: ExprType.h:127
bool isError() const
Definition: ExprType.h:168
bool isLifetimeVarying() const
Definition: ExprType.h:185
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
bool isLifeCompatible(const ExprType &o) const
Definition: ExprType.h:188
Floating point type (this combines with _d member to make vectors)
Definition: ExprType.h:44
Type
Possible types.
Definition: ExprType.h:42
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition: ExprType.h:90
ExprType & None()
Mutate this into a none type.
Definition: ExprType.h:84
Defined as a *alpha b *alpha< br ></div >< br > float< b > float a
Definition: userdoc.txt:174
ExprType & Error()
Mutate this into an error type.
Definition: ExprType.h:102