File: CppLoader.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (758 lines) | stat: -rw-r--r-- 20,194 bytes parent folder | download
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
746
747
748
749
750
751
752
753
754
755
756
757
758
#include "stdafx.h"
#include "CppLoader.h"
#include "Type.h"
#include "Engine.h"
#include "Package.h"
#include "Function.h"
#include "License.h"
#include "Version.h"
#include "Code.h"
#include "VTableCpp.h"
#include "CompilerProtocol.h"
#include "Exception.h"
#include "Lib/Maybe.h"
#include "Lib/Enum.h"
#include "Core/Str.h"

namespace storm {

	CppLoader::CppLoader(Engine &e, const CppWorld *world, World &into, Package *root, Url *docPath) :
		e(&e), world(world), into(&into), rootPackage(root), docUrl(null) {

		if (docPath)
			docUrl = docPath->push(new (e) Str(world->docFile));
	}

	Nat CppLoader::typeCount() const {
		Nat n = 0;
		while (world->types[n].name)
			n++;
		return n;
	}

	Nat CppLoader::templateCount() const {
		Nat n = 0;
		while (world->templates[n].name)
			n++;
		return n;
	}

	Nat CppLoader::threadCount() const {
		Nat n = 0;
		while (world->threads[n].name)
			n++;
		return n;
	}

	Nat CppLoader::functionCount() const {
		Nat n = 0;
		while (world->functions[n].name)
			n++;
		return n;
	}

	Nat CppLoader::variableCount() const {
		Nat n = 0;
		while (world->variables[n].name)
			n++;
		return n;
	}

	Nat CppLoader::enumValueCount() const {
		Nat n = 0;
		while (world->enumValues[n].name)
			n++;
		return n;
	}

	Nat CppLoader::licenseCount() const {
		Nat n = 0;
		while (world->licenses[n].name)
			n++;
		return n;
	}

	Nat CppLoader::versionCount() const {
		Nat n = 0;
		while (world->versions[n].name)
			n++;
		return n;
	}

	Nat CppLoader::sourceCount() const {
		Nat n = 0;
		while (world->sources[n])
			n++;
		return n;
	}

	static const void *typeVTable(const CppType &t) {
		if (t.vtable)
			return (*t.vtable)();
		else
			return null;
	}

	Type *CppLoader::createType(Nat id, const CppType &type) {
		TypeFlags flags = type.flags;
		// Validate the C++ flags in here.
		if ((flags & typeCppPOD) && (flags & typeValue)) {
			if (e->has(bootDone)) {
				Str *msg = TO_S(*e, S("The class ") << type.pkg << S(".") << type.name
								<< S(" is a POD type, which is not supported by Storm. ")
								S("Add a constructor to use the class (it does not have to be exposed to Storm) ")
								S("and compile again."));
				throw new (*e) BuiltInError(msg);
			} else {
				throw CppLoadError(L"The class " + ::toS(type.pkg) + L"." + ::toS(type.name) +
								L" is a POD type, which is not supported by Storm. "
								L"Add a constructor to the class (it does not have to be exposed to Storm) "
								L"and compile again.");
			}
		}

		GcType *gcType = createGcType(id);

		// If this type inherits from 'Type', it needs special care in its Gc-description.
		if (typeKind(type) == CppType::tSuperClassType) {
			Type::makeType(*e, gcType);
		}

		return new (*e) Type(null, flags, Size(type.size), gcType, typeVTable(type));
	}

	// Equivalent to a trimmed-down version of scope().find, but is allowed to access private members in types.
	static Named *findPrivateType(Engine &e, SimpleName *name) {
		Scope scope = e.scope();
		Named *at = e.package();
		for (Nat i = 0; i < name->count(); i++) {
			if (NameSet *s = as<NameSet>(at))
				at = s->find(name->at(i), scope.child(at));
			else
				return null;
		}
		return at;
	}

