Basic support for parsing and creating types from structure syntax.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_name_binding_h_
20 #define _include_name_binding_h_
22 #include "PoolAllocator.h"
37 class BoundName : public PoolObject
40 ScopedRoot<String> name_;
41 ScopedRoot<Type> type_;
56 BoundName(Scope *scope, Handle<String> name, const SourcePosition &pos);
58 virtual Kind kind() const = 0;
60 String *name() const {
63 Scope *scope() const {
66 Variable *toVariable() {
67 assert(kind() == VARIABLE);
68 return reinterpret_cast<Variable *>(this);
70 NamedFunction *toNamedFunction() {
71 assert(kind() == FUNCTION);
72 return reinterpret_cast<NamedFunction *>(this);
74 NamedConstant *toNamedConstant() {
75 assert(kind() == CONSTANT);
76 return reinterpret_cast<NamedConstant *>(this);
78 NamedType *toNamedType() {
79 assert(kind() == TYPE);
80 return reinterpret_cast<NamedType *>(this);
85 void setType(Handle<Type> type) {
94 const SourcePosition &pos() {
99 class NamedFunction : public BoundName
103 ScopedRoot<Type> forwardType_;
104 bool isVariadicNative_;
105 Vector<BoundName *, PoolAllocationPolicy> globals_;
108 NamedFunction(Scope *scope, Handle<String> name, const SourcePosition &pos, TokenKind token);
113 void allocate(unsigned slot) {
116 bool isForward() const {
117 return token_ == TOK_FORWARD;
119 bool isPublic() const {
120 return token_ == TOK_PUBLIC;
122 bool isNative() const {
123 return token_ == TOK_NATIVE;
125 void setForwardType(Handle<Type> type) {
128 Type *forwardType() {
131 void setImplemented(const SourcePosition &at) {
135 void markNativeVariadic() {
136 assert(token_ == TOK_NATIVE);
137 isVariadicNative_ = true;
139 bool isNativeVariadic() {
140 assert(token_ == TOK_NATIVE);
141 return isVariadicNative_;
143 unsigned slot() const {
146 bool addUsedGlobal(BoundName *name) {
147 return globals_.append(name);
149 Vector<BoundName *, PoolAllocationPolicy> *usedGlobals() {
154 class NamedConstant : public BoundName
156 BoxedPrimitive value_;
160 NamedConstant(Scope *scope, Handle<String> name, const SourcePosition &pos)
161 : BoundName(scope, name, pos),
169 void set(Type *type, const BoxedPrimitive &value) {
176 BoxedPrimitive value() const {
181 class NamedType : public BoundName
184 NamedType(Scope *scope, Handle<String> name, Handle<Type> type)
185 : BoundName(scope, name, SourcePosition())
190 NamedType(Scope *scope, Handle<String> name, const SourcePosition &pos)
191 : BoundName(scope, name, pos)
200 class UnboundName : public BoundName
203 UnboundName(Scope *scope, Handle<String> name)
204 : BoundName(scope, name, SourcePosition())
213 class Variable : public BoundName
223 Variable(Scope *scope, Handle<String> name, const SourcePosition &pos);
228 bool unallocated() const {
229 return location() == UNALLOCATED;
231 void allocate(Location location, unsigned slot) {
232 location_ = location;
235 Location location() const {
238 unsigned slot() const {
239 assert(!unallocated());
242 bool needsHeapAllocation();
251 #endif // _include_name_binding_h_