-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathType.cpp
More file actions
348 lines (269 loc) · 6.74 KB
/
Copy pathType.cpp
File metadata and controls
348 lines (269 loc) · 6.74 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
/*
* File: Type.cpp
*
* Description: This file contains the class definitions for types in
* Simple C. A type is either a scalar type, an array type,
* or a function type. Types include a specifier and the
* number of levels of indirection. Array types also have a
* length, and function types also have a parameter list. An
* error type is also supported for use in undeclared
* identifiers and the results of type checking.
*
* Extra functionality:
* - equality and inequality operators
* - predicate functions
* - stream operator
* - error type
*/
# include <cassert>
# include "tokens.h"
# include "Type.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)
: _kind(SCALAR), _specifier(specifier), _indirection(indirection)
{
assert(specifier == CHAR || specifier == INT || specifier == LONG);
}
/*
* Function: Type::Type (constructor)
*
* Description: Initialize this type object as an array type.
*/
Type::Type(int specifier, unsigned indirection, unsigned long length)
: _kind(ARRAY), _specifier(specifier), _indirection(indirection)
{
assert(specifier == CHAR || specifier == INT || specifier == LONG);
_length = length;
}
/*
* Function: Type::Type (constructor)
*
* Description: Initialize this type object as a function type.
*/
Type::Type(int specifier, unsigned indirection, Parameters *parameters)
: _kind(FUNCTION), _specifier(specifier), _indirection(indirection)
{
assert(specifier == CHAR || specifier == INT || specifier == LONG);
_parameters = parameters;
}
/*
* Function: Type::kind (accessor)
*
* Description: Return the kind of this type.
*/
int Type::kind() const
{
return _kind;
}
/*
* 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 a better way than calling assert? There
* certainly isn't an easier way.
*/
unsigned long 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::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 &that) const
{
if (_kind != that._kind)
return false;
if (_kind == ERROR)
return true;
if (_specifier != that._specifier || _indirection != that._indirection)
return false;
if (_kind == SCALAR)
return true;
if (_kind == ARRAY)
return _length == that._length;
assert(_kind == FUNCTION && _parameters && that._parameters);
if (_parameters->variadic != that._parameters->variadic)
return false;
return _parameters->types == that._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 &that) const
{
return !operator ==(that);
}
/*
* Function: Type::isScalar (predicate)
*
* Description: Return whether this type is a scalar type.
*/
bool Type::isScalar() const
{
return _kind == SCALAR;
}
/*
* Function: Type::isArray (predicate)
*
* Description: Return whether this type is an array type.
*/
bool Type::isArray() const
{
return _kind == ARRAY;
}
/*
* Function: Type::isFunction (predicate)
*
* Description: Return whether this type is a function type.
*/
bool Type::isFunction() const
{
return _kind == FUNCTION;
}
/*
* Function: Type::isNumeric (predicate)
*
* Description: Return whether this type is a numeric type, meaning it is
* simply char, int, or long.
*/
bool Type::isNumeric() const
{
return _kind == SCALAR && _indirection == 0;
}
/*
* Function: Type::isPointer (predicate)
*
* Description: Return whether this type is a pointer type, meaning it is a
* scalar type with indirection.
*/
bool Type::isPointer() const
{
return _kind == SCALAR && _indirection > 0;
}
/*
* Function: Type::isCompatibleWith (predicate)
*
* Description: Return whether this type is compatible with the given type.
* In Simple C, two types are compatible if they are both
* numeric types or are identical scalar types.
*/
bool Type::isCompatibleWith(const Type &that) const
{
return (isNumeric() && that.isNumeric()) || (isScalar() && *this == that);
}
/*
* Function: Type::decay
*
* Description: Return the result of performing type decay on this type.
* In Simple C, an array is decayed to a pointer type.
*/
Type Type::decay() const
{
if (_kind == ARRAY)
return Type(_specifier, _indirection + 1);
return *this;
}
/*
* Function: Type::promote
*
* Description: Return the result of performing arithmetic promotion on
* this type. In Simple C, a character is promoted to an
* integer.
*/
Type Type::promote() const
{
if (_kind == SCALAR && _indirection == 0 && _specifier == CHAR)
return Type(INT);
return *this;
}
/*
* Function: Type::dereference
*
* Description: Return the result of dereferencing this type, which must be
* a pointer type.
*/
Type Type::dereference() 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)
{
if (type.kind() == ERROR)
ostr << "error";
else {
if (type.specifier() == CHAR)
ostr << "char";
else if (type.specifier() == INT)
ostr << "int";
else if (type.specifier() == LONG)
ostr << "long";
else
ostr << "unknown";
if (type.indirection() > 0)
ostr << " " << string(type.indirection(), '*');
if (type.kind() == ARRAY)
ostr << "[" << type.length() << "]";
else if (type.kind() == FUNCTION)
ostr << "()";
}
return ostr;
}