	Type *CppLoader::findType(const CppType &type) {
		SimpleName *name = parseSimpleName(*e, type.pkg);
		name->add(new (*e) Str(type.name));
		// Type *t = as<Type>(e->scope().find(name));
		Type *t = as<Type>(findPrivateType(*e, name));
		if (!t) {
			if (e->has(bootDone)) {
				throw new (*e) BuiltInError(TO_S(*e, S("Failed to locate ") << name));
			} else {
				throw CppLoadError(L"Failed to locate " + ::toS(name));
			}
		}
		return t;
	}

	void CppLoader::loadTypes() {
		Nat c = typeCount();
		into->types.resize(c);

		// Note: we do not set any names yet, as the Str type is not neccessarily available until
		// after we've created the types here.
		for (Nat i = 0; i < c; i++) {
			const CppType &type = world->types[i];

			// The array could be partially filled.
			if (into->types[i] == null && !delayed(type)) {
				if (external(type))
					into->types[i] = findType(type);
				else
					into->types[i] = createType(i, type);
			}
		}

		// Create all types with custom types.
		for (Nat i = 0; i < c; i++) {
			const CppType &type = world->types[i];

			if (!delayed(type))
				continue;
			if (into->types[i])
				continue;

			CppType::CreateFn fn = (CppType::CreateFn)type.super;
			if (typeKind(type) == CppType::tEnum)
				fn = &createEnum;
			else if (typeKind(type) == CppType::tBitmaskEnum)
				fn = &createBitmaskEnum;
			into->types[i] = (*fn)(new (*e) Str(type.name), Size(type.size), createGcType(i));
		}

		// If we're in early boot, we need to manually create vtables for all types.
		if (!e->has(bootTypes)) {
			for (Nat i = 0; i < c; i++) {
				into->types[i]->vtableInit(typeVTable(world->types[i]));
			}
		}

		// Now we can fill in the names and visibility properly!
		for (Nat i = 0; i < c; i++) {
			const CppType &type = world->types[i];

			// Don't mess with external types.
			if (external(type))
				continue;

			// Just to make sure...
			if (!into->types[i])
				continue;

			// Name.
			into->types[i]->name = new (*e) Str(type.name);

			// Visibility.
			if (typeHasFlag(type, CppType::tPrivate))
				into->types[i]->visibility = e->visibility(Engine::vTypePrivate);
			else
				into->types[i]->visibility = e->visibility(Engine::vPublic);

			// Documentation.
			setDoc(into->types[i], type.doc, null);
		}
	}

	void CppLoader::loadThreads() {
		Nat c = threadCount();
		into->threads.resize(c);
		into->namedThreads.resize(c);

		for (Nat i = 0; i < c; i++) {
			const CppThread &thread = world->threads[i];

			if (external(thread)) {
				SimpleName *name = parseSimpleName(*e, thread.pkg);
				name->add(new (*e) Str(thread.name));
				NamedThread *found = as<NamedThread>(e->scope().find(name));
				if (!found) {
					if (e->has(bootDone)) {
						throw new (*e) BuiltInError(TO_S(*e, S("Failed to locate") << name));
					} else {
						throw CppLoadError(L"Failed to locate " + ::toS(name));
					}
				}

				into->namedThreads[i] = found;
				into->threads[i] = found->thread();
			} else {
				if (!into->threads[i]) {
					into->threads[i] = new (*e) Thread(thread.decl->createFn);
				}

				into->namedThreads[i] = new (*e) NamedThread(new (*e) Str(thread.name), into->threads[i]);
				into->namedThreads[i]->visibility = e->visibility(Engine::vPublic);
				setDoc(into->namedThreads[i], thread.doc, null);
			}
		}
	}

