Repository navigation
Expand file tree
/
Copy pathType.cpp
More file actions
379 lines (292 loc) · 7.18 KB
/
Copy pathType.cpp
File metadata and controls
379 lines (292 loc) · 7.18 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* File: Type.cpp
*
* Description: This file contains the member function definitions for
* types in Simple C. A type is either a scalar type, an
* array type, or a function type.
*
* Note that we simply don't like putting function definitions
* in the header file. The header file is for the interface.
* Actually, we prefer opaque pointer types in C where you
* don't even get to see what's inside, much less touch it.
* But, C++ lets us have value types with access control
* instead of just always using pointer types.
*
* Extra functionality:
* - equality and inequality operators
* - predicate functions such as isArray()
* - stream operator
* - the error type
*/
# include <cassert>
# include "Type.h"
# include "tokens.h"
# include "nullptr.h"
using namespace std;
/*
* Function: Type::Type (constructor)
*
* Description: Initialize this type as an error type.
*/
Type::Type()
: _kind(ERROR)
{
}
/*
* Function: Type::Type (constructor)
*
* Description: Initialize this type object as a scalar type.
*/
Type::Type(int specifier, unsigned indirection)
: _specifier(specifier), _indirection(indirection), _kind(SCALAR)
{
}
/*
* Function: Type::Type (constructor)
*
* Description: Initialize this type object as an array type.
*/
Type::Type(int specifier, unsigned indirection, unsigned length)
: _specifier(specifier), _indirection(indirection), _length(length)
{
_kind = ARRAY;
}
/*
* Function: Type::Type (constructor)
*
* Description: Initialize this type object as a function type.
*/
Type::Type(int specifier, unsigned indirection, Parameters *parameters)
: _specifier(specifier), _indirection(indirection), _parameters(parameters)
{
assert(_parameters != nullptr);
_kind = FUNCTION;
}
/*
* Function: Type::operator ==
*
* Description: Return whether another type is equal to this type. The
* parameter lists are checked for function types, which C++
* makes so easy. (At least, it makes something easy!)
*/
bool Type::operator ==(const Type &rhs) const
{
if (_kind != rhs._kind)
return false;
if (_kind == ERROR)
return true;
if (_specifier != rhs._specifier)
return false;
if (_indirection != rhs._indirection)
return false;
if (_kind == SCALAR)
return true;
if (_kind == ARRAY)
return _length == rhs._length;
if (_parameters->_varargs != rhs._parameters->_varargs)
return false;
return _parameters->_types == rhs._parameters->_types;
}
/*
* Function: Type::operator !=
*
* Description: Well, what do you think it does? Why can't the language
* generate this function for us? Because they think we want
* it to do something else? Yeah, like that'd be a good idea.
*/
bool Type::operator !=(const Type &rhs) const
{
return !operator ==(rhs);
}
/*
* Function: Type::isArray
*
* Description: Return whether this type is an array type.
*/
bool Type::isArray() const
{
return _kind == ARRAY;
}
/*
* Function: Type::isScalar
*
* Description: Return whether this type is a scalar type.
*/
bool Type::isScalar() const
{
return _kind == SCALAR;
}
/*
* Function: Type::isFunction
*
* Description: Return whether this type is a function type.
*/
bool Type::isFunction() const
{
return _kind == FUNCTION;
}
/*
* Function: Type::isError
*
* Description: Return whether this type is an error type.
*/
bool Type::isError() const
{
return _kind == ERROR;
}
/*
* Function: Type::specifier (accessor)
*
* Description: Return the specifier of this type.
*/
int Type::specifier() const
{
return _specifier;
}
/*
* Function: Type::indirection (accessor)
*
* Description: Return the number of levels of indirection of this type.
*/
unsigned Type::indirection() const
{
return _indirection;
}
/*
* Function: Type::length (accessor)
*
* Description: Return the length of this type, which must be an array
* type. Is there an better way than calling assert? There
* certainly isn't an easier way.
*/
unsigned Type::length() const
{
assert(_kind == ARRAY);
return _length;
}
/*
* Function: Type::parameters (accessor)
*
* Description: Return the parameters of this type, which must be a
* function type.
*/
Parameters *Type::parameters() const
{
assert(_kind == FUNCTION);
return _parameters;
}
/*
* Function: Type::isInteger
*
* Description: Check if this type is the integer type after any promotion.
*/
bool Type::isInteger() const
{
return _kind == SCALAR && _specifier != VOID && _indirection == 0;
}
/*
* Function: Type::isPointer
*
* Description: Check if this type is a pointer type after any promotion.
*/
bool Type::isPointer() const
{
return (_kind == SCALAR && _indirection > 0) || _kind == ARRAY;
}
/*
* Function: Type::isValue
*
* Description: Check if this type is a value type after any promotion.
*/
bool Type::isValue() const
{
return isPointer() || isInteger();
}
/*
* Function: Type::isCompatibleWith
*
* Description: Check if this type is compatible with the other given type.
* In Simple C, two types are compatible if they are identical
* value types (after any promotion) or one is a pointer type
* and the other is pointer to void.
*/
bool Type::isCompatibleWith(const Type &that) const
{
if (isPointer() && that._indirection == 1 && that._specifier == VOID)
return true;
if (that.isPointer() && _indirection == 1 && _specifier == VOID)
return true;
return isValue() && promote() == that.promote();
}
/*
* Function: Type::promote
*
* Description: Return the result of performing type promotion on this
* type. In Simple C, a character is promoted to an integer,
* and an array is promoted to a pointer.
*/
Type Type::promote() const
{
if (_kind == SCALAR && _indirection == 0 && _specifier == CHAR)
return Type(INT);
if (_kind == ARRAY)
return Type(_specifier, _indirection + 1);
return *this;
}
/*
* Function: Type::deref
*
* Description: Return the result of dereferencing this type, which must be
* a pointer type before any promotion.
*/
Type Type::deref() const
{
assert(_kind == SCALAR && _indirection > 0);
return Type(_specifier, _indirection - 1);
}
/*
* Function: operator <<
*
* Description: Write a type to the specified output stream. At least C++
* let's us do some cool things.
*/
ostream &operator <<(ostream &ostr, const Type &type)
{
unsigned i;
if (type.isError())
ostr << "error";
else {
if (type.specifier() == INT)
ostr << "int";
else if (type.specifier() == CHAR)
ostr << "char";
else if (type.specifier() == VOID)
ostr << "void";
else
ostr << type.specifier(); // shouldn't happen
if (type.indirection() > 0) {
ostr << " ";
for (i = 0; i < type.indirection(); i ++)
ostr << "*";
}
if (type.isArray())
ostr << "[" << type.length() << "]";
else if (type.isFunction()) {
ostr << "(";
if (type.parameters() != nullptr) {
if (type.parameters()->_types.size() == 0)
ostr << "void";
else
for (i = 0; i < type.parameters()->_types.size(); i ++) {
if (i > 0)
ostr << ", ";
ostr << type.parameters()->_types[i];
}
}
if (type.parameters()->_varargs)
ostr << ", ...";
ostr << ")";
}
}
return ostr;
}