File: NamePart.h

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 (127 lines) | stat: -rw-r--r-- 3,866 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
#pragma once
#include "Core/Array.h"
#include "Value.h"
#include "Syntax/SStr.h"

namespace storm {
	STORM_PKG(core.lang);

	class SimplePart;
	class Scope;
	class Name;
	class Named;
	class NameOverloads;
	class NameLookup;

	/**
	 * Represents one part of a name. Each part is a string and zero or more parameters (think
	 * templates in C++). Parameters may either be resolved names, or other names that has not yet
	 * been resolved. The base class represents the case when parameters are not neccessarily resolved.
	 */
	class NamePart : public ObjectOn<Compiler> {
		STORM_CLASS;
	public:
		// Create with a single name.
		STORM_CTOR NamePart(Str *name);
		NamePart(const wchar *name);

		// Copy.
		STORM_CTOR NamePart(const NamePart &o);

		// Our name.
		Str *name;

		// Resolve names.
		virtual MAYBE(SimplePart *) STORM_FN find(Scope scope);

		// To string.
		virtual void STORM_FN toS(StrBuf *to) const;
	};

	/**
	 * A NamePart which has all parameters properly resolved to Values.
	 */
	class SimplePart : public NamePart {
		STORM_CLASS;
	public:
		// Create with just a name.
		STORM_CTOR SimplePart(Str *name);
		STORM_CTOR SimplePart(syntax::SStr *name);
		SimplePart(const wchar *name);

		// Create with name and parameters.
		STORM_CTOR SimplePart(Str *name, Array<Value> *params);

		// Create with name and one parameter.
		STORM_CTOR SimplePart(Str *name, Value param);

		// Copy.
		STORM_CTOR SimplePart(const SimplePart &o);

		// Parameters.
		Array<Value> *params;

		// Resolve names.
		virtual MAYBE(SimplePart *) STORM_FN find(Scope scope);

		// Compute the badness of a candidate. Returns -1 on no match, and otherwise a positive
		// measure of how bad the match is. The lookup process will pick the one with the lowest
		// badness. We also assume that an exact match has a badness of 0.
		virtual Int STORM_FN matches(Named *candidate, Scope source) const;

		// Present the lookup system with an alternative part. Parts are resolved in the order they
		// appear, and only the first one matches. This can be used to implement multiple levels of
		// name resolution rules without having to traverse the name tree multiple times (which is
		// both slow and causes confusing behavior in some situations).
		virtual MAYBE(SimplePart *) STORM_FN nextOption() const;

		// Note if a parameter is expected to alter the scope of searches. For example, the
		// expression a.b() is expected to look inside 'a' before looking inside other scopes. This
		// is not something the system respects by default. Rather, individual lookup
		// implementations may choose to respect it.
		virtual Bool STORM_FN scopeParam(Nat id) const;

		// Determine if a particular named entity should be visible. The default implementation
		// simply calls 'candidate->visibleFrom(source)', which is suitable in most cases. This
		// function is provided to allow overriding the default visibility rules.
		virtual Bool STORM_FN visible(Named *candidate, Scope source) const;

		// To string.
		virtual void STORM_FN toS(StrBuf *to) const;

		// See if this part is the same as another part.
		virtual Bool STORM_FN sameAs(const SimplePart *as) const;
	};


	/**
	 * A NamePart which has unresolved (recursive) parameters.
	 * TODO: Support reference parameters.
	 */
	class RecPart : public NamePart {
		STORM_CLASS;
	public:
		// Create with just a name.
		STORM_CTOR RecPart(Str *name);
		STORM_CTOR RecPart(syntax::SStr *name);

		// Create with parameters as well.
		STORM_CTOR RecPart(Str *name, Array<Name *> *params);

		// Copy.
		STORM_CTOR RecPart(const RecPart &o);

		// Parameters.
		Array<Name *> *params;

		// Add a parameter.
		inline void STORM_FN add(Name *param) { params->push(param); }

		// Resolve.
		virtual MAYBE(SimplePart *) STORM_FN find(Scope scope);

		// To string.
		virtual void STORM_FN toS(StrBuf *to) const;
	};

}