Fix linux/mac support.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of SourcePawn.
6 * SourcePawn is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * SourcePawn. If not, see http://www.gnu.org/licenses/.
18 #ifndef _include_sp2_packages_h_
19 #define _include_sp2_packages_h_
21 #include "PoolAllocator.h"
23 #include "HashTable.h"
30 class ImportStatement;
31 class VirtualObjectVisitor;
33 template <typename T> class List;
35 struct PathComponent : PoolObject
37 PathComponent(ScopedRoot<String> *name, PathComponent *next)
42 ScopedRoot<String> &name;
46 class Importable : public Object
60 return is(MapKind_Module);
63 return is(MapKind_Package);
66 static inline size_t offsetOfName() {
67 return OFFSETOF(Importable, name_);
69 static inline size_t offsetOfParent() {
70 return OFFSETOF(Importable, parent_);
72 static inline size_t offsetOfPath() {
73 return OFFSETOF(Importable, path_);
76 static inline Importable *cast(Object *obj) {
77 assert(!obj || obj->is(MapKind_Module) || obj->is(MapKind_Package));
78 return reinterpret_cast<Importable *>(obj);
82 Barriered<String> name_;
83 Barriered<Package> parent_;
84 Barriered<String> path_;
87 // A package is a single folder in the file system, and contains any number of
88 // modules and sub-packages. The package hierarchy forms a tree. For simplicity
89 // the root of the tree is an anonymous package that contains no modules.
91 // Each package in the package hierarchy has two components of path information:
92 // (1) Canonical path. This is a normalized path used to compute the paths of
94 // (2) Name. This is the package's name as it would appear in a path
95 // component. It is optional.
97 // Packages may be "hidden", meaning that even if they have names, their name
98 // is not accessible from any import statements. Packages may also be "jailed",
99 // meaning that other names in the package are not visible to each other.
101 // Packages are created in a few ways:
103 // (1) System Roots. A system root is a default search location for imports,
104 // and they are installed at the top-level of the hierarchy by the host
105 // application. A system root has a unique top-level name and path.
106 // System roots are always hidden.
108 // (2) Imports. When importing, the package hierarchy may be extended as
109 // new paths are discovered. Packages discovered this way are never
112 // (3) Via Package::New(), which creates an empty package. This can be used
113 // to create custom packages.
114 class Package : public Importable
120 // These must match the flags in the public API for Package.
126 static Package *NewNormalized(Zone *zone,
127 Handle<Package> parent,
131 static Package *New(Zone *zone,
132 Handle<Package> parent,
137 bool add(Zone *zone, Handle<Importable> component);
140 return !!(flags_ & Package::Hidden);
143 return !!(flags_ & Package::Jailed);
146 void setNativeIdentity(void *identity) {
147 nativeIdentity_ = identity;
149 void *getNativeIdentity() {
150 return nativeIdentity_;
152 Importable *import(Handle<String> name);
154 // Begin GC descriptor.
155 static inline size_t offsetOfContents() {
156 return OFFSETOF(Package, contents_);
158 // End GC descriptor.
160 static inline Package *cast(Object *obj) {
161 assert(!obj || obj->is(MapKind_Package));
162 return reinterpret_cast<Package *>(obj);
167 Barriered<List<Importable> > contents_;
168 void *nativeIdentity_;
174 PackageCache(Zone *zone);
178 bool findOrAdd(Handle<Package> package, PathComponent *path, Importable **out);
180 void accept(VirtualObjectVisitor *visitor);
183 bool import(Handle<Package> package, PathComponent *path, Importable **out);
184 bool checkfs(Handle<Package> package, PathComponent *path, Importable **out);
188 Vector<Unbarriered<Package> > roots_;
193 #endif // _include_sp2_packages_h_