	void CppLoader::loadSuper() {
		Nat c = typeCount();

		// Remember which ones we've already set.
		vector<bool> updated(c, false);

		do {
			// Try to add super classes to all classes until we're done. We're only adding classes
			// as a super class when they have their super classes added, to avoid re-computation of
			// lookup tables and similar.
			for (Nat i = 0; i < c; i++) {
				const CppType &type = world->types[i];
				if (updated[i])
					continue;

				switch (typeKind(type)) {
				case CppType::tNone:
					// Nothing to do.
					break;
				case CppType::tSuperClass:
				case CppType::tSuperClassType:
					// Delay update?
					if (!updated[type.super])
						continue;

					into->types[i]->setSuper(into->types[Nat(type.super)]);
					break;
				case CppType::tSuperThread:
					if (TObject::stormType(*e) == null)
						continue;

					into->types[i]->setThread(into->namedThreads[Nat(type.super)]);
					break;
				case CppType::tCustom:
				case CppType::tEnum:
				case CppType::tBitmaskEnum:
					// Already done.
					break;
				case CppType::tExternal:
					// Nothing to do!
					break;
				default:
					assert(false, L"Unknown kind on type " + ::toS(type.name) + L": " + ::toS(type.kind) + L".");
					break;
				}

				updated[i] = true;
			}
		} while (!all(updated));
	}

	GcType *CppLoader::createGcType(Nat id) {
		const CppType &type = world->types[id];

		Nat entries;
		for (entries = 0; type.ptrOffsets[entries] != CppOffset::invalid; entries++)
			;

		GcType *t = e->gc.allocType(GcType::tFixedObj, null, Size(type.size).current(), entries);

		for (Nat i = 0; i < entries; i++) {
			t->offset[i] = Offset(type.ptrOffsets[i]).current();
		}

#ifdef DEBUG
		if (!world->refTypes) {
			// Don't crash if someone missed their data.
			WARNING(L"Missing debug offset data when loading C++ functions!");
		} else {
			const CppRefType &ref = world->refTypes[id];

			// Note: t->stride == 0 means that sizeof(Type) will be 1.
			// Note: ref.size == 0 means that this type does not exist in C++.
			assert(t->stride == 0 || ref.size == 0 || t->stride == ref.size, L"Type size mismatch for " +
				::toS(type.name) + L". Storm size: " + ::toS(t->stride) + L", C++ size: " + ::toS(ref.size) + L".");

			for (Nat i = 0; i < entries; i++) {
				// Uncheckable?
				if (ref.offsets[i] == nat(-1))
					continue;

				assert(t->offset[i] == ref.offsets[i], L"Type offset mismatch for " + ::toS(type.name) + L" member #" +
					::toS(i) + L". Storm offset: " + ::toS(t->offset[i]) + L", C++ offset: " + ::toS(ref.offsets[i]));
			}

			assert(ref.offsets[entries] == nat(-1), L"There is more data for " + ::toS(type.name));
		}
#endif

		return t;
	}

	void CppLoader::loadTemplates() {
		Nat c = templateCount();
		into->templates.resize(c);

		for (Nat i = 0; i < c; i++) {
			const CppTemplate &t = world->templates[i];

			if (!into->templates[i]) {
				into->templates[i] = loadTemplate(t);
				// TODO: Documentation for templates!
			}
		}
	}

	TemplateList *CppLoader::loadTemplate(const CppTemplate &t) {
		Str *n = new (*e) Str(t.name);
		TemplateCppFn *templ = null;
		if (external(t))
			templ = new (*e) TemplatePlaceholder(n);
		else
			templ = new (*e) TemplateCppFn(n, t.generate);

		TemplateList *result = new (*e) TemplateList(into, templ);

		if (external(t)) {
			// Attach the template to the correct package right now, otherwise it can not be used.
			NameSet *pkg = findAbsPkg(t.pkg);
			if (!pkg) {
				if (e->has(bootDone)) {
					throw new (*e) InternalError(TO_S(*e, S("Could not find the package ") << t.pkg << S("!")));
				} else {
					throw CppLoadError(L"Could not find the package " + ::toS(t.pkg) + L"!");
				}
			}
			result->addTo(pkg);
		}

		return result;
	}

