File: Unknown.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 (70 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download | duplicates (3)
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
#include "stdafx.h"
#include "Unknown.h"
#include "Compiler/Engine.h"

namespace storm {

	UnknownType::UnknownType(Str *name, Size size, GcType *type)
		: Type(name, typeValue | typeFinal, size, type, null) {}

	code::TypeDesc *UnknownType::createTypeDesc() {
		Size s = size();
		if (s == Size::sPtr) {
			return engine.ptrDesc();
		} else {
			return code::intDesc(engine);
		}
	}

	static void copyPtr(void **to, void **from) {
		*to = *from;
	}

	static void copyInt(Int *to, Int *from) {
		*to = *from;
	}

	Str *CODECALL unknownPtrToS(EnginePtr e, RootObject *ptr) {
		StrBuf *buf = new (e) StrBuf();
		*buf << S("0x") << hex(ptr);
		return buf->toS();
	}

	Bool UnknownType::loadAll() {
		Array<Value> *rr = new (this) Array<Value>(2, Value(this, true));
		Array<Value> *v = new (this) Array<Value>(1, Value(this, false));
		// Note: These constructors do not need to be very efficient. They are almost never called.
		if (size() == Size::sPtr) {
			add(nativeFunction(engine, Value(), Type::CTOR, rr, address(&copyPtr))->makePure());
			add(nativeEngineFunction(engine, StormInfo<Str *>::type(engine), S("toS"), v, address(&unknownPtrToS)));
		} else {
			add(nativeFunction(engine, Value(), Type::CTOR, rr, address(&copyInt))->makePure());
		}

		return true;
	}

	Type *createUnknownInt(Str *name, Size size, GcType *type) {
		return new (name) UnknownType(name, Size::sInt, type);
	}

	Type *createUnknownGc(Str *name, Size size, GcType *type) {
		// The GcType here will probably not contain proper data for the type. Fix that!
		if (type->count == 0) {
			Engine &e = name->engine();
			GcType *old = type;
			type = e.gc.allocType(GcType::Kind(old->kind), old->type, old->stride, 1);
			e.gc.freeType(old);

			// We're a pointer!
			type->offset[0] = 0;
		}

		return new (name) UnknownType(name, Size::sPtr, type);
	}

	Type *createUnknownNoGc(Str *name, Size size, GcType *type) {
		return new (name) UnknownType(name, Size::sPtr, type);
	}

}