[r302]: cmajor++ / Cm / Cm.Core / Util.cpp Maximize Restore History

Download this file

Util.cpp    746 lines (693 with data), 22.7 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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*========================================================================
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/Util.hpp>
#include <Cm.Core/Pointer.hpp>
#include <Cm.Core/Namespace.hpp>
#include <Cm.Core/BasicType.hpp>
#include <Cm.Core/Assert.hpp>
#include <Cm.Core/Error.hpp>
#include <Cm.Core/Function.hpp>
#include <Cm.Core/Variable.hpp>
#include <Cm.Core/Class.hpp>
#include <Cm.Core/Temporary.hpp>
#include <Cm.Core/Concept.hpp>
#include <Soul.Util/TextUtils.hpp>
#include <map>
namespace Cm { namespace Core {
using Soul::Util::IntToString;
class Factory;
typedef std::shared_ptr<Factory> FactoryPtr;
class Factory
{
public:
static void Init();
static void Done();
static FactoryPtr Instance();
int GetNextTypeId() { return nextTypeId++; }
std::string GetNextTempVarName() { return "$" + IntToString(nextTempVarNumber++); }
void ResetTempVarCounter() { nextTempVarNumber = 0; }
std::string GetNextTempTypeName() { return "__tempType" + IntToString(nextTempTypeNumber++); }
std::string GetNextArchetypeSetName() { return "Archetype" + IntToString(nextArchetypeSetNumber++); }
std::string GetNextStringValueName() { return ".s" + IntToString(nextStringNumber++); }
void ResetStringValueCounter() { nextStringNumber = 0; }
private:
static FactoryPtr instance;
static std::stack<FactoryPtr> instanceStack;
Factory(): nextTypeId(FIRST_USER_TYPEID), nextTempVarNumber(0), nextTempTypeNumber(0), nextArchetypeSetNumber(0), nextStringNumber(0) {}
int nextTypeId;
int nextTempVarNumber;
int nextTempTypeNumber;
int nextArchetypeSetNumber;
int nextStringNumber;
};
FactoryPtr Factory::instance;
std::stack<FactoryPtr> Factory::instanceStack;
void Factory::Init()
{
instanceStack.push(instance);
instance = FactoryPtr(new Factory());
}
void Factory::Done()
{
instance = instanceStack.top();
instanceStack.pop();
}
FactoryPtr Factory::Instance()
{
CM_CORE_ASSERT(instance);
return instance;
}
int GetNextTypeId()
{
return Factory::Instance()->GetNextTypeId();
}
std::string GetNextTempVarName()
{
return Factory::Instance()->GetNextTempVarName();
}
void ResetTempVarCounter()
{
Factory::Instance()->ResetTempVarCounter();
}
std::string GetNextTempTypeName()
{
return Factory::Instance()->GetNextTempTypeName();
}
std::string GetNextArchetypeSetName()
{
return Factory::Instance()->GetNextArchetypeSetName();
}
std::string GetNextStringValueName()
{
return Factory::Instance()->GetNextStringValueName();
}
void ResetStringValueCounter()
{
Factory::Instance()->ResetStringValueCounter();
}
std::string GetParameterListStr(const ParameterList& parameters, int start, TypeCheckContext& context)
{
std::string parameterListStr;
int n = parameters.size();
bool first = true;
for (int i = start; i < n; ++i)
{
ParameterPtr parameter = parameters[i];
if (first)
{
first = false;
}
else
{
parameterListStr.append(", ");
}
parameter->TypeCheck(context);
ObjectPtr typeExpr = parameter->TypeExpr();
TypePtr type = typeExpr->GetType();
std::string typeName = type ? type->FullTypeName() : typeExpr->FullName();
parameterListStr.append(typeName + " " + parameter->Name());
}
return parameterListStr;
}
std::string GetParameterListStr(const ParameterList& parameters, TypeCheckContext& context)
{
return GetParameterListStr(parameters, 0, context);
}
void CheckAccessSpecifiers(const ObjectPtr& forObject, Specifiers specifiers)
{
Specifiers accessSpecifiers = Specifiers(specifiers & Specifiers::access);
bool classLevelObject = forObject->EnclosingScope()->GetKind() == ScopeKind::class_;
if (accessSpecifiers == Specifiers::none)
{
if (classLevelObject)
{
specifiers = Specifiers(specifiers | Specifiers::private_);
}
else
{
specifiers = Specifiers(specifiers | Specifiers::internal_);
}
}
else
{
if (classLevelObject)
{
if (accessSpecifiers != Specifiers::public_ && accessSpecifiers != Specifiers::protected_ && accessSpecifiers != Specifiers::private_ &&
accessSpecifiers != Specifiers::internal_ && accessSpecifiers != Specifiers::protected_internal)
{
throw TypeCheckException(INVALID_ACCESSSPECIFIERS, "invalid combination of access specifiers", forObject->Pos());
}
}
else if (accessSpecifiers != Specifiers::public_ && accessSpecifiers != Specifiers::internal_)
{
throw TypeCheckException(INVALID_NAMESPACE_LEVEL_ACCESSSPECIFIERS, "namespace-level entity can have only 'public' or 'internal' access", forObject->Pos());
}
}
}
std::string MakeTemplateInstanceName(const ObjectPtr& subject, const ExpressionList& templateArgs, bool fullName, TypeCheckContext& context)
{
std::string subjectName = fullName ? subject->FullName() : subject->Name();
if (subject->IsClassType())
{
ClassTypePtr classType = std::static_pointer_cast<ClassType>(subject);
if (classType->IsInstantiatedFromTemplate() && subject->Name() != classType->BaseName())
{
return subjectName;
}
if (classType->IsTemplate())
{
subjectName = fullName ? classType->FullBaseName() : classType->BaseName();
}
}
FunctionPtr function;
if (subject->IsFunction())
{
function = std::static_pointer_cast<Function>(subject);
subjectName = fullName ? function->FullFunctionSetName() : function->FunctionSetName();
}
std::string templateInstanceName(subjectName + "<");
int n = templateArgs.size();
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
templateInstanceName.append(", ");
}
ObjectPtr templateArg = templateArgs[i];
TypeChecker::TypeCheck(templateArg, context);
if (templateArg->IsTemplateParameter() || templateArg->GetType() && templateArg->GetType()->IsTypenameType())
{
templateInstanceName.append(templateArg->Name());
}
else if (templateArg->IsTypeExpr() || templateArg->IsMember() || templateArg->IsTemplateName())
{
TypePtr typeParam = templateArg->GetType();
if (typeParam)
{
templateInstanceName.append(typeParam->FullName());
}
else
{
templateInstanceName.append(templateArg->FullName());
}
}
else
{
templateInstanceName.append(templateArg->Name());
}
}
templateInstanceName.append(">");
if (function)
{
templateInstanceName.append("(" + GetParameterListStr(function->Parameters(), context) + ")");
}
return templateInstanceName;
}
std::string GetConstructorFunctionSetName()
{
return "@constructor";
}
std::string GetDestructorFunctionSetName()
{
return "@destructor";
}
std::string GetStaticConstructorFunctionSetName()
{
return "@static_constructor";
}
std::string MakeDefaultConstructorName(TypePtr type)
{
return GetConstructorFunctionSetName() + "(" + MakePointerType(type, TypeCheckExtent::name)->Name() + ")";
}
std::string MakeConversionName(const std::string& sourceTypeName, const std::string& targetTypeName)
{
return sourceTypeName + " -> " + targetTypeName;
}
void AppendParameterTypeNames(std::string& name, const ParameterList& parameters)
{
int n = parameters.size();
for (int i = 0; i < n; ++i)
{
if (i > 0)
{
name.append(", ");
}
TypePtr type = parameters[i]->GetType();
ObjectPtr typeExpr = parameters[i]->TypeExpr();
std::string typeName = type ? type->FullName() : typeExpr->FullName();
name.append(typeName);
}
}
std::string MakeConversionFunctionName(const TypePtr& type, const ParameterList& parameters)
{
std::string name("@operator_");
name.append(type->FullName());
name.append(1, '(');
AppendParameterTypeNames(name, parameters);
name.append(1, ')');
return name;
}
std::string MakeConversionFunctionName(const ObjectPtr& typeExpr, const ParameterList& parameters)
{
std::string name("@operator_");
name.append(typeExpr->Name());
name.append(1, '(');
AppendParameterTypeNames(name, parameters);
name.append(1, ')');
return name;
}
std::string MakeOverloadName(const std::string& functionSetName, const ParameterList& parameters)
{
std::string name(functionSetName);
name.append("(");
AppendParameterTypeNames(name, parameters);
name.append(")");
return name;
}
std::string MakeOverloadName(const std::string& functionSetName, const ArgumentVector& arguments)
{
std::string name(functionSetName);
name.append("(");
int n = arguments.size();
for (int i = 0; i < n; ++i)
{
if (i > 0)
{
name.append(", ");
}
Argument arg = arguments[i];
TypePtr type = arg.Type();
name.append(type->FullName());
}
name.append(")");
return name;
}
std::string MakeConstructorName(const std::string& className, const ParameterList& parameters)
{
std::string name(GetConstructorFunctionSetName());
name.append(1, '(');
AppendParameterTypeNames(name, parameters);
name.append(1, ')');
return name;
}
std::string MakeDestructorName(const std::string& className, const ParameterList& parameters)
{
std::string name(GetDestructorFunctionSetName());
name.append(1, '(');
AppendParameterTypeNames(name, parameters);
name.append(1, ')');
return name;
}
std::string MakeStaticConstructorName(const std::string& className)
{
std::string name(GetStaticConstructorFunctionSetName());
name.append(1, '(');
name.append(className);
name.append(1, ')');
return name;
}
class MangleMap;
typedef std::shared_ptr<MangleMap> MangleMapPtr;
class MangleMap
{
public:
static void Init();
static void Done();
static MangleMapPtr Instance();
std::string Mangle(const std::string& name)
{
MapIt i = map.find(name);
if (i != map.end())
{
return "__" + i->second;
}
return name;
}
std::string Plain(const std::string& name)
{
MapIt i = map.find(name);
if (i != map.end())
{
return i->second;
}
return name;
}
private:
static MangleMapPtr instance;
static int initCount;
typedef std::map<std::string, std::string> Map;
typedef Map::const_iterator MapIt;
Map map;
MangleMap()
{
map["operator+"] = "pl";
map["operator-"] = "mi";
map["operator*"] = "tm";
map["operator/"] = "di";
map["operator%"] = "mo";
map["operator=="] = "eq";
map["operator<"] = "ls";
map["operator!"] = "nt";
map["operator~"] = "cp";
map["operator&"] = "an";
map["operator|"] = "or";
map["operator^"] = "xo";
map["operator<<"] = "sl";
map["operator>>"] = "sr";
map["operator->"] = "pm";
map["operator++"] = "pp";
map["operator--"] = "mm";
map["operator="] = "as";
map["operator()"] = "ap";
map["operator[]"] = "ix";
map["@operator_conv"] = "cv";
map[GetConstructorFunctionSetName()] = "ct";
map[GetDestructorFunctionSetName()] = "dt";
map[GetStaticConstructorFunctionSetName()] = "sc";
}
};
MangleMapPtr MangleMap::instance;
int MangleMap::initCount = 0;
void MangleMap::Init()
{
if (initCount++ == 0)
{
instance = MangleMapPtr(new MangleMap());
}
}
void MangleMap::Done()
{
if (--initCount == 0)
{
instance.reset();
}
}
MangleMapPtr MangleMap::Instance()
{
CM_CORE_ASSERT(instance);
return instance;
}
std::string MakeAssemblyName(const std::string& className)
{
std::string assemblyName;
assemblyName.reserve(2 * className.length());
int n = className.size();
for (int i = 0; i < n; ++i)
{
char c = className[i];
switch (c)
{
case '&': assemblyName.append("_R"); break;
case '*': assemblyName.append("_P"); break;
case '<': assemblyName.append("$$"); break;
case '>': assemblyName.append("$$"); break;
case ',': assemblyName.append("."); break;
case ' ': break;
default: assemblyName.append(1, c); break;
}
}
return assemblyName;
}
std::string MangleName(const std::string& namespaceName, const std::string& functionSetName, const ExpressionList& templateArgs, const ParameterList& parameters)
{
std::string mangledNs = namespaceName;
int nm = mangledNs.size();
if (nm > 0)
{
mangledNs.append(1, '.');
}
std::string mangledName(mangledNs + MangleMap::Instance()->Mangle(functionSetName));
if (!templateArgs.empty())
{
mangledName.append("$$");
int n = templateArgs.size();
for (int i = 0; i < n; ++i)
{
if (i > 0)
{
mangledName.append(1, '.');
}
mangledName.append(MakeAssemblyName(templateArgs[i]->FullName()));
}
mangledName.append("$$");
}
int n = parameters.size();
if (n > 0)
{
mangledName.append("$");
}
for (int i = 0; i < n; ++i)
{
ParameterPtr parameter = parameters[i];
TypePtr type = parameter->GetType();
mangledName.append(type->GetMangleId());
}
return mangledName;
}
std::string MakeFunctionFileName(const std::string& functionSetName)
{
return MangleMap::Instance()->Plain(functionSetName);
}
std::string MakeFunctionTypeName(TypePtr returnType, const std::vector<TypePtr>& parameterTypes)
{
std::string s;
if (returnType)
{
s.append(returnType->FullName()).append(" ");
}
s.append("(");
bool first = true;
for (TypePtr paramType : parameterTypes)
{
if (first)
{
first = false;
}
else
{
s.append(", ");
}
s.append(paramType->FullName());
}
s.append(")");
return s;
}
std::string MakeConceptInstanceName(const ConceptPtr& concept, const std::vector<TypePtr>& types)
{
std::string typeArgumentList = "<";
int n = types.size();
for (int i = 0; i < n; ++i)
{
if (i > 0)
{
typeArgumentList.append(", ");
}
typeArgumentList.append(types[i]->FullName());
}
typeArgumentList.append(">");
std::string conceptInstanceName = concept->FullName() + typeArgumentList;
return conceptInstanceName;
}
void CheckAccess(const ObjectPtr& to, const ObjectPtr& from)
{
return; // todo
/*
if (to->EnclosingScope() == from->EnclosingScope())
{
return; // access from same scope granted
}
Specifiers memberAccess = Specifiers::none;
ClassTypePtr toClassType;
if (to->IsMemberVariable())
{
MemberVariablePtr m = std::static_pointer_cast<MemberVariable>(to);
memberAccess = Specifiers(m->GetSpecifiers() & Specifiers::access);
toClassType = m->ClassType();
}
else if (to->IsEnumeratedType())
{
EnumeratedTypePtr t = std::static_pointer_cast<EnumeratedType>(to);
memberAccess = Specifiers(t->GetSpecifiers() & Specifiers::access);
toClassType = t->ClassType();
}
else if (to->IsEnumerationObject())
{
return; //access to enumeration objects granted
}
else if (to->IsNamespace())
{
return; // access to namespace granted
}
else if (to->IsConstant())
{
ConstantPtr c = std::static_pointer_cast<Constant>(to);
memberAccess = Specifiers(c->GetSpecifiers() & Specifiers::access);
toClassType = c->ClassType();
}
else if (to->IsFunction())
{
if (to->IsMemberFunction())
{
if (to->IsSuppressed())
{
throw TypeCheckException(CANNOT_CALL_SUPPRESSED_MEMBER_FUNCTION, "cannot call suppressed member function '" + to->FullName() + "'", from->Pos());
}
MemberFunctionPtr m = std::static_pointer_cast<MemberFunction>(to);
memberAccess = Specifiers(m->GetSpecifiers() & Specifiers::access);
toClassType = m->ClassType();
}
else
{
FunctionPtr function = std::static_pointer_cast<Function>(to);
Specifiers access = Specifiers(function->GetSpecifiers() & Specifiers::access);
if (access == Specifiers::none || (access & Specifiers::internal_) != Specifiers::none)
{
if (to->Library() != from->Library())
{
throw TypeCheckException(ACCESS_DENIED, "'" + to->FullName() + "' is inaccessible due to its protection level",
Position(from->Pos(), from->IndexOffset(), from->LineOffset()), from->Library());
}
}
return;
}
}
else if (to->IsFunctionSet())
{
return; // access to function set granted
}
else if (to->IsFunctionSetLink())
{
return; // access to function set granted
}
else if (to->IsConceptSet())
{
return; // access to concept set granted
}
else if (to->IsConceptSetLink())
{
return; // access to concept set granted
}
else if (to->IsClassType())
{
toClassType = std::static_pointer_cast<ClassType>(to);
}
else if (to->IsEnumerationObject())
{
return; //access to enumeration objects granted
}
else if (to->IsTypedef())
{
TypedefPtr t = std::static_pointer_cast<Typedef>(to);
memberAccess = Specifiers(t->GetSpecifiers() & Specifiers::access);
toClassType = t->ClassType();
}
else if (to->IsLocalVariable())
{
return; // access granted
}
else if (to->IsBoundTemplateParameter())
{
return; // access granted
}
else if (to->IsNamespaceImport())
{
return; // access granted
}
else
{
CM_CORE_ASSERT(false);
}
ScopePtr outermostMemberScope = from->EnclosingScope()->GetOutermostMemberScope();
ClassTypePtr fromClassType;
if (outermostMemberScope)
{
fromClassType = outermostMemberScope->OwnerClassType();
}
if (toClassType && fromClassType != toClassType && !toClassType->Contains(fromClassType))
{
if (!fromClassType || !fromClassType->Contains(toClassType))
{
while (toClassType)
{
Specifiers classAccess = Specifiers(toClassType->GetSpecifiers() & Specifiers::access);
if (classAccess == Specifiers::private_ || classAccess == Specifiers::protected_)
{
throw TypeCheckException(ACCESS_DENIED, "'" + to->FullName() + "' is inaccessible due to its protection level", from->Pos());
}
else if (!toClassType->ContainingClass() && classAccess == Specifiers::none || classAccess == Specifiers::internal_)
{
if (!fromClassType)
{
if (from->Library() != toClassType->Library())
{
throw TypeCheckException(ACCESS_DENIED, "'" + to->FullName() + "' is inaccessible due to its protection level",
Position(from->Pos(), from->IndexOffset(), from->LineOffset()), from->Library());
}
}
}
toClassType = toClassType->ContainingClass();
}
}
}
if (memberAccess == Specifiers::public_)
{
return; // access granted
}
else if (memberAccess == Specifiers::protected_ || memberAccess == Specifiers::protected_internal)
{
if (to->EnclosingScope()->IsEnclosingScopeOf(from->EnclosingScope()) ||
to->EnclosingScope()->IsBaseScopeOf(from->EnclosingScope()->GetClassScope()))
{
return;
}
if (memberAccess == Specifiers::protected_internal)
{
memberAccess = Specifiers::internal_;
}
}
else if (memberAccess == Specifiers::private_)
{
if (to->EnclosingScope()->IsEnclosingScopeOf(from->EnclosingScope()))
{
return;
}
}
if (memberAccess == Specifiers::none || memberAccess == Specifiers::internal_)
{
if (to->Library() == from->Library())
{
return; // access from same library or program granted
}
}
*/
throw TypeCheckException(ACCESS_DENIED, "'" + to->FullName() + "' is inaccessible due to its protection level", from->Pos());
}
void Split(const std::string& fullFunctionSetName, std::string* nsName, std::string* functionSetName)
{
std::string::size_type lastDotPos = fullFunctionSetName.rfind('.');
if (lastDotPos != std::string::npos)
{
*nsName = fullFunctionSetName.substr(0, lastDotPos);
*functionSetName = fullFunctionSetName.substr(lastDotPos + 1);
}
else
{
nsName->clear();
*functionSetName = fullFunctionSetName;
}
}
void UtilInit()
{
Factory::Init();
MangleMap::Init();
}
void UtilDone()
{
MangleMap::Done();
Factory::Done();
}
} } // namespace Cm::Core