Create Autoload map from repo
[hiphop-php.git] / hphp / runtime / vm / repo-autoload-map-builder.h
blobfa3d989895c33ae54f9edb28efcebea48a4e383e
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_REPO_AUTOLOAD_MAP_BUILDER_H_
17 #define incl_HPHP_REPO_AUTOLOAD_MAP_BUILDER_H_
19 #include "hphp/runtime/base/repo-autoload-map.h"
20 #include "hphp/runtime/base/string-data.h"
21 #include "hphp/runtime/base/string-functors.h"
22 #include "hphp/runtime/vm/blob-helper.h"
23 #include "hphp/util/hash-map.h"
25 #include <tbb/concurrent_hash_map.h>
27 namespace HPHP {
29 //////////////////////////////////////////////////////////////////////
31 struct PreClassEmitter;
32 struct FuncEmitter;
33 struct TypeAlias;
34 struct Constant;
36 struct RepoAutoloadMapBuilder {
38 struct Guard {
39 Guard() {
40 RepoAutoloadMapBuilder::init();
43 ~Guard() {
44 RepoAutoloadMapBuilder::fini();
48 template <typename Compare>
49 using Map = tbb::concurrent_hash_map<
50 const StringData*,
51 int64_t,
52 Compare
55 using CaseInsensitiveMap = Map<StringDataHashICompare>;
56 using CaseSensitiveMap = Map<StringDataHashCompare>;
58 struct BuilderBase {
59 virtual void addClass(const PreClassEmitter& ce, int unitSn) = 0;
60 virtual void addFunc(const FuncEmitter& fe, int unitSn) = 0;
61 virtual void addTypeAlias(const TypeAlias& fe, int unitSn) = 0;
62 virtual void addConstant(const Constant& fe, int unitSn) = 0;
63 virtual void serde(BlobEncoder& sd) = 0;
66 friend struct FuncEmitter;
68 struct BuilderNoop : public BuilderBase {
69 void addClass(const PreClassEmitter& ce, int unitSn) {};
70 void addFunc(const FuncEmitter& fe, int unitSn) {};
71 void addTypeAlias(const TypeAlias& fe, int unitSn) {};
72 void addConstant(const Constant& fe, int unitSn) {};
73 void serde(BlobEncoder& sd) {
74 assertx(false && "Can not call serde on BuilderNoop");
78 struct BuilderCollect : public BuilderBase {
79 void addClass(const PreClassEmitter& ce, int unitSn);
80 void addFunc(const FuncEmitter& fe, int unitSn);
81 void addTypeAlias(const TypeAlias& fe, int unitSn);
82 void addConstant(const Constant& fe, int unitSn);
83 void serde(BlobEncoder& sd);
85 private:
86 CaseInsensitiveMap m_classes;
87 CaseInsensitiveMap m_funcs;
88 CaseInsensitiveMap m_typeAliases;
89 CaseSensitiveMap m_constants;
92 static std::unique_ptr<RepoAutoloadMap> serde(BlobDecoder& sd);
94 static RepoAutoloadMapBuilder::BuilderBase& get();
96 template<class Compare>
97 static void serdeMap(BlobEncoder& sd, Map<Compare> map) {
98 sd(map.size());
99 for (auto it = map.begin(); it != map.end(); ++it) {
100 sd(it->first)
101 (it->second)
106 template<class Map>
107 static Map serdeMap(BlobDecoder& sd) {
108 size_t size;
109 sd(size);
110 Map map(size);
111 for (size_t i = 0; i < size; i++) {
112 const StringData* str;
113 int64_t unitSn;
114 sd(str)
115 (unitSn)
117 map[str] = unitSn;
119 return map;
122 static Guard collect() {
123 return Guard();
126 private:
127 static void init();
128 static void fini();
131 //////////////////////////////////////////////////////////////////////
136 #endif