	NameSet *CppLoader::findPkg(const wchar *name) {
		if (!rootPackage)
			rootPackage = e->package();

		SimpleName *pkgName = parseSimpleName(*e, name);
		NameSet *r = e->nameSet(pkgName, rootPackage, true);
		assert(r, L"Failed to find the package or type " + String(name));
		return r;
	}

	NameSet *CppLoader::findAbsPkg(const wchar *name) {
		SimpleName *pkgName = parseSimpleName(*e, name);
		NameSet *r = e->nameSet(pkgName, e->package(), false);
		assert(r, L"Failed to find the package or type " + String(name));
		return r;
	}

	Value CppLoader::findValue(const CppTypeRef &ref) {
		Value result;
		if (ref.params) {
			// Template!
			const Nat maxElem = 20;
			Nat elems[maxElem];
			Nat count = 0;
			for (count = 0; count < maxElem && ref.params[count] != CppTypeRef::invalid; count++) {
				assert(count < maxElem, L"Too many template parameters used in C++!");
				if (ref.params[count] == CppTypeRef::tVoid)
					elems[count] = -1;
				else
					elems[count] = Nat(ref.params[count]);
			}

			result = Value(into->templates[ref.id]->find(elems, count));
		} else if (ref.id != CppTypeRef::invalid) {
			// Regular type.
			result = Value(into->types[ref.id]);
		} else {
			// Void.
			return Value();
		}

		if (ref.ref) {
			// Not applicable to class types.
			if (!result.isObject())
				result = result.asRef(true);
		}

		if (ref.maybe) {
			result = wrapMaybe(result);
		}

		return result;
	}

	void CppLoader::loadPackages() {
		Nat c = typeCount();
		for (Nat i = 0; i < c; i++) {
			const CppType &t = world->types[i];
			if (!external(t)) {
				findPkg(t.pkg)->add(into->types[i]);
				setPos(into->types[i], t.pos);
			}
		}

		c = templateCount();
		for (Nat i = 0; i < c; i++) {
			const CppTemplate &t = world->templates[i];
			if (!external(t)) {
				NameSet *to = findPkg(t.pkg);
				into->templates[i]->addTo(to);
			}
		}

		c = threadCount();
		for (Nat i = 0; i < c; i++) {
			const CppThread &t = world->threads[i];
			if (!external(t)) {
				findPkg(t.pkg)->add(into->namedThreads[i]);
				setPos(into->namedThreads[i], t.pos);
			}
		}
	}


	void CppLoader::loadFunctions() {
		// This is fairly expensive and generates a bit of garbage. Help the GC by informing it of
		// the mess we're going to create.
		Gc::RampAlloc z(e->gc);

		Nat c = functionCount();
		for (Nat i = 0; i < c; i++) {
			loadFunction(world->functions[i]);
		}
	}

	void CppLoader::loadFunction(const CppFunction &fn) {
		switch (fnKind(fn)) {
		case CppFunction::fnFree:
		case CppFunction::fnFreeEngine:
		case CppFunction::fnStatic:
		case CppFunction::fnStaticEngine:
			loadFreeFunction(fn);
			break;
		case CppFunction::fnMember:
			loadMemberFunction(fn, false);
			break;
		case CppFunction::fnCastMember:
			loadMemberFunction(fn, true);
			break;
		default:
			assert(false, L"Unknown function kind: " + ::toS(fn.kind));
			break;
		}
	}

	void CppLoader::loadFreeFunction(const CppFunction &fn) {
		NameSet *into = findPkg(fn.pkg);

		Value result = findValue(fn.result);

		Function *f = new (*e) Function(result, new (*e) Str(fn.name), loadFnParams(fn.params));

		switch (fnKind(fn)) {
		case CppFunction::fnFreeEngine:
		case CppFunction::fnStaticEngine:
			f->setCode(new (*e) StaticEngineCode(fn.ptr));
			break;
		default:
			f->setCode(new (*e) StaticCode(fn.ptr));
			break;
		}

		if (fn.threadId < this->into->namedThreads.count())
			f->runOn(this->into->namedThreads[fn.threadId]);

		if (fnHasFlag(fn, CppFunction::fnAssign))
			f->make(fnAssign);

		if (fnKind(fn) == CppFunction::fnStatic)
			f->make(fnStatic);

		// Note: CppFunction::fnFinal or CppFunction::fnAbstract has no meaning for free functions,
		// so we can ignore it here.

		f->visibility = visibility(fn.access);
		setDoc(f, fn.doc, fn.params);
		setPos(f, fn.pos);

		into->add(f);
	}

