FAUST compiler  0.9.9.6b8
sigtype.hh
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003     FAUST compiler
00004     Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005     ---------------------------------------------------------------------
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021  
00022  
00023  
00024 #ifndef _SigType_
00025 #define _SigType_
00026 
00027 #include <vector>
00028 #include <string>
00029 #include <iostream>
00030 #include "tree.hh"
00031 #include "smartpointer.hh"
00032 #include "interval.hh"
00033 
00034 
00035 /*********************************************************************
00036 *
00037 *                       Type System for FAUST
00038 *
00039 *   <type>          ::= <simpletype> ||  table(<type>)
00040 *                       ||  <type>|<type>  || <type>*<type>
00041 *   <simpletype>    ::= <nature><variability><computability><vectorability>||<boolean>
00042 *   <nature>        ::= TINT || TREAL
00043 *   <variability>   ::= TKONST || TBLOCK || TSAMP
00044 *   <computability> ::= TCOMP  || TINIT  || TEXEC
00045 *   <vectorability> ::= KVECT  || KSCAL  || KTRUESCAL
00046 *   <boolean>       ::= KNUM   || KBOOL
00047 *
00048 **********************************************************************/
00049 
00050 
00051 //--------------------------------------------------
00052 // qualite des types simples
00053 
00054 enum { kInt = 0, kReal = 1 };                           
00055 enum { kNum = 0 , kBool = 1};                           
00056 enum { kKonst = 0, kBlock = 1, kSamp = 3 };             
00057 enum { kComp = 0, kInit = 1, kExec = 3 };               
00058 enum { kVect = 0, kScal = 1, kTrueScal = 3/*, kIndex = 4*/};
00059 
00060 /*---------------------------------------------------------------------
00061 
00062     AbstractType : 
00063     The root class for SimpleType, TableType and TupletType
00064     
00065     Type :
00066     A smartPointer to type
00067 
00068 ----------------------------------------------------------------------*/
00069 
00070 using namespace std;
00071 
00072 class AudioType;
00073 
00074 typedef P<AudioType> Type;
00075 
00082 class AudioType
00083 {
00084   public:
00085     static int  gAllocationCount;
00086   protected:
00087     int         fNature;            
00088     int         fVariability;       
00089     int         fComputability;     
00090     int         fVectorability;     
00091     int         fBoolean;           
00092     
00093     interval    fInterval;          
00094     Tree        fCode;              
00095 
00096     
00097   public :        
00098     AudioType(int n, int v, int c, int vec = kVect, int b = kNum, interval i=interval()) 
00099           : fNature(n), fVariability(v), fComputability(c), 
00100             fVectorability(vec), fBoolean(b), 
00101             fInterval(i), fCode(0) {}                           
00102     virtual ~AudioType()                                    {}  
00103     
00104     int     nature()        const   { return fNature;       }   
00105     int     variability()   const   { return fVariability; }    
00106     int     computability() const   { return fComputability;}   
00107     int     vectorability() const   { return fVectorability;}   
00108     int     boolean()       const   { return fBoolean; }        
00109     
00110     interval getInterval()  const   { return fInterval; }       
00111 
00112     void    setCode(Tree code)      { fCode = code; }           
00113     Tree    getCode()               { return fCode; }           
00114 
00115     
00116     virtual AudioType* promoteNature(int n)     = 0;            
00117     virtual AudioType* promoteVariability(int n)    = 0;        
00118     virtual AudioType* promoteComputability(int n)  = 0;        
00119     virtual AudioType* promoteVectorability(int n)  = 0;        
00120     virtual AudioType* promoteBoolean(int n)    = 0;            
00121     //virtual AudioType* promoteInterval(const interval& i) = 0;        ///< promote the interval of a type
00122 
00123     
00124     virtual ostream& print(ostream& dst) const      = 0;        
00125     virtual bool    isMaximal() const               = 0;        
00126     
00127   protected:    
00128     void        setInterval(const interval& r)  { fInterval = r;}
00129 
00130 };
00131 
00132 //printing
00133 inline ostream& operator << (ostream& s, const AudioType& n) { return n.print(s); }
00134 
00135 
00139 inline int mergenature(const vector<Type>& v)
00140 {
00141     int r = 0;
00142     for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->nature(); 
00143     return r;
00144 }
00145 
00146 
00147 
00151 inline int mergevariability(const vector<Type>& v)
00152 {
00153     int r = 0;
00154     for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->variability(); 
00155     return r;
00156 }
00157 
00158 
00159 
00163 inline int mergecomputability(const vector<Type>& v)
00164 {
00165     int r = 0;
00166     for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->computability(); 
00167     return r;
00168 }
00169 
00170 
00171 
00175 inline int mergevectorability(const vector<Type>& v)
00176 {
00177     int r = 0;
00178     for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->vectorability();
00179     return r;
00180 }
00181 
00182 
00183 
00187 inline int mergeboolean(const vector<Type>& v)
00188 {
00189     int r = 0;
00190     for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->boolean();
00191     return r;
00192 }
00193 
00194 
00195 
00199 inline interval mergeinterval(const vector<Type>& v)
00200 {
00201     if (v.size()==0) {
00202         return interval();
00203     } else {
00204         double lo=0, hi=0;
00205         for (unsigned int i = 0; i < v.size(); i++) {
00206             interval r = v[i]->getInterval();
00207             if (!r.valid) return r;
00208             if (i==0) { 
00209                 lo = r.lo;
00210                 hi = r.hi;
00211             } else {
00212                 lo = min(lo,r.lo);
00213                 hi = max(hi,r.hi);
00214             }
00215         }
00216         return interval(lo, hi);
00217     }
00218 }
00219 
00220 
00221 AudioType* makeSimpleType(int n, int v, int c, int vec, int b, const interval& i);
00222 
00223 AudioType* makeTableType(const Type& ct);
00224 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec);
00225 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec, int b, const interval& i);
00226 
00227 
00228 AudioType* makeTupletType(const vector<Type>& vt);
00229 AudioType* makeTupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i);
00230 
00237 class SimpleType : public AudioType
00238 {   
00239   public :
00240 
00241     SimpleType(int n, int v, int c, int vec, int b, const interval& i) : AudioType(n,v,c,vec,b,i) {
00242         //cerr << "new simple type " << i << " -> " << *this << endl;
00243     }           
00244 
00245     virtual ostream& print(ostream& dst) const;                     
00246 
00247 
00248 
00249     virtual AudioType* promoteNature(int n)             { return makeSimpleType(n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); }        
00250     virtual AudioType* promoteVariability(int v)            { return makeSimpleType(fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); }        
00251     virtual AudioType* promoteComputability(int c)          { return makeSimpleType(fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); }        
00252     virtual AudioType* promoteVectorability(int vec)        { return makeSimpleType(fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval); }  
00253     virtual AudioType* promoteBoolean(int b)                { return makeSimpleType(fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); }        
00254 //  virtual AudioType* promoteInterval(const interval& i)   { 
00255 //      cerr << "promote to Interval " << i  << endl; 
00256 //      cerr << "for type : " << *this << endl;
00257 //      Type t = makeSimpleType(fNature, fVariability, fComputability, fVectorability, fBoolean, i);                ///< promote the interval of a type
00258 //      cerr << "gives type " << *t << endl;
00259 //      return t;
00260 //  }
00261 
00262 
00263     virtual bool    isMaximal() const;                              
00264 
00265 
00266 
00267 };
00268 
00269 inline Type intCast (Type t)    { return makeSimpleType(kInt, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
00270 inline Type floatCast (Type t)  { return makeSimpleType(kReal, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
00271 inline Type sampCast (Type t)   { return makeSimpleType(t->nature(), kSamp, t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
00272 inline Type boolCast (Type t)   { return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kBool, t->getInterval()); }
00273 inline Type numCast (Type t)    { return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kNum, t->getInterval()); }
00274 inline Type vecCast (Type t)    { return makeSimpleType(t->nature(), t->variability(), t->computability(), kVect, t->boolean(), t->getInterval()); }
00275 inline Type scalCast (Type t)   { return makeSimpleType(t->nature(), t->variability(), t->computability(), kScal, t->boolean(), t->getInterval()); }
00276 inline Type truescalCast (Type t){ return makeSimpleType(t->nature(), t->variability(), t->computability(), kTrueScal, t->boolean(), t->getInterval()); }
00277 
00278 inline Type castInterval (Type t, const interval& i)    
00279 { 
00280     return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean(), i);
00281 }
00282 
00288 class TableType : public AudioType 
00289 {   
00290   protected :         
00291     const Type fContent;                                            
00292     
00293   public :
00294     TableType(const Type& t) : 
00295           AudioType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean()), 
00296           fContent(t) {}        
00297 #if 0
00298     TableType(const Type& t, int v, int c) : 
00299           AudioType(t->nature(), t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()), 
00300           fContent(t) {}        
00301 
00302     TableType(const Type& t, int n, int v, int c) : 
00303           AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()), 
00304           fContent(t) {}        
00305 
00306     TableType(const Type& t, int n, int v, int c, int vec, int b) :
00307           AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b),
00308           fContent(t) {}        
00309 #endif
00310 
00311     TableType(const Type& t, int n, int v, int c, int vec, int b, const interval& i) :
00312           AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b, i),
00313           fContent(t) {}        
00314 
00315     TableType(const Type& t, int n, int v, int c, int vec) :
00316           AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()),
00317           fContent(t) {}        
00318 
00319 
00320     Type content() const                { return fContent;  }       
00321     virtual ostream& print(ostream& dst) const;                     
00322     
00323     virtual AudioType* promoteNature(int n)             { return makeTableType(fContent, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); }   
00324     virtual AudioType* promoteVariability(int v)            { return makeTableType(fContent, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); }   
00325     virtual AudioType* promoteComputability(int c)          { return makeTableType(fContent, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); }   
00326     virtual AudioType* promoteVectorability(int vec)        { return makeTableType(fContent, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);}
00327     virtual AudioType* promoteBoolean(int b)                { return makeTableType(fContent, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); }   
00328     //virtual AudioType* promoteInterval(const interval& i) { return makeTableType(fContent, fNature, fVariability, fComputability, fVectorability, fBoolean, i); }         ///< promote the interval of a type
00329 
00330     virtual bool    isMaximal() const;                              
00331 };
00332 
00333 
00334             
00340 class TupletType : public AudioType
00341 {
00342   protected:
00343     vector<Type> fComponents;
00344     
00345   public:
00346     TupletType() :
00347           AudioType(0,0,0)
00348           {}
00349 
00350     TupletType(const vector<Type>& vt) :
00351           AudioType(mergenature(vt),mergevariability(vt),mergecomputability(vt),mergevectorability(vt),mergeboolean(vt), mergeinterval(vt)),
00352           fComponents(vt) {}
00353 
00354     TupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i) :
00355           AudioType(n|mergenature(vt), v|mergevariability(vt), c|mergecomputability(vt), vec|mergevectorability(vt), b|mergeboolean(vt), i),
00356           fComponents(vt) {}
00357 
00358     int arity() const                       { return fComponents.size(); }
00359     Type operator[](unsigned int i) const   { return fComponents[i]; }
00360     virtual ostream& print(ostream& dst) const;
00361     
00362     virtual AudioType* promoteNature(int n)             { return new TupletType(fComponents, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); }   
00363     virtual AudioType* promoteVariability(int v)            { return new TupletType(fComponents, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); }   
00364     virtual AudioType* promoteComputability(int c)          { return new TupletType(fComponents, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); }   
00365     virtual AudioType* promoteVectorability(int vec)        { return new TupletType(fComponents, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);}  
00366     virtual AudioType* promoteBoolean(int b)                { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval);  }  
00367     //virtual AudioType* promoteInterval(const interval& i) { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, fBoolean, i);  }            ///< promote the interval of a type
00368   
00369     virtual bool    isMaximal() const;                              
00370 
00371 };
00372 
00373 
00374 
00375 
00376 //-------------------------------------------------
00377 //-------------------------------------------------
00378 //              operations sur les types
00379 //-------------------------------------------------
00380 //-------------------------------------------------
00381 
00382 
00383 
00384 //--------------------------------------------------
00385 // liste de types predefinis
00386 
00387 extern Type TINT;
00388 extern Type TREAL;
00389 
00390 extern Type TKONST;
00391 extern Type TBLOCK;
00392 extern Type TSAMP;
00393 
00394 extern Type TCOMP;
00395 extern Type TINIT;
00396 extern Type TEXEC;
00397 
00398 extern Type TINPUT;
00399 extern Type TGUI;
00400 extern Type TGUI01;
00401 extern Type INT_TGUI;
00402 extern Type TREC;
00403 
00404 
00405 //--------------------------------------------------
00406 // creation de types
00407 
00408 Type table  (const Type& t);
00409 Type operator| 	(const Type& t1, const Type& t2);
00410 Type operator* 	(const Type& t1, const Type& t2);
00411 
00412 
00413 //--------------------------------------------------
00414 // comparaison de types
00415 
00416 bool operator==(const Type& t1, const Type& t2);
00417 bool operator<=(const Type& t1, const Type& t2);
00418 
00419 inline bool operator!=(const Type& t1, const Type& t2) { return !(t1==t2); }
00420 inline bool operator< (const Type& t1, const Type& t2) { return t1<=t2 && t1!=t2; }
00421 inline bool operator> (const Type& t1, const Type& t2) { return t2<t1;  }
00422 inline bool operator>=(const Type& t1, const Type& t2) { return t2<=t1; }
00423 
00424 
00425 //--------------------------------------------------
00426 // predicats-conversion de types
00427 
00428 SimpleType*     isSimpleType (AudioType* t);
00429 TableType*      isTableType  (AudioType* t);
00430 TupletType*     isTupletType (AudioType* t);
00431 
00432 
00433 //--------------------------------------------------
00434 // impressions de types
00435 
00436 ostream& operator<<(ostream& dst, const SimpleType& t); 
00437 ostream& operator<<(ostream& dst, const Type& t);
00438 ostream& operator<<(ostream& dst, const TableType& t);
00439 ostream& operator<<(ostream& dst, const TupletType& t);
00440 
00441 
00442 //--------------------------------------------------
00443 // verification de type
00444 
00445 Type checkInt(Type t);              
00446 Type checkKonst(Type t);            
00447 Type checkInit(Type t);             
00448 
00449 Type checkIntParam(Type t);         
00450 
00451 Type checkWRTbl(Type tbl, Type wr); 
00452 
00453 int checkDelayInterval(Type t);     
00454 
00455 
00456 //--------------------------------------------------
00457 // conversion de type
00458 
00459 string cType (Type t);
00460 
00461 Tree codeAudioType(AudioType* t);   
00462 
00463 
00464 #endif