2d4694cfd3b3de2ce4f38938b695f9bf56e953b0
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / signals / sigtype.hh
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22
23
24 #ifndef _SigType_
25 #define _SigType_
26
27 #include <vector>
28 #include <string>
29 #include <iostream>
30 #include "tree.hh"
31 #include "smartpointer.hh"
32 #include "interval.hh"
33
34
35 /*********************************************************************
36 *
37 * Type System for FAUST
38 *
39 * <type> ::= <simpletype> || table(<type>)
40 * || <type>|<type> || <type>*<type>
41 * <simpletype> ::= <nature><variability><computability><vectorability>||<boolean>
42 * <nature> ::= TINT || TREAL
43 * <variability> ::= TKONST || TBLOCK || TSAMP
44 * <computability> ::= TCOMP || TINIT || TEXEC
45 * <vectorability> ::= KVECT || KSCAL || KTRUESCAL
46 * <boolean> ::= KNUM || KBOOL
47 *
48 **********************************************************************/
49
50
51 //--------------------------------------------------
52 // qualite des types simples
53
54 enum { kInt = 0, kReal = 1 }; ///< nature : integer or floating point values
55 enum { kNum = 0 , kBool = 1}; ///< boolean : when a signal stands for a boolean value ( while being of c-type int or float )
56 enum { kKonst = 0, kBlock = 1, kSamp = 3 }; ///< variability : how fast values change
57 enum { kComp = 0, kInit = 1, kExec = 3 }; ///< computability : when values are available
58 enum { kVect = 0, kScal = 1, kTrueScal = 3/*, kIndex = 4*/};///< vectorability: when a signal can be vectorized ( actually, only kVect and kScal matter; kTrueScal and kIndex don't denote types but are here to simplify code generation )
59
60 /*---------------------------------------------------------------------
61
62 AbstractType :
63 The root class for SimpleType, TableType and TupletType
64
65 Type :
66 A smartPointer to type
67
68 ----------------------------------------------------------------------*/
69
70 using namespace std;
71
72 class AudioType;
73
74 typedef P<AudioType> Type;
75
76 /**
77 * The Root class for all audio data types.
78 * All audio types have a "variability" (how fast the values change) and
79 * a "computability" (when the values are available). Simple types have
80 * also a "nature" (integer or floating point).
81 */
82 class AudioType
83 {
84 public:
85 static int gAllocationCount;
86 protected:
87 int fNature; ///< the kind of data represented
88 int fVariability; ///< how fast values change
89 int fComputability; ///< when are values available
90 int fVectorability; ///< when a signal can be vectorized
91 int fBoolean; ///< when a signal stands for a boolean value
92
93 interval fInterval; ///< Minimal and maximal values the signal can take
94 Tree fCode; ///< Tree representation (for memoization purposes)
95
96
97 public :
98 AudioType(int n, int v, int c, int vec = kVect, int b = kNum, interval i=interval())
99 : fNature(n), fVariability(v), fComputability(c),
100 fVectorability(vec), fBoolean(b),
101 fInterval(i), fCode(0) {} ///< constructs an abstract audio type
102
103 virtual ~AudioType() {} ///< not really useful here, but make compiler happier
104
105 int nature() const { return fNature; } ///< returns the kind of values (integre or floating point)
106 int variability() const { return fVariability; } ///< returns how fast values change (constant, by blocks, by samples)
107 int computability() const { return fComputability;} ///< returns when values are available (compilation, initialisation, execution)
108 int vectorability() const { return fVectorability;} ///< returns when a signal can be vectorized
109 int boolean() const { return fBoolean; } ///< returns when a signal stands for a boolean value
110
111 interval getInterval() const { return fInterval; } ///< returns the interval (min dn max values) of a signal
112
113 void setCode(Tree code) { fCode = code; } ///< returns the interval (min dn max values) of a signal
114 Tree getCode() { return fCode; } ///< returns the interval (min dn max values) of a signal
115
116
117
118 virtual AudioType* promoteNature(int n) = 0; ///< promote the nature of a type
119 virtual AudioType* promoteVariability(int n) = 0; ///< promote the variability of a type
120 virtual AudioType* promoteComputability(int n) = 0; ///< promote the computability of a type
121 virtual AudioType* promoteVectorability(int n) = 0; ///< promote the vectorability of a type
122 virtual AudioType* promoteBoolean(int n) = 0; ///< promote the booleanity of a type
123 //virtual AudioType* promoteInterval(const interval& i) = 0; ///< promote the interval of a type
124
125
126 virtual ostream& print(ostream& dst) const = 0; ///< print nicely a type
127 virtual bool isMaximal() const = 0; ///< true when type is maximal (and therefore can't change depending of hypothesis)
128
129 virtual AudioType* dimensions(vector<int>& D) = 0; /// Fill D with the dimensions of the type and returns its base type
130
131 protected:
132 void setInterval(const interval& r) { fInterval = r;}
133
134 };
135
136 //printing
137 inline ostream& operator << (ostream& s, const AudioType& n) { return n.print(s); }
138
139
140 /**
141 * Return the nature of a vector of types.
142 */
143 inline int mergenature(const vector<Type>& v)
144 {
145 int r = 0;
146 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->nature();
147 return r;
148 }
149
150
151
152 /**
153 * Return the variability of a vector of types.
154 */
155 inline int mergevariability(const vector<Type>& v)
156 {
157 int r = 0;
158 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->variability();
159 return r;
160 }
161
162
163
164 /**
165 * Return the computability of a vector of types.
166 */
167 inline int mergecomputability(const vector<Type>& v)
168 {
169 int r = 0;
170 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->computability();
171 return r;
172 }
173
174
175
176 /**
177 * Return the vectorability of a vector of types.
178 */
179 inline int mergevectorability(const vector<Type>& v)
180 {
181 int r = 0;
182 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->vectorability();
183 return r;
184 }
185
186
187
188 /**
189 * Return the booleanity of a vector of types.
190 */
191 inline int mergeboolean(const vector<Type>& v)
192 {
193 int r = 0;
194 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->boolean();
195 return r;
196 }
197
198
199
200 /**
201 * Return the interval of a vector of types.
202 */
203 inline interval mergeinterval(const vector<Type>& v)
204 {
205 if (v.size()==0) {
206 return interval();
207 } else {
208 double lo=0, hi=0;
209 for (unsigned int i = 0; i < v.size(); i++) {
210 interval r = v[i]->getInterval();
211 if (!r.valid) return r;
212 if (i==0) {
213 lo = r.lo;
214 hi = r.hi;
215 } else {
216 lo = min(lo,r.lo);
217 hi = max(hi,r.hi);
218 }
219 }
220 return interval(lo, hi);
221 }
222 }
223
224
225 AudioType* makeSimpleType(int n, int v, int c, int vec, int b, const interval& i);
226
227 AudioType* makeTableType(const Type& ct);
228 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec);
229 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec, int b, const interval& i);
230
231
232 AudioType* makeTupletType(const vector<Type>& vt);
233 AudioType* makeTupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i);
234
235 /**
236 * The type of a simple numeric audio signal.
237 * Beside a computability and a variability, SimpleTypes have
238 * a "nature" indicating if they represent an integer or floating
239 * point audio signals.
240 */
241 class SimpleType : public AudioType
242 {
243 public :
244
245 SimpleType(int n, int v, int c, int vec, int b, const interval& i) : AudioType(n,v,c,vec,b,i) {
246 //cerr << "new simple type " << i << " -> " << *this << endl;
247 } ///< constructs a SimpleType from a nature a variability and a computability
248
249 virtual ostream& print(ostream& dst) const; ///< print a SimpleType
250
251
252
253 virtual AudioType* promoteNature(int n) { return makeSimpleType(n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the nature of a type
254 virtual AudioType* promoteVariability(int v) { return makeSimpleType(fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the variability of a type
255 virtual AudioType* promoteComputability(int c) { return makeSimpleType(fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); } ///< promote the computability of a type
256 virtual AudioType* promoteVectorability(int vec) { return makeSimpleType(fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval); } ///< promote the vectorability of a type
257 virtual AudioType* promoteBoolean(int b) { return makeSimpleType(fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); } ///< promote the booleanity of a type
258 // virtual AudioType* promoteInterval(const interval& i) {
259 // cerr << "promote to Interval " << i << endl;
260 // cerr << "for type : " << *this << endl;
261 // Type t = makeSimpleType(fNature, fVariability, fComputability, fVectorability, fBoolean, i); ///< promote the interval of a type
262 // cerr << "gives type " << *t << endl;
263 // return t;
264 // }
265 virtual AudioType* dimensions(vector<int>& D) { D.clear(); return this; } // scalar have no dimensions
266
267 virtual bool isMaximal() const; ///< true when type is maximal (and therefore can't change depending of hypothesis)
268
269
270
271 };
272
273 inline Type intCast (Type t) { return makeSimpleType(kInt, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
274 inline Type floatCast (Type t) { return makeSimpleType(kReal, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
275 inline Type sampCast (Type t) { return makeSimpleType(t->nature(), kSamp, t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
276 inline Type boolCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kBool, t->getInterval()); }
277 inline Type numCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kNum, t->getInterval()); }
278 inline Type vecCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), kVect, t->boolean(), t->getInterval()); }
279 inline Type scalCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), kScal, t->boolean(), t->getInterval()); }
280 inline Type truescalCast (Type t){ return makeSimpleType(t->nature(), t->variability(), t->computability(), kTrueScal, t->boolean(), t->getInterval()); }
281
282 inline Type castInterval (Type t, const interval& i)
283 {
284 return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean(), i);
285 }
286
287 /**
288 * The type of a table of audio data.
289 * Beside a computability and a variability, TableTypes have
290 * a "content" indicating the type of the data stored in the table.
291 */
292 class TableType : public AudioType
293 {
294 protected :
295 const Type fContent; ///< type of that data stored in the table
296
297 public :
298 TableType(const Type& t) :
299 AudioType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean()),
300 fContent(t) {} ///< construct a TableType with a content of a type t
301 #if 0
302 TableType(const Type& t, int v, int c) :
303 AudioType(t->nature(), t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()),
304 fContent(t) {} ///< construct a TableType with a content of a type t, promoting variability and computability
305
306 TableType(const Type& t, int n, int v, int c) :
307 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()),
308 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability and computability
309
310 TableType(const Type& t, int n, int v, int c, int vec, int b) :
311 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b),
312 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability, computability, vectorability and booleanity
313 #endif
314
315 TableType(const Type& t, int n, int v, int c, int vec, int b, const interval& i) :
316 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b, i),
317 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability, computability, vectorability and booleanity
318
319 TableType(const Type& t, int n, int v, int c, int vec) :
320 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()),
321 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability, computability and vectorability
322
323
324 Type content() const { return fContent; } ///< return the type of data store in the table
325 virtual ostream& print(ostream& dst) const; ///< print a TableType
326
327 virtual AudioType* promoteNature(int n) { return makeTableType(fContent, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the nature of a type
328 virtual AudioType* promoteVariability(int v) { return makeTableType(fContent, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the variability of a type
329 virtual AudioType* promoteComputability(int c) { return makeTableType(fContent, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); } ///< promote the computability of a type
330 virtual AudioType* promoteVectorability(int vec) { return makeTableType(fContent, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);}///< promote the vectorability of a type
331 virtual AudioType* promoteBoolean(int b) { return makeTableType(fContent, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); } ///< promote the booleanity of a type
332 //virtual AudioType* promoteInterval(const interval& i) { return makeTableType(fContent, fNature, fVariability, fComputability, fVectorability, fBoolean, i); } ///< promote the interval of a type
333
334 virtual bool isMaximal() const; ///< true when type is maximal (and therefore can't change depending of hypothesis)
335
336 virtual AudioType* dimensions(vector<int>& D) { D.clear(); return this; } // tables have no dimensions
337
338 };
339
340
341
342 /**
343 * The type of a tuplet of data.
344 * Beside a computability and a variability, TupletTypes have
345 * a set of components.
346 */
347 class TupletType : public AudioType
348 {
349 protected:
350 vector<Type> fComponents;
351
352 public:
353 TupletType() :
354 AudioType(0,0,0)
355 {}
356
357 TupletType(const vector<Type>& vt) :
358 AudioType(mergenature(vt),mergevariability(vt),mergecomputability(vt),mergevectorability(vt),mergeboolean(vt), mergeinterval(vt)),
359 fComponents(vt) {}
360
361 TupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i) :
362 AudioType(n|mergenature(vt), v|mergevariability(vt), c|mergecomputability(vt), vec|mergevectorability(vt), b|mergeboolean(vt), i),
363 fComponents(vt) {}
364
365 int arity() const { return fComponents.size(); }
366 Type operator[](unsigned int i) const { return fComponents[i]; }
367 virtual ostream& print(ostream& dst) const;
368
369 virtual AudioType* promoteNature(int n) { return new TupletType(fComponents, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the nature of a type
370 virtual AudioType* promoteVariability(int v) { return new TupletType(fComponents, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the variability of a type
371 virtual AudioType* promoteComputability(int c) { return new TupletType(fComponents, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); } ///< promote the computability of a type
372 virtual AudioType* promoteVectorability(int vec) { return new TupletType(fComponents, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);} ///< promote the vectorability of a type
373 virtual AudioType* promoteBoolean(int b) { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); } ///< promote the booleanity of a type
374 //virtual AudioType* promoteInterval(const interval& i) { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, fBoolean, i); } ///< promote the interval of a type
375
376 virtual bool isMaximal() const; ///< true when type is maximal (and therefore can't change depending of hypothesis)
377 virtual AudioType* dimensions(vector<int>& D) { D.clear(); return this; } // tuples have no dimensions
378
379 };
380
381
382
383 /**
384 * The type T = vector(n, T') of a signal of vectors of fixed size n and elements of type T'
385 * The following properties hold :
386 * nature(vector(n,T')) = nature(T')
387 * variability(vector(n,T')) = variability(T')
388 * computability(vector(n,T')) = computability(T')
389 * vectorability(vector(n,T')) = ??? (so probably no)
390 * boolean(vector(n,T')) = false (a vector is not a boolean)
391 * getInterval(vector(n,T')) = getInterval(T') (is it meanuful ?)
392 */
393 class VectorType : public AudioType
394 {
395 protected:
396 int fSize; ///< size of the vector
397 Type fContent; ///< type of the elements
398
399 public:
400 VectorType(int n, const Type& t) : AudioType(t->nature(), t->variability(), t->computability(), t->vectorability(),
401 t->boolean(), t->getInterval()),
402 fSize(n),
403 fContent(t)
404 {}
405
406
407 int size() const { return fSize; }
408 Type content() const { return fContent; }
409
410 virtual ostream& print(ostream& dst) const;
411
412 virtual AudioType* promoteNature(int n) { return new VectorType(fSize, fContent->promoteNature(n)); } ///< promote the nature of a type
413 virtual AudioType* promoteVariability(int v) { return new VectorType(fSize, fContent->promoteVariability(v)); } ///< promote the variability of a type
414 virtual AudioType* promoteComputability(int c) { return new VectorType(fSize, fContent->promoteComputability(c)); } ///< promote the computability of a type
415 virtual AudioType* promoteVectorability(int vec) { return new VectorType(fSize, fContent->promoteVectorability(vec)); } ///< promote the vectorability of a type
416 virtual AudioType* promoteBoolean(int b) { return new VectorType(fSize, fContent->promoteBoolean(b)); } ///< promote the booleanity of a type
417 virtual bool isMaximal() const { return false; } ///< true when type is maximal (and therefore can't change depending of hypothesis)
418 virtual AudioType* dimensions(vector<int>& D) { AudioType* t = fContent->dimensions(D); D.push_back(fSize); return t;} ///< vectors have a dimension
419
420
421 };
422
423 Type makeVectorType(const Type& b, const vector<int>& dim);
424 bool maxdimensions(const vector<int>& D1, const vector<int>& D2, vector<int>& D3);
425
426
427
428
429 //-------------------------------------------------
430 //-------------------------------------------------
431 // operations sur les types
432 //-------------------------------------------------
433 //-------------------------------------------------
434
435
436
437 //--------------------------------------------------
438 // liste de types predefinis
439
440 extern Type TINT;
441 extern Type TREAL;
442
443 extern Type TKONST;
444 extern Type TBLOCK;
445 extern Type TSAMP;
446
447 extern Type TCOMP;
448 extern Type TINIT;
449 extern Type TEXEC;
450
451 extern Type TINPUT;
452 extern Type TGUI;
453 extern Type TGUI01;
454 extern Type INT_TGUI;
455 extern Type TREC;
456
457
458 //--------------------------------------------------
459 // creation de types
460
461 Type table (const Type& t);
462 Type operator| (const Type& t1, const Type& t2);
463 Type operator* (const Type& t1, const Type& t2);
464
465
466 //--------------------------------------------------
467 // comparaison de types
468
469 bool operator==(const Type& t1, const Type& t2);
470 bool operator<=(const Type& t1, const Type& t2);
471
472 inline bool operator!=(const Type& t1, const Type& t2) { return !(t1==t2); }
473 inline bool operator< (const Type& t1, const Type& t2) { return t1<=t2 && t1!=t2; }
474 inline bool operator> (const Type& t1, const Type& t2) { return t2<t1; }
475 inline bool operator>=(const Type& t1, const Type& t2) { return t2<=t1; }
476
477
478 //--------------------------------------------------
479 // predicats-conversion de types
480
481 SimpleType* isSimpleType (AudioType* t);
482 TableType* isTableType (AudioType* t);
483 TupletType* isTupletType (AudioType* t);
484 VectorType* isVectorType (AudioType* t);
485
486
487 //--------------------------------------------------
488 // impressions de types
489
490 ostream& operator<<(ostream& dst, const SimpleType& t);
491 ostream& operator<<(ostream& dst, const Type& t);
492 ostream& operator<<(ostream& dst, const TableType& t);
493 ostream& operator<<(ostream& dst, const TupletType& t);
494 ostream& operator<<(ostream& dst, const VectorType& t);
495
496
497 //--------------------------------------------------
498 // verification de type
499
500 Type checkInt(Type t); ///< verifie que t est entier
501 Type checkKonst(Type t); ///< verifie que t est constant
502 Type checkInit(Type t); ///< verifie que t est connu a l'initialisation
503
504 Type checkIntParam(Type t); ///< verifie que t est connu a l'initialisation, constant et entier
505
506 Type checkWRTbl(Type tbl, Type wr); ///< verifie que wr est compatible avec le contenu de tbl
507
508 int checkDelayInterval(Type t); ///< Check if the interval of t is appropriate for a delay
509
510
511 //--------------------------------------------------
512 // conversion de type
513
514 string cType (Type t);
515
516 Tree codeAudioType(AudioType* t); ///< Code an audio type as a tree (memoization)
517
518
519 #endif