	void CppLoader::loadMemberFunction(const CppFunction &fn, bool cast) {
		Value result = findValue(fn.result);
		Array<Value> *params = loadFnParams(fn.params);
		assert(params->count() > 0, L"Missing this pointer for " + ::toS(fn.name) + L"!");

		// Find the vtable...
		const CppParam &firstParam = fn.params[0];
		assert(firstParam.type.params == null, L"Members for template types are not supported!");
		// CppType::VTableFn vFn = world->types[firstParam.id].vtable;

		const void *ptr = deVirtualize(firstParam.type, fn.ptr);
		Function *f = new (*e) CppMemberFunction(result, new (*e) Str(fn.name), params, fn.ptr);

		if (cast)
			f->makeAutoCast();

		if (fn.threadId < into->namedThreads.count())
			f->runOn(into->namedThreads[fn.threadId]);

		if (wcscmp(Type::CTOR, fn.name) == 0 || wcscmp(Type::DTOR, fn.name) == 0) {
			// Check if the type was declared as a simple type.
			Type *owner = params->at(0).type;
			if (owner->typeFlags() & typeCppSimple)
				// If so, it has a pure constructor.
				f->makePure();
		}

		if (fnHasFlag(fn, CppFunction::fnAssign))
			f->make(fnAssign);

		if (fnHasFlag(fn, CppFunction::fnFinal))
			f->make(fnFinal);

		if (fnHasFlag(fn, CppFunction::fnAbstract))
			f->make(fnAbstract);

		f->visibility = visibility(fn.access);
		setDoc(f, fn.doc, fn.params);
		setPos(f, fn.pos);

		params->at(0).type->add(f);

		// We don't rely on the vtable to be correct in this particular case. Depending on which
		// instance of the vtable we get, we might get a version that calls the C++ runtime's
		// version of "pure virtual call", which we don't want.
		if (fnHasFlag(fn, CppFunction::fnAbstract)) {
			f->setCode(abstractThrowCode(result, params, f->identifier()));
		} else {
			f->setCode(new (*e) StaticCode(ptr));
		}
	}

	Array<Value> *CppLoader::loadFnParams(const CppParam *params) {
		Array<Value> *r = new (*e) Array<Value>();

		for (Value v = findValue(params->type); v != Value(); v = findValue((++params)->type)) {
			r->push(v);
		}

		return r;
	}

	const void *CppLoader::findVTable(const CppTypeRef &ref) {
		assert(ref.params == null, L"Members of template types are not supported!");
		CppType::VTableFn f = world->types[ref.id].vtable;
		if (f)
			return (*f)();
		else
			return null;
	}

	const void *CppLoader::deVirtualize(const CppTypeRef &ref, const void *fn) {
		const void *tab = findVTable(ref);
		if (!tab)
			return fn;
		const void *f = vtable::deVirtualize(tab, fn);
		if (!f)
			return fn;
		return f;
	}

	void CppLoader::loadVariables() {
		Nat c = variableCount();
		for (Nat i = 0; i < c; i++) {
			loadVariable(world->variables[i]);
		}

		c = enumValueCount();
		for (Nat i = 0; i < c; i++) {
			loadEnumValue(world->enumValues[i]);
		}
	}

