File: TemplateList.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • 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 (175 lines) | stat: -rw-r--r-- 3,850 bytes parent folder | download | duplicates (4)
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
#include "stdafx.h"
#include "TemplateList.h"
#include "Engine.h"
#include "Type.h"
#include "Exception.h"

namespace storm {

	/**
	 * Compatible with GcArray. We're also storing a pointer to another node in here, so 'elements'
	 * will always be one too large. Therefore, we store the 'correct' count inside 'count'.
	 */
	struct TemplateList::Node {
		size_t elements;
		size_t count;

		// If we're done.
		Type *done;

		// Instantiations here. The index 0 is used if for the parameter 'null'.
		Node *next[1];
	};

	TemplateList::TemplateList(World *world, TemplateCppFn *t) : templ(t), world(world), addedTo(null) {
		lock = new (this) Lock();
	}

	void TemplateList::addTo(NameSet *to) {
		if (!addedTo) {
			addedTo = to;
			if (!templ->placeholder())
				to->add(templ);
			addTo(root, to);
		}
	}

	void TemplateList::addTo(Node *at, NameSet *to) {
		if (!at)
			return;

		try {
			if (at->done)
				to->add(at->done);
		} catch (const TypedefError *e) {
			// Sometimes we have a different notion of what is acceptable.
			WARNING(e->toS());
		}

		for (nat i = 0; i < at->count; i++)
			addTo(at->next[i], to);
	}

	Type *TemplateList::find(Nat *elems, Nat count) {
		Lock::Guard z(lock);

		Type *found = findAt(elems, count, root, 0);
		if (!found) {
			found = generate(elems, count);
			insertAt(elems, count, found, root, 0);
		}

		return found;
	}

	Type *TemplateList::findAt(Nat *elems, Nat count, Node *node, Nat at) {
		if (!node)
			return null;

		if (count == at)
			return node->done;

		Nat now = elems[at];
		if (now == Nat(-1))
			now = 0;
		else
			now++;

		if (node->count <= now)
			return null;

		return findAt(elems, count, node->next[now], at + 1);
	}

	void TemplateList::insertAt(Nat *elems, Nat count, Type *insert, Node *&node, Nat at) {
		if (count == at) {
			// Insert it here!
			if (!node)
				node = allocNode(0);

			node->done = insert;
			return;
		}

		Nat now = elems[at];
		if (now == Nat(-1))
			now = 0;
		else
			now++;

		// Create a new node?
		if (!node)
			node = allocNode(now + 1);

		// Increase size of existing node?
		if (node->count <= now)
			node = allocNode(node, now + 1);

		insertAt(elems, count, insert, node->next[now], at + 1);
	}

	void TemplateList::forNamed(NamedFn fn) {
		forNamed(root, fn);
	}

	void TemplateList::forNamed(Node *node, NamedFn fn) {
		if (!node)
			return;

		if (node->done)
			(*fn)(node->done);

		for (nat i = 0; i < node->count; i++)
			forNamed(node->next[i], fn);
	}

	TemplateList::Node *TemplateList::allocNode(Nat count) {
		Node *n = (Node *)engine().gc.allocArray(&pointerArrayType, count + 1);
		n->count = count;
		return n;
	}

	TemplateList::Node *TemplateList::allocNode(const Node *original, Nat count) {
		Node *mem = allocNode(count);
		mem->done = original->done;
		for (nat i = 0; i < min(count, nat(original->count)); i++)
			mem->next[i] = original->next[i];
		return mem;
	}

	Type *TemplateList::generate(Nat *elems, Nat count) {
		Engine &e = engine();
		ValueArray *types = new (e) ValueArray();
		types->reserve(count);

		for (nat i = 0; i < count; i++) {
			if (elems[i] == Nat(-1)) {
				types->push(Value());
			} else {
				Type *t = world->types[elems[i]];
				assert(t, L"Type with id " + ::toS(elems[i]) + L" not found.");
				types->push(Value(t));
			}
		}

		if (addedTo && e.has(bootTemplates)) {
			// Was the type already added from somewhere else?
			SimplePart *p = new (e) SimplePart(templ->name, types->toArray());
			if (Type *t = as<Type>(addedTo->find(p, Scope())))
				return t;
		}

		Type *r = templ->generate(types);
		assert(r, L"Invalid template usage for types " + ::toS(types));
		if (addedTo) {
			try {
				addedTo->add(r);
			} catch (const TypedefError *e) {
				// Sometimes, we have a different notion of what is acceptable together.
				WARNING(e->toS());
			}
		}
		return r;
	}

}