KSeExpr  4.0.4.0
ExprType.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2 // SPDX-License-Identifier: LicenseRef-Apache-2.0
3 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #pragma once
7 
8 #include <cassert>
9 #include <sstream>
10 #include <string>
11 
12 namespace KSeExpr
13 {
23 class ExprType
24 {
25 public:
27  enum Type {
28  tERROR = 0,
29  tFP,
31  tNONE
32  };
33 
35  enum Lifetime {
36  ltERROR = 0,
39  ltCONSTANT
40  };
41 
43  ExprType() = default;
44 
46  ~ExprType() = default;
47 
50  : _type(type)
51  , _n(n)
53  {
54  assert(_n >= 1);
55  assert(_type == tFP || _n == 1);
56  }
57 
59  ExprType(const ExprType &other)
60  : _type(other.type())
61  , _n(other.dim())
62  , _lifetime(other.lifetime())
63  {
64  assert(_n >= 1);
65  assert(_type == tFP || _n == 1);
66  }
67 
69  ExprType(ExprType &&) = default;
70 
72  ExprType &operator=(const ExprType &other) = default;
73  ExprType &operator=(ExprType &&) = default;
74 
76  bool operator!=(const ExprType &other) const
77  {
78  return !(*this == other);
79  }
80 
82  bool operator==(const ExprType &other) const
83  {
84  return (type() == other.type() && dim() == other.dim() && lifetime() == other.lifetime());
85  }
86 
88 
91  {
92  _type = tNONE;
93  _n = 1;
94  return *this;
95  }
97  ExprType &FP(int d)
98  {
99  _type = tFP;
100  _n = d;
101  return *this;
102  }
105  {
106  _type = tSTRING;
107  _n = 1;
108  return *this;
109  }
112  {
113  _type = tERROR;
114  _n = 1;
115  return *this;
116  }
117 
120 
123  {
125  return *this;
126  }
129  {
131  return *this;
132  }
135  {
137  return *this;
138  }
141  {
142  _lifetime = ltERROR;
143  return *this;
144  }
146 
148 
151  {
152  _lifetime = a.lifetime();
153  return *this;
154  }
155 
157  ExprType &setLifetime(const ExprType &a, const ExprType &b)
158  {
159  a.lifetime() < b.lifetime() ? setLifetime(a) : setLifetime(b);
160  return *this;
161  }
162 
164  ExprType &setLifetime(const ExprType &a, const ExprType &b, const ExprType &c)
165  {
166  setLifetime(a, b);
167  setLifetime(*this, c);
168  return *this;
169  }
171 
172  //####################################################################
174 
175  // accessors
176  Type type() const
177  {
178  return _type;
179  }
180  int dim() const
181  {
182  return _n;
183  }
185  {
186  return _lifetime;
187  }
188 
190  bool isFP() const
191  {
192  return _type == tFP;
193  }
194  bool isFP(int d) const
195  {
196  return _type == tFP && _n == d;
197  }
198  bool isValue() const
199  {
200  return _type == tFP || _type == tSTRING;
201  }
202  bool isValid() const
203  {
204  return !isError() && !isLifetimeError();
205  }
206  bool isError() const
207  {
208  return type() == tERROR;
209  }
210  bool isString() const
211  {
212  return type() == tSTRING;
213  }
214  bool isNone() const
215  {
216  return type() == tNONE;
217  }
218 
220  static bool valuesCompatible(const ExprType &a, const ExprType &b)
221  {
222  return (a.isString() && b.isString()) || (a._type == tFP && b._type == tFP && (a._n == 1 || b._n == 1 || a._n == b._n));
223  }
224 
226 
228 
229  // lifetime matchers
230  bool isLifetimeConstant() const
231  {
232  return lifetime() == ltCONSTANT;
233  }
234  bool isLifetimeUniform() const
235  {
236  return lifetime() == ltUNIFORM;
237  }
238  bool isLifetimeVarying() const
239  {
240  return lifetime() == ltVARYING;
241  }
242  bool isLifetimeError() const
243  {
244  return lifetime() == ltERROR;
245  }
246 
247  bool isLifeCompatible(const ExprType &o) const
248  {
249  return o.lifetime() >= lifetime();
250  }
251 
253  std::string toString() const
254  {
255  std::stringstream ss;
256 
257  if (isLifetimeConstant())
258  ss << "constant ";
259  else if (isLifetimeUniform())
260  ss << "uniform ";
261  else if (isLifetimeVarying())
262  ss << "varying ";
263  else if (isLifetimeError())
264  ss << "lifetime_error ";
265  else
266  ss << "Invalid_Lifetime ";
267 
268  if (isError())
269  ss << "Error";
270  else if (isFP(1))
271  ss << "Float";
272  else if (isFP())
273  ss << "Float[" << dim() << "]";
274  else if (isString())
275  ss << "String";
276  else if (isNone())
277  ss << "None";
278  else
279  ss << "Invalid_Type";
280  return ss.str();
281  }
282 
283 private:
287  int _n {1};
290 };
291 
293 inline ExprType TypeVec(int n)
294 {
295  return ExprType().FP(n).Varying();
296 }
297 
298 } // namespace KSeExpr
Lifetime
Lifetimes that are possible for type, note the order is from highest to lowest priority for promotion...
Definition: ExprType.h:35
@ ltUNIFORM
Uniform data (i.e. changes only on grids or pixel tiles, depending on how expr used)
Definition: ExprType.h:38
@ ltERROR
Error in lifetime (uniform data depending on varying etc.)
Definition: ExprType.h:36
@ ltCONSTANT
Constant data (i.e. sub parts of the tree that need only be computed once)
Definition: ExprType.h:39
@ ltVARYING
Varying data (i.e. changes per evaluation point)
Definition: ExprType.h:37
bool isLifetimeError() const
Definition: ExprType.h:242
bool isLifetimeUniform() const
Definition: ExprType.h:234
ExprType & None()
Mutate this into a none type.
Definition: ExprType.h:90
~ExprType()=default
Default destructor.
Type type() const
Definition: ExprType.h:176
bool isNone() const
Definition: ExprType.h:214
bool isFP(int d) const
Definition: ExprType.h:194
bool isLifetimeVarying() const
Definition: ExprType.h:238
ExprType & String()
Mutate this into a string type.
Definition: ExprType.h:104
bool isValid() const
Definition: ExprType.h:202
ExprType(ExprType &&)=default
Default move constructor.
int _n
Dimension of type _n==1 ignored if _type != FP.
Definition: ExprType.h:287
Lifetime _lifetime
lifetime of type
Definition: ExprType.h:289
Type _type
Class of type)
Definition: ExprType.h:285
Type
Possible types.
Definition: ExprType.h:27
@ tERROR
Error type (bad things happened here or upstream in tree)
Definition: ExprType.h:28
@ tFP
Floating point type (this combines with _d member to make vectors)
Definition: ExprType.h:29
@ tSTRING
String type.
Definition: ExprType.h:30
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:164
ExprType & Constant()
Mutate this into a constant lifetime.
Definition: ExprType.h:122
bool isError() const
Definition: ExprType.h:206
int dim() const
Definition: ExprType.h:180
ExprType & LifeError()
Mutate this into a lifetime error.
Definition: ExprType.h:140
ExprType(Type type, int n, Lifetime lifetime)
Fully specified type.
Definition: ExprType.h:49
ExprType & operator=(ExprType &&)=default
ExprType & Varying()
Mutate this into a varying lifetime.
Definition: ExprType.h:134
bool operator==(const ExprType &other) const
Returns true if this and other match type and dimension.
Definition: ExprType.h:82
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
Definition: ExprType.h:97
bool operator!=(const ExprType &other) const
Returns true if this and other do not match on type and dimension.
Definition: ExprType.h:76
bool isString() const
Definition: ExprType.h:210
ExprType & setLifetime(const ExprType &a)
Assign the lifetime from type a to be my type.
Definition: ExprType.h:150
ExprType(const ExprType &other)
Copy constructor.
Definition: ExprType.h:59
bool isLifetimeConstant() const
validity check: type is not an error
Definition: ExprType.h:230
std::string toString() const
Stringify the type into a printable string.
Definition: ExprType.h:253
ExprType & Uniform()
Mutate this into a uniform lifetime.
Definition: ExprType.h:128
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:190
ExprType & operator=(const ExprType &other)=default
Assignment operator.
bool isLifeCompatible(const ExprType &o) const
Definition: ExprType.h:247
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:157
ExprType & Error()
Mutate this into an error type.
Definition: ExprType.h:111
bool isValue() const
Definition: ExprType.h:198
static bool valuesCompatible(const ExprType &a, const ExprType &b)
Checks if value types are compatible.
Definition: ExprType.h:220
Lifetime lifetime() const
Definition: ExprType.h:184
ExprType()=default
Default constructor for a type (error and lifetime error)
ExprType TypeVec(int n)
Quick way to get a vector type i.e. 3 vec is TypeVec(3)
Definition: ExprType.h:293