	void CppLoader::loadVariable(const CppVariable &var) {
		Type *memberOf = into->types[var.memberOf];
		assert(memberOf, L"Type not properly loaded!");

		Value type = findValue(var.type);
		assert(type != Value(), L"Type of the variable is void!");

		MemberVar *v = new (*e) MemberVar(SrcPos(), new (*e) Str(var.name), type, memberOf);
		v->setOffset(Offset(var.offset));
		v->visibility = visibility(var.access);
		setDoc(v, var.doc, null);
		memberOf->add(v);
	}

	void CppLoader::loadEnumValue(const CppEnumValue &val) {
		Enum *memberOf = as<Enum>(into->types[val.memberOf]);
		assert(memberOf, L"Type not properly loaded or not an enum!");

		EnumValue *value = new (*e) EnumValue(memberOf, new (*e) Str(val.name), val.value);
		setDoc(value, val.doc, null);
		memberOf->add(value);
	}

	void CppLoader::loadMeta() {
		loadLicenses();
		loadVersions();
	}

	void CppLoader::loadLicenses() {
		Nat count = licenseCount();
		for (Nat i = 0; i < count; i++) {
			const CppLicense &l = world->licenses[i];
			NameSet *into = findPkg(l.pkg);
			into->add(new (*e) License(
						new (*e) Str(l.name), new (*e) Str(l.title),
						new (*e) Str(l.author), new (*e) Str(l.body)));
		}
	}

	void CppLoader::loadVersions() {
		Nat count = versionCount();
		for (Nat i = 0; i < count; i++) {
			const CppVersion &v = world->versions[i];
			Version *ver = parseVersion(new (*e) Str(v.version));
			if (!ver) {
				if (e->has(bootDone)) {
					throw new (*e) InternalError(TO_S(*e, S("Failed to parse the version string ") << v.version));
				} else {
					throw CppLoadError(L"Failed to parse the version string " + ::toS(v.version));
				}
			}

			NameSet *into = findPkg(v.pkg);
			into->add(new (*e) VersionTag(new (*e) Str(v.name), ver, SrcPos()));
		}
	}

	Visibility *CppLoader::visibility(CppAccess a) {
		switch (a) {
		case cppPublic:
			return e->visibility(Engine::vPublic);
		case cppProtected:
			return e->visibility(Engine::vTypeProtected);
		case cppPrivate:
			return e->visibility(Engine::vTypePrivate);
		default:
			assert(false, L"Unknown access in C++ code: " + ::toS(a));
			return null;
		}
	}

	CppDoc *CppLoader::createDoc(Named *entity, Nat id, const CppParam *params) {
		if (id <= 0)
			return null;

		if (docUrl)
			return new (*e) CppDoc(entity, docUrl, id, params);
		else
			return new (*e) CppDoc(entity, world->docFile, id, params);
	}

	void CppLoader::setDoc(Named *entity, Nat id, const CppParam *params) {
		entity->documentation = createDoc(entity, id, params);
	}

	void CppLoader::setPos(Named *entity, CppSrcPos pos) {
		if (pos.id < 0)
			return;

		if (into->sources.count() == 0)
			createSources();

		entity->pos = SrcPos(into->sources[pos.id], pos.pos, pos.pos + 1);
	}

	static Array<Str *> *splitPath(Engine &e, const wchar *path) {
		Array<Str *> *r = new (e) Array<Str *>();
		const wchar *start = path;
		const wchar *at;
		for (at = start; *at; at++) {
			if (*at == '/') {
				if (start != at)
					*r << new (e) Str(start, at);
				start = at + 1;
			}
		}

		if (start != at)
			*r << new (e) Str(start, at);

		return r;
	}

	void CppLoader::createSources() {
		Nat n = sourceCount();
		into->sources.resize(n);

		CompilerProtocol *proto;
		if (world->libName)
			proto = new (*e) CompilerProtocol(new (*e) Str(world->libName));
		else
			proto = new (*e) CompilerProtocol();

		for (Nat i = 0; i < n; i++) {
			into->sources[i] = new (*e) Url(proto, splitPath(*e, world->sources[i]));
		}
	}

}