PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/vm/preclass-emitter.h

https://gitlab.com/0072016/0072016-PHP.LLC
C Header | 305 lines | 242 code | 40 blank | 23 comment | 2 complexity | d5b38ee3303d770e10f657db8e2ce98b MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2015 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. +----------------------------------------------------------------------+
  15. */
  16. #ifndef incl_HPHP_VM_CLASS_EMIT_H_
  17. #define incl_HPHP_VM_CLASS_EMIT_H_
  18. #include "hphp/runtime/base/repo-auth-type.h"
  19. #include "hphp/runtime/base/types.h"
  20. #include "hphp/runtime/base/array-data.h"
  21. #include "hphp/runtime/vm/class.h"
  22. #include "hphp/runtime/vm/func.h"
  23. #include "hphp/runtime/vm/func-emitter.h"
  24. #include "hphp/runtime/vm/repo-helpers.h"
  25. #include <vector>
  26. namespace HPHP {
  27. ///////////////////////////////////////////////////////////////////////////////
  28. struct UnitEmitter;
  29. ///////////////////////////////////////////////////////////////////////////////
  30. /*
  31. * Information about an extension subclass of ObjectData.
  32. */
  33. struct BuiltinObjExtents {
  34. // Total sizeof(c_Foo).
  35. size_t totalSizeBytes;
  36. // The offset of the ObjectData subobject in c_Foo.
  37. ptrdiff_t odOffsetBytes;
  38. };
  39. class PreClassEmitter {
  40. public:
  41. typedef std::vector<FuncEmitter*> MethodVec;
  42. class Prop {
  43. public:
  44. Prop()
  45. : m_name(nullptr)
  46. , m_mangledName(nullptr)
  47. , m_attrs(AttrNone)
  48. , m_typeConstraint(nullptr)
  49. , m_docComment(nullptr)
  50. , m_repoAuthType{}
  51. {}
  52. Prop(const PreClassEmitter* pce,
  53. const StringData* n,
  54. Attr attrs,
  55. const StringData* typeConstraint,
  56. const StringData* docComment,
  57. const TypedValue* val,
  58. RepoAuthType repoAuthType);
  59. ~Prop();
  60. const StringData* name() const { return m_name; }
  61. const StringData* mangledName() const { return m_mangledName; }
  62. Attr attrs() const { return m_attrs; }
  63. const StringData* typeConstraint() const { return m_typeConstraint; }
  64. const StringData* docComment() const { return m_docComment; }
  65. const TypedValue& val() const { return m_val; }
  66. RepoAuthType repoAuthType() const { return m_repoAuthType; }
  67. template<class SerDe> void serde(SerDe& sd) {
  68. sd(m_name)
  69. (m_mangledName)
  70. (m_attrs)
  71. (m_typeConstraint)
  72. (m_docComment)
  73. (m_val)
  74. (m_repoAuthType)
  75. ;
  76. }
  77. private:
  78. LowStringPtr m_name;
  79. LowStringPtr m_mangledName;
  80. Attr m_attrs;
  81. LowStringPtr m_typeConstraint;
  82. LowStringPtr m_docComment;
  83. TypedValue m_val;
  84. RepoAuthType m_repoAuthType;
  85. };
  86. class Const {
  87. public:
  88. Const()
  89. : m_name(nullptr)
  90. , m_typeConstraint(nullptr)
  91. , m_val(make_tv<KindOfUninit>())
  92. , m_phpCode(nullptr)
  93. , m_typeconst(false)
  94. {}
  95. Const(const StringData* n, const StringData* typeConstraint,
  96. const TypedValue* val, const StringData* phpCode,
  97. const bool typeconst)
  98. : m_name(n), m_typeConstraint(typeConstraint), m_phpCode(phpCode),
  99. m_typeconst(typeconst) {
  100. if (!val) {
  101. m_val.clear();
  102. } else {
  103. m_val = *val;
  104. }
  105. }
  106. ~Const() {}
  107. const StringData* name() const { return m_name; }
  108. const StringData* typeConstraint() const { return m_typeConstraint; }
  109. const TypedValue& val() const { return m_val.value(); }
  110. const folly::Optional<TypedValue>& valOption() const { return m_val; }
  111. const StringData* phpCode() const { return m_phpCode; }
  112. bool isAbstract() const { return !m_val.hasValue(); }
  113. bool isTypeconst() const { return m_typeconst; }
  114. template<class SerDe> void serde(SerDe& sd) {
  115. sd(m_name)
  116. (m_val)
  117. (m_phpCode)
  118. (m_typeconst);
  119. }
  120. private:
  121. LowStringPtr m_name;
  122. LowStringPtr m_typeConstraint;
  123. folly::Optional<TypedValue> m_val;
  124. LowStringPtr m_phpCode;
  125. bool m_typeconst;
  126. };
  127. typedef IndexedStringMap<Prop, true, Slot> PropMap;
  128. typedef IndexedStringMap<Const, true, Slot> ConstMap;
  129. PreClassEmitter(UnitEmitter& ue, Id id, const StringData* n,
  130. PreClass::Hoistable hoistable);
  131. ~PreClassEmitter();
  132. void init(int line1, int line2, Offset offset, Attr attrs,
  133. const StringData* parent, const StringData* docComment);
  134. UnitEmitter& ue() const { return m_ue; }
  135. const StringData* name() const { return m_name; }
  136. Attr attrs() const { return m_attrs; }
  137. void setAttrs(Attr attrs) { m_attrs = attrs; }
  138. void setHoistable(PreClass::Hoistable h) { m_hoistable = h; }
  139. PreClass::Hoistable hoistability() const { return m_hoistable; }
  140. void setOffset(Offset off) { m_offset = off; }
  141. void setEnumBaseTy(TypeConstraint ty) { m_enumBaseTy = ty; }
  142. const TypeConstraint& enumBaseTy() const { return m_enumBaseTy; }
  143. Id id() const { return m_id; }
  144. int32_t numDeclMethods() const { return m_numDeclMethods; }
  145. void setNumDeclMethods(uint32_t n) { m_numDeclMethods = n; }
  146. void setIfaceVtableSlot(Slot s) { m_ifaceVtableSlot = s; }
  147. const MethodVec& methods() const { return m_methods; }
  148. FuncEmitter* findMethod(const StringData* name) { return m_methodMap[name]; }
  149. const PropMap::Builder& propMap() const { return m_propMap; }
  150. const ConstMap::Builder& constMap() const { return m_constMap; }
  151. const StringData* docComment() const { return m_docComment; }
  152. const StringData* parentName() const { return m_parent; }
  153. void addInterface(const StringData* n);
  154. const std::vector<LowStringPtr>& interfaces() const {
  155. return m_interfaces;
  156. }
  157. bool addMethod(FuncEmitter* method);
  158. void renameMethod(const StringData* oldName, const StringData *newName);
  159. bool addProperty(const StringData* n,
  160. Attr attrs,
  161. const StringData* typeConstraint,
  162. const StringData* docComment,
  163. const TypedValue* val,
  164. RepoAuthType);
  165. const Prop& lookupProp(const StringData* propName) const;
  166. bool addConstant(const StringData* n, const StringData* typeConstraint,
  167. const TypedValue* val, const StringData* phpCode,
  168. const bool typeConst = false,
  169. const ArrayData* typeStructure = nullptr);
  170. bool addAbstractConstant(const StringData* n,
  171. const StringData* typeConstraint,
  172. const bool typeConst = false);
  173. void addUsedTrait(const StringData* traitName);
  174. void addClassRequirement(const PreClass::ClassRequirement req) {
  175. m_requirements.push_back(req);
  176. }
  177. void addTraitPrecRule(const PreClass::TraitPrecRule &rule);
  178. void addTraitAliasRule(const PreClass::TraitAliasRule &rule);
  179. const std::vector<LowStringPtr>& usedTraits() const {
  180. return m_usedTraits;
  181. }
  182. const std::vector<PreClass::ClassRequirement>& requirements() const {
  183. return m_requirements;
  184. }
  185. const std::vector<PreClass::TraitAliasRule>& traitAliasRules() const {
  186. return m_traitAliasRules;
  187. }
  188. const std::vector<PreClass::TraitPrecRule>& traitPrecRules() const {
  189. return m_traitPrecRules;
  190. }
  191. void addUserAttribute(const StringData* name, TypedValue tv);
  192. void setUserAttributes(UserAttributeMap map) {
  193. m_userAttributes = std::move(map);
  194. }
  195. UserAttributeMap userAttributes() const { return m_userAttributes; }
  196. void commit(RepoTxn& txn) const;
  197. void setBuiltinClassInfo(const ClassInfo* info,
  198. BuiltinCtorFunction ctorFunc,
  199. BuiltinDtorFunction dtorFunc,
  200. BuiltinObjExtents extents);
  201. PreClass* create(Unit& unit) const;
  202. template<class SerDe> void serdeMetaData(SerDe&);
  203. std::pair<int,int> getLocation() const {
  204. return std::make_pair(m_line1, m_line2);
  205. }
  206. int getNextMemoizeCacheKey() {
  207. return m_memoizeInstanceSerial++;
  208. }
  209. private:
  210. typedef hphp_hash_map<LowStringPtr,
  211. FuncEmitter*,
  212. string_data_hash,
  213. string_data_isame> MethodMap;
  214. UnitEmitter& m_ue;
  215. int m_line1;
  216. int m_line2;
  217. Offset m_offset; // offset of the DefCls for this class
  218. LowStringPtr m_name;
  219. Attr m_attrs;
  220. LowStringPtr m_parent;
  221. LowStringPtr m_docComment;
  222. TypeConstraint m_enumBaseTy;
  223. Id m_id;
  224. PreClass::Hoistable m_hoistable;
  225. BuiltinCtorFunction m_instanceCtor{nullptr};
  226. BuiltinDtorFunction m_instanceDtor{nullptr};
  227. uint32_t m_builtinObjSize{0};
  228. int32_t m_builtinODOffset{0};
  229. int32_t m_numDeclMethods{-1};
  230. Slot m_ifaceVtableSlot{kInvalidSlot};
  231. int m_memoizeInstanceSerial{0};
  232. std::vector<LowStringPtr> m_interfaces;
  233. std::vector<LowStringPtr> m_usedTraits;
  234. std::vector<PreClass::ClassRequirement> m_requirements;
  235. std::vector<PreClass::TraitPrecRule> m_traitPrecRules;
  236. std::vector<PreClass::TraitAliasRule> m_traitAliasRules;
  237. UserAttributeMap m_userAttributes;
  238. MethodVec m_methods;
  239. MethodMap m_methodMap;
  240. PropMap::Builder m_propMap;
  241. ConstMap::Builder m_constMap;
  242. };
  243. class PreClassRepoProxy : public RepoProxy {
  244. friend class PreClass;
  245. friend class PreClassEmitter;
  246. public:
  247. explicit PreClassRepoProxy(Repo& repo);
  248. ~PreClassRepoProxy();
  249. void createSchema(int repoId, RepoTxn& txn);
  250. struct InsertPreClassStmt : public RepoProxy::Stmt {
  251. InsertPreClassStmt(Repo& repo, int repoId) : Stmt(repo, repoId) {}
  252. void insert(const PreClassEmitter& pce, RepoTxn& txn, int64_t unitSn,
  253. Id preClassId, const StringData* name,
  254. PreClass::Hoistable hoistable);
  255. };
  256. struct GetPreClassesStmt : public RepoProxy::Stmt {
  257. GetPreClassesStmt(Repo& repo, int repoId) : Stmt(repo, repoId) {}
  258. void get(UnitEmitter& ue);
  259. };
  260. InsertPreClassStmt insertPreClass[RepoIdCount];
  261. GetPreClassesStmt getPreClasses[RepoIdCount];
  262. };
  263. ///////////////////////////////////////////////////////////////////////////////
  264. }
  265. #endif