Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r368]: cmajor++ / Cm / Cm.Core / DerivedType.cpp Maximize Restore History

Download this file

DerivedType.cpp    544 lines (492 with data), 16.9 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
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
/*========================================================================
Copyright (c) 2012-2013 Seppo Laakko
http://sourceforge.net/projects/cmajor/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#include <Cm.Core/DerivedType.hpp>
#include <Cm.Core/BasicTypeFun.hpp>
#include <Cm.Core/Assert.hpp>
#include <Cm.Core/Namespace.hpp>
#include <Cm.Core/Clone.hpp>
#include <Cm.Core/Util.hpp>
#include <Cm.Core/TypeChecker.hpp>
#include <Cm.Core/Error.hpp>
#include <Cm.Core/Object.hpp>
#include <Cm.Core/Pointer.hpp>
namespace Cm { namespace Core {
class ReferenceTypeDirectory;
typedef std::shared_ptr<ReferenceTypeDirectory> ReferenceTypeDirectoryPtr;
class ReferenceTypeDirectory
{
public:
static void Init();
static void Done();
static ReferenceTypeDirectoryPtr Instance();
~ReferenceTypeDirectory();
TypePtr GetReferenceType(const std::string& referenceTypeName) const;
void AddReferenceType(TypePtr referenceType);
private:
static ReferenceTypeDirectoryPtr instance;
static std::stack<ReferenceTypeDirectoryPtr> instanceStack;
ReferenceTypeDirectory();
typedef std::map<std::string, TypePtr> ReferenceTypeMap;
typedef ReferenceTypeMap::const_iterator ReferenceTypeMapIt;
ReferenceTypeMap referenceTypeMap;
};
void ReferenceTypeDirectory::Init()
{
instanceStack.push(instance);
instance = ReferenceTypeDirectoryPtr(new ReferenceTypeDirectory());
}
void ReferenceTypeDirectory::Done()
{
instance = instanceStack.top();
instanceStack.pop();
}
ReferenceTypeDirectoryPtr ReferenceTypeDirectory::Instance()
{
CM_CORE_ASSERT(instance, nullptr);
return instance;
}
ReferenceTypeDirectory::ReferenceTypeDirectory()
{
}
ReferenceTypeDirectory::~ReferenceTypeDirectory()
{
ReferenceTypeMapIt e = referenceTypeMap.end();
for (ReferenceTypeMapIt i = referenceTypeMap.begin(); i != e; ++i)
{
TypePtr r = i->second;
r->Dispose();
}
referenceTypeMap.clear();
}
ReferenceTypeDirectoryPtr ReferenceTypeDirectory::instance;
std::stack<ReferenceTypeDirectoryPtr> ReferenceTypeDirectory::instanceStack;
TypePtr ReferenceTypeDirectory::GetReferenceType(const std::string& referenceTypeName) const
{
ReferenceTypeMapIt i = referenceTypeMap.find(referenceTypeName);
if (i != referenceTypeMap.end())
{
return i->second;
}
return ReferenceTypePtr();
}
void ReferenceTypeDirectory::AddReferenceType(TypePtr referenceType)
{
referenceTypeMap[referenceType->FullName()] = referenceType;
}
DerivedType::DerivedType(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, int id_, const ObjectPtr& baseTypeExpr_):
Type(name_, pos_, enclosingScope_, fileScope_, id_, USER_RANK), baseTypeExpr(baseTypeExpr_)
{
}
void DerivedType::SetFunction(Function* function)
{
baseTypeExpr->SetFunction(function);
}
void DerivedType::Dispose()
{
if (!Disposed())
{
Type::Dispose();
if (baseTypeExpr)
{
if (!baseTypeExpr->IsShared())
{
baseTypeExpr->Dispose();
}
baseTypeExpr.reset();
}
}
}
void DerivedType::TypeCheckName(TypeCheckContext& context)
{
if (!NameComputed())
{
Type::TypeCheckName(context);
TypeChecker::TypeCheck(baseTypeExpr, context);
baseType = baseTypeExpr->GetType();
}
}
void DerivedType::TypeCheckSignature(TypeCheckContext& context)
{
Type::TypeCheckSignature(context);
TypeChecker::TypeCheck(baseTypeExpr, context);
baseType = baseTypeExpr->GetType();
if (!baseType.lock())
{
throw TypeCheckException(OBJECT_DOES_NOT_DENOTE_TYPE, "object '" + baseTypeExpr->FullName() + "' does not denote a type", baseTypeExpr->Pos());
}
if (baseType.lock()->IsTemplate())
{
throw TypeCheckException(TEMPLATE_ARGUMENTS_MISSING, "template arguments missing for '" + baseType.lock()->Name() + "'", Pos());
}
}
void DerivedType::TypeCheckBody(TypeCheckContext& context)
{
Type::TypeCheckBody(context);
GetBaseType()->TypeCheck(context);
}
ObjectPtr DerivedType::SubstituteTemplateParams(const BoundTemplateParamMap& boundTemplateParamMap, bool& substituted)
{
ObjectPtr substitute = baseTypeExpr->SubstituteTemplateParams(boundTemplateParamMap, substituted);
if (substitute)
{
baseTypeExpr = substitute;
}
if (substituted)
{
SetTypeCheckingPhase(TypeCheckingPhase::not_typechecked);
}
return ObjectPtr();
}
void DerivedType::CollectExternalObjects(CompilationUnitPtr compilationUnit)
{
baseType.lock()->CollectExternalObjects(compilationUnit);
}
ConstType::ConstType(const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, const ObjectPtr& baseTypeExpr_):
DerivedType("const", pos_, enclosingScope_, fileScope_, GetNextTypeId(), baseTypeExpr_)
{
}
ObjectPtr ConstType::Clone(CloneContext& context) const
{
return ObjectPtr(new ConstType(Pos(), context.CurrentScope(), FileScope(), context.CloneTypeExpr(GetBaseTypeExpr())));
}
void ConstType::TypeCheckName(TypeCheckContext& context)
{
DerivedType::TypeCheckName(context);
SetName("const " + (GetBaseType() ? GetBaseType()->FullName() : GetBaseTypeExpr()->FullName()));
}
void ConstType::TypeCheckSignature(TypeCheckContext& context)
{
DerivedType::TypeCheckSignature(context);
SetName("const " + GetBaseType()->FullName());
SetEnclosingScope(GetBaseType()->EnclosingScope()->Ns()->GetScope());
TypePtr baseType = GetBaseType();
SetIrType(baseType->IrType());
}
std::string ConstType::FullName() const
{
std::string fullName = "const " + (GetBaseType() ? GetBaseType()->FullName() : GetBaseTypeExpr()->FullName());
SetFullNameCache(fullName);
return fullName;
}
std::string ConstType::TypeDocName() const
{
return GetBaseTypeExpr()->TypeDocName() + ".const";
}
std::string ConstType::ShortTypeDocName() const
{
return "const " + GetBaseTypeExpr()->ShortTypeDocName();
}
std::string ConstType::AliasOrShortTypeDocName() const
{
return "const " + GetBaseTypeExpr()->AliasOrShortTypeDocName();
}
ReferenceType::ReferenceType(const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, const ObjectPtr& baseTypeExpr_):
DerivedType("&", pos_, enclosingScope_, fileScope_, GetNextTypeId(), baseTypeExpr_)
{
}
ObjectPtr ReferenceType::Clone(CloneContext& context) const
{
return ObjectPtr(new ReferenceType(Pos(), context.CurrentScope(), FileScope(), context.CloneTypeExpr(GetBaseTypeExpr())));
}
bool ReferenceType::IsRefToClassType() const
{
TypePtr baseType = GetBaseType();
if (baseType->IsConstType())
{
baseType = baseType->GetBaseType();
}
return baseType->IsClassType();
}
std::string ReferenceType::ToString() const
{
TypePtr baseType = GetBaseType();
if (baseType)
{
if (baseType->IsConstType())
{
TypePtr constType = std::static_pointer_cast<ConstType>(baseType);
baseType = constType->GetBaseType();
if (baseType)
{
return "const " + baseType->ToString() + "&";
}
else
{
ObjectPtr baseTypeExpr = constType->GetBaseTypeExpr();
return "const " + baseTypeExpr->ToString() + "&";
}
}
}
else
{
ObjectPtr baseTypeExpr = GetBaseTypeExpr();
if (baseTypeExpr->IsConstType())
{
TypePtr constType = std::static_pointer_cast<ConstType>(baseType);
baseType = constType->GetBaseType();
if (baseType)
{
return "const " + baseType->ToString() + "&";
}
else
{
ObjectPtr baseTypeExpr = constType->GetBaseTypeExpr();
return "const " + baseTypeExpr->ToString() + "&";
}
}
}
return (GetBaseType() ? GetBaseType()->ToString() : GetBaseTypeExpr()->ToString()) + "&";
}
std::string ReferenceType::FullName() const
{
TypePtr baseType = GetBaseType();
if (baseType)
{
if (baseType->IsConstType())
{
TypePtr constType = std::static_pointer_cast<ConstType>(baseType);
baseType = constType->GetBaseType();
if (baseType)
{
std::string fullName = "const " + baseType->FullName() + "&";
SetFullNameCache(fullName);
return fullName;
}
else
{
ObjectPtr baseTypeExpr = constType->GetBaseTypeExpr();
std::string fullName = "const " + baseTypeExpr->FullName() + "&";
SetFullNameCache(fullName);
return fullName;
}
}
}
else
{
ObjectPtr baseTypeExpr = GetBaseTypeExpr();
if (baseTypeExpr->IsConstType())
{
TypePtr constType = std::static_pointer_cast<ConstType>(baseType);
baseType = constType->GetBaseType();
if (baseType)
{
std::string fullName = "const " + baseType->FullName() + "&";
SetFullNameCache(fullName);
return fullName;
}
else
{
ObjectPtr baseTypeExpr = constType->GetBaseTypeExpr();
std::string fullName = "const " + baseTypeExpr->FullName() + "&";
SetFullNameCache(fullName);
return fullName;
}
}
}
std::string fullName = (GetBaseType() ? GetBaseType()->FullName() : GetBaseTypeExpr()->FullName()) + "&";
SetFullNameCache(fullName);
return fullName;
}
void ReferenceType::TypeCheckName(TypeCheckContext& context)
{
DerivedType::TypeCheckName(context);
TypePtr baseType = GetBaseType();
if (baseType)
{
if (baseType->IsConstType())
{
TypePtr baseBaseType = baseType->GetBaseType();
if (baseBaseType)
{
SetName("const " + baseBaseType->FullName() + "&");
}
else
{
SetName("const " + baseType->GetBaseTypeExpr()->FullName() + "&");
}
}
else
{
SetName(baseType->FullName() + "&");
}
}
else
{
ObjectPtr baseTypeExpr = GetBaseTypeExpr();
if (baseTypeExpr->IsConstType())
{
baseTypeExpr = baseTypeExpr->GetBaseTypeExpr();
SetName("const " + baseTypeExpr->FullName() + "&");
}
else
{
SetName(baseTypeExpr->FullName() + "&");
}
}
}
void ReferenceType::TypeCheckSignature(TypeCheckContext& context)
{
DerivedType::TypeCheckSignature(context);
TypePtr foundType = ReferenceTypeDirectory::Instance()->GetReferenceType(FullName());
if (foundType)
{
SetId(foundType->Id());
SetEnclosingScope(foundType->EnclosingScope());
SetBaseType(foundType->GetBaseType());
SetIrType(foundType->IrType());
SetName(foundType->Name());
}
else
{
SetEnclosingScope(GetBaseType()->EnclosingScope()->Ns()->GetScope());
if (GetBaseType()->IrType())
{
SetIrType(Llvm::Ir::Pointer(GetBaseType()->IrType()));
}
ReferenceTypePtr thisType = std::static_pointer_cast<ReferenceType>(shared_from_this());
ReferenceTypeDirectory::Instance()->AddReferenceType(thisType);
SetId(GetNextTypeId());
TypePtr baseType = GetBaseType();
if (baseType->IsConstType())
{
baseType = baseType->GetBaseType();
SetName("const " + baseType->FullName() + "&");
}
else
{
SetName(GetBaseType()->FullName() + "&");
}
AddToScope(false);
bool createCopyCtorAndAssignment = true;
if (baseType->IsTemplateParameter())
{
createCopyCtorAndAssignment = false;
}
else if (GetBaseType()->IsPointerType())
{
PointerTypePtr ptrType = std::static_pointer_cast<PointerType>(GetBaseType());
TypePtr baseType = ptrType->GetBaseType();
if (baseType->IsReferenceType() || baseType->IsPointerType())
{
createCopyCtorAndAssignment = false;
}
}
if (createCopyCtorAndAssignment)
{
FunctionPtr referenceCopyCtor = CreateCopyCtor(thisType);
std::vector<FunctionPtr> referenceOps;
referenceOps.push_back(referenceCopyCtor);
FunctionPtr plainRefCtor = CreatePlainRefCtor(thisType, baseType);
referenceOps.push_back(plainRefCtor);
FunctionPtr referenceAssignment = CreateAssignmentOp(thisType);
referenceOps.push_back(referenceAssignment);
AddFunctionsToScope(referenceOps);
}
}
}
std::string ReferenceType::TypeDocName() const
{
return GetBaseTypeExpr()->TypeDocName() + ".ref";
}
std::string ReferenceType::ShortTypeDocName() const
{
return GetBaseTypeExpr()->ShortTypeDocName() + "&";
}
std::string ReferenceType::AliasOrShortTypeDocName() const
{
return GetBaseTypeExpr()->AliasOrShortTypeDocName() + "&";
}
TypePtr MakeConstType(const ObjectPtr& baseTypeExpr, TypeCheckExtent typeCheckExtent)
{
if (baseTypeExpr->IsConstType())
{
return std::static_pointer_cast<Type>(baseTypeExpr);
}
TypePtr type;
ObjectPtr baseType = baseTypeExpr;
type = TypePtr(new ConstType(baseType->Pos(), baseType->EnclosingScope(), baseType->FileScope(), baseType));
TypeCheckContext context(typeCheckExtent);
type->TypeCheck(context);
return type;
}
TypePtr MakeReferenceType(const ObjectPtr& baseTypeExpr, TypeCheckExtent typeCheckExtent)
{
if (baseTypeExpr->IsReferenceType())
{
return std::static_pointer_cast<Type>(baseTypeExpr);
}
TypePtr type(new ReferenceType(baseTypeExpr->Pos(), baseTypeExpr->EnclosingScope(), baseTypeExpr->FileScope(), baseTypeExpr));
TypeCheckContext context(typeCheckExtent);
type->TypeCheck(context);
return type;
}
TypePtr MakeConstReferenceType(const ObjectPtr& baseTypeExpr, TypeCheckExtent typeCheckExtent)
{
if (baseTypeExpr->IsConstReferenceType())
{
return std::static_pointer_cast<Type>(baseTypeExpr);
}
TypePtr type;
if (baseTypeExpr->IsReferenceType())
{
TypePtr baseType = baseTypeExpr->GetBaseType();
type = MakeReferenceType(MakeConstType(baseType, typeCheckExtent), typeCheckExtent);
}
else
{
type = MakeReferenceType(MakeConstType(baseTypeExpr, typeCheckExtent), typeCheckExtent);
}
TypeCheckContext context(typeCheckExtent);
type->TypeCheck(context);
return type;
}
TypePtr MakeConstPointerType(const ObjectPtr& baseTypeExpr, TypeCheckExtent typeCheckExtent)
{
TypePtr constType(new ConstType(baseTypeExpr->Pos(), baseTypeExpr->EnclosingScope(), baseTypeExpr->FileScope(), baseTypeExpr));
TypePtr ptrType = MakePointerType(constType, typeCheckExtent);
return ptrType;
}
bool HasConsts(const TypePtr& type)
{
if (type->IsConstType())
{
return true;
}
if (type->IsPointerType())
{
return HasConsts(type->GetBaseType());
}
return false;
}
TypePtr DropConsts(const TypePtr& type, int& numConsts)
{
if (HasConsts(type))
{
if (type->IsConstType())
{
++numConsts;
return type->GetBaseType();
}
if (type->IsPointerType())
{
TypePtr ptrType = PointerTypePtr(new PointerType("*", type->Pos(), type->EnclosingScope(), type->FileScope(), DropConsts(type->GetBaseType(), numConsts)));
TypeCheckContext signature(TypeCheckExtent::signature);
ptrType->TypeCheck(signature);
return ptrType;
}
}
return type;
}
void DerivedTypeInit()
{
ReferenceTypeDirectory::Init();
}
void DerivedTypeDone()
{
ReferenceTypeDirectory::Done();
}
} } // namespace Cm::Core