PageRenderTime 32ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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