File: TType.hh

package info (click to toggle)
faust 2.14.4~repack2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 276,136 kB
  • sloc: cpp: 231,578; ansic: 15,403; sh: 10,871; java: 6,917; objc: 4,085; makefile: 3,002; cs: 1,077; ruby: 951; python: 885; xml: 550; yacc: 516; lex: 233; lisp: 201
file content (199 lines) | stat: -rw-r--r-- 5,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef __TType__
#define __TType__

#include "TPrintable.hh"
#include "Text.hh"

#include <assert.h>

string getFreshID(const string& prefix);

struct TType : public TPrintable
{
    virtual ~TType () {}

	virtual void generate(ostream* dst, int n) = 0;
    virtual void generateCPP(ostream* dst, int n) = 0;
    virtual void generateCPPNoAlias(ostream* dst, int n) = 0;
    virtual void generateDef(ostream* dst, int n) = 0;
    virtual void generateDefNoAlias(ostream* dst, int n) = 0;

    virtual int getSize()  = 0;

    virtual bool equal(TType* type) = 0;

    virtual TType* deref() = 0;

};

struct TTypable
{
  	virtual TType* getType() = 0;
};

struct TRateable
{
  	virtual int getRate() = 0;
};

struct TIntType : public TType
{
    virtual ~TIntType() {}

	virtual void generate(ostream* dst, int n) { *dst << "int"; }
    virtual void generateCPP(ostream* dst, int n) { *dst << "int"; }
    virtual void generateCPPNoAlias(ostream* dst, int n) { *dst << "int"; }
    virtual void generateDef(ostream* dst, int n) {}
    virtual void generateDefNoAlias(ostream* dst, int n) {}

    virtual int getSize() { return 0; }

    virtual bool equal(TType* type)
    {
        return dynamic_cast<TIntType*>(type);
    }

    virtual TType* deref()
    {
        cerr << "Error : dereferencing int type !!" << endl;
        assert(false);
    }
};

struct TFloatType : public TType
{
    virtual ~TFloatType() {}

	virtual void generate(ostream* dst, int n) { *dst << "float"; }
    virtual void generateCPP(ostream* dst, int n) { *dst << "float"; }
    virtual void generateCPPNoAlias(ostream* dst, int n) { *dst << "float"; }
    virtual void generateDef(ostream* dst, int n) {}
    virtual void generateDefNoAlias(ostream* dst, int n) {}

    virtual int getSize() { return 0; }

    virtual bool equal(TType* type)
    {
        return dynamic_cast<TFloatType*>(type);
    }

    virtual TType* deref()
    {
        cerr << "Error : dereferencing float type !!" << endl;
        assert(false);
    }
};

struct TVectorType : public TType
{
    TType* fType;
    int fSize;
    string fDecName;
    bool fGenerated;

    TVectorType(TType* type, int size, const string& name):fType(type), fSize(size), fDecName(name), fGenerated(false)
    {}

    virtual ~TVectorType() {}

	virtual void generate(ostream* dst, int n)
    {
       fType->generate(dst, n); *dst << "[" << fSize << "]";
    }
    virtual void generateCPP(ostream* dst, int n)
    {
        *dst  << fDecName;
    }

    virtual void generateCPPNoAlias(ostream* dst, int n)
    {
        *dst  << fDecName;
    }

    void generatePoly(ostream* dst, int n, const string& op)
    {
        tab(n+1, *dst); *dst << fDecName << " operator" << op << "(const " << fDecName << "& val)" << " {" << endl;
            tab(n+2, *dst); *dst << "for (int i = 0; i < " << fSize << "; i++) {" << endl;
            tab(n+3, *dst); *dst << "f[i] = f[i] " << op << " val.f[i];" << endl;
            tab(n+2, *dst); *dst << "}" << endl;
            tab(n+2, *dst); *dst << "return *this;" << endl;
        tab(n+1, *dst); *dst << "}" << endl;
    }

    void generatePolyScalar(ostream* dst, int n, const string& op)
    {
        tab(n+1, *dst); *dst << fDecName << " operator" << op << "(float val)" << " {" << endl;
            tab(n+2, *dst); *dst << "for (int i = 0; i < " << fSize << "; i++) {" << endl;
            tab(n+3, *dst); *dst << "f[i] = f[i] " << op << " val;" << endl;
            tab(n+2, *dst); *dst << "}" << endl;
            tab(n+2, *dst); *dst << "return *this;" << endl;
        tab(n+1, *dst); *dst << "}" << endl;
    }

    virtual void generateDef(ostream* dst, int n)
    {
        if (!fGenerated) {
            tab(n, *dst);
            *dst << "struct " << fDecName;
            *dst << " {" << endl;
            tab(n+1, *dst);
            fType->generateCPP(dst, n+1);
            *dst << " f" << "[" << fSize << "];" << endl;

            generatePoly(dst, n, "+");
            generatePoly(dst, n, "-");
            generatePoly(dst, n, "*");
            generatePoly(dst, n, "/");

            generatePolyScalar(dst, n, "+");
            generatePolyScalar(dst, n, "-");
            generatePolyScalar(dst, n, "*");
            generatePolyScalar(dst, n, "/");

            tab(n, *dst); *dst << "};" << endl;
            fGenerated = true;
        }
    }

    virtual void generateDefNoAlias(ostream* dst, int n)
    {
        if (!fGenerated) {
            tab(n, *dst);
            *dst << "typedef ";
            fType->generateCPPNoAlias(dst, n);
            *dst << " " << fDecName;
            *dst << "[" << fSize << "];" << endl;
            fGenerated = true;
        }
    }

    virtual int getSize() { return fSize; }

    virtual bool equal(TType* type)
    {
        TVectorType* vec_type = dynamic_cast<TVectorType*>(type);
        return vec_type && fSize == vec_type->fSize && fType->equal(vec_type->fType);
    }

    virtual TType* deref()
    {
        return fType;
    }

    vector<int> dimensions() const
    {
        std::vector<int> ret;
        ret.push_back(fSize);
        TVectorType * vt = dynamic_cast<TVectorType*>(fType);
        if (vt) {
            vector<int> base = vt->dimensions();
            ret.insert(ret.end(), base.begin(), base.end());
        }
        return ret;
    }

};

#endif