00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "sigtype.hh"
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 ostream& operator<<(ostream& dst, const Type& t) { return t->print(dst);}
00035
00036 ostream& operator<<(ostream& dst, const SimpleType& t) { return t.print(dst); }
00037
00038 ostream& operator<<(ostream& dst, const TableType& t) { return t.print(dst); }
00039
00040 ostream& operator<<(ostream& dst, const TupletType& t) { return t.print(dst); }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00053 ostream& SimpleType::print(ostream& dst) const
00054 {
00055 return dst << "NR"[nature()]
00056 << "KB?S"[variability()]
00057 << "CI?E"[computability()]
00058 << "VS?TS"[vectorability()]
00059 << "N?B"[boolean()]
00060 << " " << fInterval;
00061 }
00062
00063
00067 ostream& TableType::print(ostream& dst) const
00068 {
00069 dst << "KB?S"[variability()]
00070 << "CI?E"[computability()]
00071 << " " << fInterval
00072 << ":Table(";
00073 fContent->print(dst);
00074 return dst << ')';
00075 }
00076
00077
00078
00082 ostream& TupletType::print(ostream& dst) const
00083 {
00084 dst << "KB?S"[variability()]
00085 << "CI?E"[computability()]
00086 << " " << fInterval
00087 << " : {";
00088 string sep = "";
00089 for (unsigned int i = 0; i < fComponents.size(); i++, sep="*") {
00090 dst << sep;
00091 fComponents[i]->print(dst);
00092 }
00093 dst << '}';
00094 return dst;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 Type TINT = new SimpleType(kInt, kKonst, kComp, kVect, kNum, interval());
00108 Type TREAL = new SimpleType(kReal, kKonst, kComp, kVect, kNum, interval());
00109
00110 Type TKONST = new SimpleType(kInt, kKonst, kComp, kVect, kNum, interval());
00111 Type TBLOCK = new SimpleType(kInt, kBlock, kComp, kVect, kNum, interval());
00112 Type TSAMP = new SimpleType(kInt, kSamp, kComp, kVect, kNum, interval());
00113
00114 Type TCOMP = new SimpleType(kInt, kKonst, kComp, kVect, kNum, interval());
00115 Type TINIT = new SimpleType(kInt, kKonst, kInit, kVect, kNum, interval());
00116 Type TEXEC = new SimpleType(kInt, kKonst, kExec, kVect, kNum, interval());
00117
00118
00119
00120 Type TINPUT = new SimpleType(kReal, kSamp, kExec, kVect, kNum, interval());
00121 Type TGUI = new SimpleType(kReal, kBlock,kExec, kVect, kNum, interval());
00122 Type INT_TGUI = new SimpleType(kInt, kBlock,kExec, kVect, kNum, interval());
00123
00124 Type TREC = TINT;
00125
00126
00127 Type operator| ( const Type& t1, const Type& t2)
00128 {
00129 SimpleType *st1, *st2;
00130 TableType *tt1, *tt2;
00131 TupletType *nt1, *nt2;
00132
00133 if ( (st1 = isSimpleType(t1)) && (st2 = isSimpleType(t2)) ) {
00134
00135 return new SimpleType( st1->nature()|st2->nature(),
00136 st1->variability()|st2->variability(),
00137 st1->computability()|st2->computability(),
00138 st1->vectorability()|st2->vectorability(),
00139 st1->boolean()|st2->boolean(),
00140 interval()
00141 );
00142
00143 } else if ( (tt1 = isTableType(t1)) && (tt2 = isTableType(t2)) ) {
00144
00145 return new TableType( tt1->content() | tt2->content() );
00146
00147 } else if ( (nt1 = isTupletType(t1)) && (nt2 = isTupletType(t2)) ) {
00148
00149 vector<Type> v;
00150 int n = min(nt1->arity(), nt2->arity());
00151 for (int i=0; i<n; i++) { v.push_back( (*nt1)[i] | (*nt2)[i]); }
00152 return new TupletType( v );
00153
00154 } else {
00155
00156 cerr << "Error : trying to combine incompatible types, " << t1 << " and " << t2 << endl;
00157 exit(1);
00158 return 0;
00159 }
00160 }
00161
00162 bool operator== ( const Type& t1, const Type& t2)
00163 {
00164 SimpleType *st1, *st2;
00165 TableType *tt1, *tt2;
00166 TupletType *nt1, *nt2;
00167
00168 if (t1->variability() != t2->variability()) return false;
00169 if (t1->computability() != t2->computability()) return false;
00170
00171 if ( (st1 = isSimpleType(t1)) && (st2 = isSimpleType(t2)) ) return (st1->nature() == st2->nature())&&(st1->vectorability() == st2->vectorability())&&(st1->boolean() == st2->boolean());
00172 if ( (tt1 = isTableType(t1)) && (tt2 = isTableType(t2)) ) return tt1->content()== tt2->content();
00173 if ( (nt1 = isTupletType(t1)) && (nt2 = isTupletType(t2)) ) {
00174 int a1 = nt1->arity();
00175 int a2 = nt2->arity();
00176 if (a1 == a2) {
00177 for (int i=0; i<a1; i++) { if ((*nt1)[i] != (*nt2)[i]) return false; }
00178 return true;
00179 } else {
00180 return false;
00181 }
00182 }
00183 return false;
00184 }
00185
00186 bool operator<= ( const Type& t1, const Type& t2)
00187 {
00188 return (t1|t2) == t2;
00189 }
00190
00191
00192
00193 Type operator* (const Type& t1, const Type& t2)
00194 {
00195 vector<Type> v;
00196
00197 TupletType* nt1 = dynamic_cast<TupletType*>((AudioType*)t1);
00198 TupletType* nt2 = dynamic_cast<TupletType*>((AudioType*)t2);
00199
00200 if (nt1) {
00201 for (int i=0; i<nt1->arity(); i++) {
00202 v.push_back((*nt1)[i]);
00203 }
00204 } else {
00205 v.push_back(t1);
00206 }
00207
00208 if (nt2) {
00209 for (int i=0; i<nt2->arity(); i++) {
00210 v.push_back((*nt2)[i]);
00211 }
00212 } else {
00213 v.push_back(t2);
00214 }
00215 return new TupletType(v);
00216 }
00217
00218
00219 SimpleType* isSimpleType(AudioType* t) { return dynamic_cast<SimpleType*>(t); }
00220 TableType* isTableType(AudioType* t) { return dynamic_cast<TableType*>(t); }
00221 TupletType* isTupletType(AudioType* t) { return dynamic_cast<TupletType*>(t); }
00222
00223
00224
00225
00226
00227
00228 Type checkInt(Type t)
00229 {
00230
00231 SimpleType* st = isSimpleType(t);
00232 if (st == 0 || st->nature() > kInt) {
00233 cerr << "Error : checkInt failed for type " << t << endl;
00234 exit(1);
00235 }
00236 return t;
00237 }
00238
00239 Type checkKonst(Type t)
00240 {
00241
00242 if (t->variability() > kKonst) {
00243 cerr << "Error : checkKonst failed for type " << t << endl;
00244 exit(1);
00245 }
00246 return t;
00247 }
00248
00249 Type checkInit(Type t)
00250 {
00251
00252 if (t->computability() > kInit) {
00253 cerr << "Error : checkInit failed for type " << t << endl;
00254 exit(1);
00255 }
00256 return t;
00257 }
00258
00259 Type checkIntParam(Type t)
00260 {
00261 return checkInit(checkKonst(checkInt(t)));
00262 }
00263
00264 Type checkWRTbl(Type tbl, Type wr)
00265 {
00266
00267 if (wr->nature() > tbl->nature()) {
00268 cerr << "Error : checkWRTbl failed, the content of " << tbl << " is incompatible with " << wr << endl;
00269 exit(1);
00270 }
00271 return tbl;
00272 }
00273
00279 int checkDelayInterval(Type t)
00280 {
00281 interval i = t->getInterval();
00282 if (i.valid && i.lo >= 0) {
00283 return int(i.hi+0.5);
00284 } else {
00285
00286 return -1;
00287 }
00288 }
00289
00290
00291
00292 string cType (Type t)
00293 {
00294 return (t->nature() == kInt) ? "int" : "float";
00295 }