-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.h
713 lines (553 loc) · 17.6 KB
/
types.h
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// Copyright 2018 LPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// LPC type system
#pragma once
#include <map>
#include <assert.h>
#include "common.h"
#include "stringTable.h"
#include "compilationContext.h"
struct Scope;
// forward declarations from the obj namespace
//
namespace obj
{
struct Constant;
typedef std::list<Constant*> ConstList;
}
namespace ts
{
// forward declarations
//
class SubroutineType;
class Visitor;
///////////////////////////////////////////////////////////////////////////////
//
// Base class for the type system
//
class Type : private _DontCopy
{
public:
// extension point (for backend information for example)
//
VarPtr ext;
public:
virtual bool isPOD() const { return false; }
virtual bool isOrdinal() const { return false; }
virtual bool isInteger() const { return false; }
virtual bool isReal() const { return false; }
virtual bool isChar() const { return false; }
virtual bool isBool() const { return false; }
virtual bool isSet() const { return false; }
virtual bool isArray() const { return false; }
virtual bool isRecord() const { return false; }
virtual bool isString() const { return false; }
virtual bool isFile() const { return false; }
virtual bool isDummy() const { return false; }
virtual bool isSubroutine() const { return false; }
virtual bool isPointer() const { return false; }
virtual bool isRange() const { return false; }
virtual bool isEnum() const { return false; }
virtual int minValue() const { throw Exception("min value is not defined for this type"); }
virtual int maxValue() const { throw Exception("min value is not defined for this type"); }
void setIdentifier(Identifier* pId)
{
// NOTE: it uses the first assigned type name
// (multiple type aliases can reference the same actual type)
//
if(nullptr == m_pId)
{
m_pId = pId;
}
}
// brief type name (ex. for error messages or generating anonymous names)
//
virtual std::string typeDef() const = 0;
// unique (within scope) type name
//
virtual std::string typeId() const
{
// CONSIDER: I'm not sure I like the mutable for m_typeId
//
if(m_typeId.empty())
{
std::stringstream id;
if(nullptr != m_pId)
{
id << m_pId->name;
}
else
{
id << typeDef() << "_" << m_uniqueId++;
}
m_typeId = id.str();
}
return m_typeId;
}
virtual void resolve() {}
int line() const { return m_line; }
Scope* scope() const { return m_pScope; }
bool canBeAssigned(const Type* pSrcType) const;
// are they exactly the same type?
//
virtual bool isSameType(const Type* pType) const
{
return this == pType || this->isDummy() || pType->isDummy();
}
virtual bool isCompatible(const Type* pType) const
{
// NOTE: this is an ad-hoc definition of "compatible" and
// it may require some refinements to match the real language definition
//
return this->canBeAssigned(pType) || pType->canBeAssigned(this);
}
virtual bool isNumber() const
{
return isInteger() || isReal();
}
template<class T>
T* as()
{
return dynamic_cast<T*>(this);
}
template<class T>
const T* as() const
{
return dynamic_cast<const T*>(this);
}
template<class T>
bool isA() const
{
return dynamic_cast<const T*>(this) != nullptr;
}
virtual VarPtr accept(Visitor*)
{
assert(!"unexpected");
return VarPtr();
}
protected:
Type(Scope* pScope, int line);
virtual ~Type() {}
private:
int m_line;
Identifier* m_pId;
mutable std::string m_typeId;
Scope* m_pScope;
private:
static long m_uniqueId;
};
typedef std::list<Type*> TypeList;
///////////////////////////////////////////////////////////////////////////////
//
// a dummy placeholder used when we hit errors
//
class DummyType : public Type
{
public:
DummyType(int line) : Type(nullptr, line) {}
public:
virtual bool isPOD() const { return true; }
virtual bool isOrdinal() const { return true; }
virtual bool isInteger() const { return true; }
virtual bool isReal() const { return true; }
virtual bool isChar() const { return true; }
virtual bool isBool() const { return true; }
virtual bool isSet() const { return true; }
virtual bool isString() const { return true; }
virtual bool isDummy() const { return true; }
virtual bool isSubroutine() const { return true; }
virtual bool isRecord() const { return true; }
virtual bool isFile() const { return true; }
virtual bool isArray() const { return true; }
virtual bool isPointer() const { return true; }
virtual bool isRange() const { return true; }
virtual bool isEnum() const { return true; }
virtual int minValue() const { return INT_MIN; }
virtual int maxValue() const { return INT_MAX; }
virtual std::string typeDef() const { return "<dummy>"; }
};
///////////////////////////////////////////////////////////////////////////////
//
class VoidType : public Type
{
public:
VoidType() : Type(nullptr, PREDEFINED_LOCATION) {}
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "void"; }
};
///////////////////////////////////////////////////////////////////////////////
//
// predefined integer type
//
class IntegerType : public Type
{
public:
IntegerType() : Type(nullptr, PREDEFINED_LOCATION) {}
public:
virtual bool isPOD() const { return true; }
virtual bool isOrdinal() const { return true; }
virtual bool isInteger() const { return true; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "integer"; }
virtual int minValue() const { return INT_MIN; }
virtual int maxValue() const { return INT_MAX; }
};
///////////////////////////////////////////////////////////////////////////////
//
// predefined char type
//
class CharType : public Type
{
public:
CharType() : Type(nullptr, PREDEFINED_LOCATION) {}
public:
virtual bool isPOD() const { return true; }
virtual bool isOrdinal() const { return true; }
virtual bool isChar() const { return true; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "char"; }
virtual int minValue() const { return 0; }
virtual int maxValue() const { return 255; }
};
///////////////////////////////////////////////////////////////////////////////
//
// predefined bool type
//
class BoolType : public Type
{
public:
BoolType() : Type(nullptr, PREDEFINED_LOCATION) {}
public:
virtual bool isPOD() const { return true; }
virtual bool isOrdinal() const { return true; }
virtual bool isBool() const { return true; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "boolean"; }
virtual int minValue() const { return 0; }
virtual int maxValue() const { return 1; }
};
///////////////////////////////////////////////////////////////////////////////
//
// predefined real type
//
class RealType : public Type
{
public:
RealType() : Type(nullptr, PREDEFINED_LOCATION) {}
public:
virtual bool isPOD() const { return true; }
virtual bool isReal() const { return true; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "real"; }
};
///////////////////////////////////////////////////////////////////////////////
//
class EnumType : public Type
{
public:
EnumType(IdList* pEnumIDs, Scope* pScope, int line);
public:
virtual bool isPOD() const { return true; }
virtual bool isOrdinal() const { return true; }
virtual bool isInteger() const { return true; }
virtual bool isEnum() const { return true; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "enum"; }
virtual int minValue() const { return 0; }
virtual int maxValue() const { return m_pEnumIDs->size() - 1; }
private:
IdList* m_pEnumIDs;
};
///////////////////////////////////////////////////////////////////////////////
//
class RangeType : public Type
{
public:
RangeType(obj::Constant* pStart, obj::Constant* pEnd, Scope* pScope, int line);
public:
virtual bool isOrdinal() const { return true; }
virtual bool isInteger() const { return baseType()->isInteger(); }
virtual bool isChar() const { return baseType()->isChar(); }
virtual bool isRange() const { return true; }
virtual std::string typeDef() const { return "range"; }
bool isValidStringIndex() const;
Type* baseType() const;
virtual VarPtr accept(Visitor* pVisitor);
virtual int minValue() const;
virtual int maxValue() const;
private:
obj::Constant* m_pStart;
obj::Constant* m_pEnd;
};
///////////////////////////////////////////////////////////////////////////////
//
class ArrayType : public Type
{
public:
ArrayType(TypeList* pDimTypes, Type* pElemType, Scope* pScope, int line) :
Type(pScope, line),
m_pIndexType(nullptr),
m_pElemType(nullptr)
{
// index type
//
m_pIndexType = pDimTypes->front();
pDimTypes->pop_front();
if(!m_pIndexType->isOrdinal())
context()->error(line, "index type must be ordinal");
// element type
//
m_pElemType = pDimTypes->empty() ?
pElemType :
new ArrayType(pDimTypes, pElemType, pScope, line);
assert(nullptr != m_pElemType);
}
virtual bool isArray() const { return true; }
virtual bool isString() const;
virtual void resolve()
{
m_pIndexType->resolve();
m_pElemType->resolve();
}
Type* elemType() const { return m_pElemType; }
Type* indexType() const { return m_pIndexType; }
int strLength() const;
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "array"; }
private:
Type* m_pIndexType;
Type* m_pElemType;
};
///////////////////////////////////////////////////////////////////////////////
//
class FileType : public Type
{
public:
FileType(Type* pElemType, Scope* pScope, int line) :
Type(pScope, line),
m_pElemType(pElemType)
{
}
virtual bool isFile() const { return true; }
virtual void resolve()
{
// TODO: look for invalid embedded types (pointers, files)
//
m_pElemType->resolve();
}
Type* elemType() const { return m_pElemType; }
bool isText() const { return m_pElemType->isA<CharType>(); }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return isText() ? "text" : "file"; }
private:
Type* m_pElemType;
};
///////////////////////////////////////////////////////////////////////////////
//
class SetType : public Type
{
public:
SetType(Type* pElemType, Scope* pScope, int line) :
Type(pScope, line),
m_pElemType(pElemType)
{
if(!pElemType->isOrdinal())
context()->error(line, "set element type must be ordinal");
}
virtual void resolve()
{
m_pElemType->resolve();
}
virtual bool isSet() const { return true; }
Type* elemType() const { return m_pElemType; }
virtual int minValue() const { return max(0, m_pElemType->minValue()); }
virtual int maxValue() const { return min(255, m_pElemType->maxValue()); }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "set"; }
private:
Type* m_pElemType;
};
///////////////////////////////////////////////////////////////////////////////
//
// record type building blocks
//
struct FieldSet
{
IdList* pNames;
Type* pType;
FieldSet(IdList* pNames, Type* pType) :
pNames(pNames), pType(pType) {}
};
typedef std::list<FieldSet*> FixedFieldList;
struct VariantSelector
{
Identifier* pId;
Type* pType;
VariantSelector(Identifier* pId, Type* pType) :
pId(pId), pType(pType) {}
};
struct RecordFields;
struct VariantSection
{
obj::ConstList* pConstants;
RecordFields* pFields;
VariantSection(obj::ConstList* pConstants, RecordFields* pFields) :
pConstants(pConstants), pFields(pFields) {}
};
typedef std::list<VariantSection*> VariantFieldList;
struct VariableFields
{
VariantSelector* pVariantSelector;
VariantFieldList* pVariantFields;
VariableFields(VariantSelector* pVariantSelector, VariantFieldList* pVariantFields) :
pVariantSelector(pVariantSelector), pVariantFields(pVariantFields) {}
};
struct RecordFields
{
FixedFieldList* pFixedFields;
VariableFields* pVariableFields;
RecordFields(FixedFieldList* pFixedFields, VariableFields* pVariableFields) :
pFixedFields(pFixedFields), pVariableFields(pVariableFields) {}
RecordFields* getVariant(const obj::Constant* pConst) const;
};
///////////////////////////////////////////////////////////////////////////////
//
class RecordType : public Type
{
public:
typedef std::map<Identifier*, Type*, IdCmpLess> FieldMap;
public:
RecordType(RecordFields* pFields, Scope* pScope, int line);
virtual bool isRecord() const { return true; }
virtual void resolve();
const FieldMap& fieldMap() const { return m_fieldMap; }
const RecordFields* fields() const { return m_pFields; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "record"; }
private:
void _addFieldMapEntry(Identifier* pId, Type* pType);
void _populateFieldMap(const RecordFields* pFields);
private:
RecordFields* m_pFields;
FieldMap m_fieldMap;
};
///////////////////////////////////////////////////////////////////////////////
//
class PointerType : public Type
{
public:
PointerType(Identifier* pTypeId, Scope* pScope, int line);
PointerType(Type* pBaseType, Scope* pScope, int line) :
Type(pScope, line),
m_pTypeId(nullptr),
m_pBaseType(pBaseType)
{
}
public:
virtual bool isPointer() const { return true; }
virtual void resolve();
Type* baseType() const { return m_pBaseType; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "pointer"; }
private:
Identifier* m_pTypeId;
Type* m_pBaseType;
};
///////////////////////////////////////////////////////////////////////////////
//
// function type building blocks
//
// CONSIDER: there's a significant overlap with obj::Parameter!
//
struct ParamSet
{
IdList* pNames;
Type* pType;
bool byRef;
ParamSet(IdList* pNames, Type* pType, bool byRef) :
pNames(pNames), pType(pType), byRef(byRef)
{
assert(!pNames->empty());
// for parameter definitions the type must be resolved immediately
//
pType->resolve();
}
ParamSet(Identifier* pId, SubroutineType* pSubroutinesType);
};
struct Param
{
Identifier* pId;
Type* pType;
bool byRef;
public:
Param(Identifier* pId, Type* pType, bool byRef) : pId(pId), pType(pType), byRef(byRef) {}
};
typedef std::list<ParamSet*> ParamSetList;
typedef std::list<Param> ParamList;
///////////////////////////////////////////////////////////////////////////////
//
class SubroutineType : public Type
{
public:
SubroutineType(ParamSetList* pParamSetList, Type* pReturnType, Scope* pScope, int line);
public:
virtual bool isSubroutine() const { return true; }
virtual bool isSameType(const Type* pType) const;
bool isFunction() const { return m_pReturnType != nullptr; }
Type* returnType() const { return m_pReturnType; }
const ParamList* paramList() const { return &m_paramList; }
virtual VarPtr accept(Visitor* pVisitor);
virtual std::string typeDef() const { return "subroutine"; }
private:
ParamList m_paramList;
Type* m_pReturnType;
};
///////////////////////////////////////////////////////////////////////////////
//
// this is the interface for types duble dispatch visitors
//
class Visitor
{
public:
virtual ~Visitor() {}
public:
virtual VarPtr visit(VoidType* pType) = 0;
virtual VarPtr visit(IntegerType* pType) = 0;
virtual VarPtr visit(CharType* pType) = 0;
virtual VarPtr visit(BoolType* pType) = 0;
virtual VarPtr visit(RealType* pType) = 0;
virtual VarPtr visit(EnumType* pType) = 0;
virtual VarPtr visit(RangeType* pType) = 0;
virtual VarPtr visit(ArrayType* pType) = 0;
virtual VarPtr visit(FileType* pType) = 0;
virtual VarPtr visit(SetType* pType) = 0;
virtual VarPtr visit(RecordType* pType) = 0;
virtual VarPtr visit(PointerType* pType) = 0;
virtual VarPtr visit(SubroutineType* pType) = 0;
};
///////////////////////////////////////////////////////////////////////////////
//
// predefined types
//
// CONSIDER: encapsulate these globals
//
extern VoidType* g_pVoidType;
extern IntegerType* g_pIntegerType;
extern CharType* g_pCharType;
extern BoolType* g_pBoolType;
extern RealType* g_pRealType;
extern FileType* g_pTextType;
extern SetType* g_pGenericSetType;
void predefineTypes();
} // end namespace ts