PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/src/compiler/analysis/file_scope.h

https://github.com/kevlund/hiphop-php
C Header | 291 lines | 216 code | 35 blank | 40 comment | 0 complexity | fc1c4f62ddcc3722c4c805b72cf6991e MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010- 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 __FILE_SCOPE_H__
  17. #define __FILE_SCOPE_H__
  18. #include <compiler/analysis/block_scope.h>
  19. #include <compiler/analysis/function_container.h>
  20. #include <compiler/analysis/code_error.h>
  21. #include <compiler/code_generator.h>
  22. #include <boost/graph/adjacency_list.hpp>
  23. #include <util/json.h>
  24. namespace HPHP {
  25. ///////////////////////////////////////////////////////////////////////////////
  26. class CodeGenerator;
  27. DECLARE_BOOST_TYPES(StatementList);
  28. DECLARE_BOOST_TYPES(ClassScope);
  29. DECLARE_BOOST_TYPES(Location);
  30. DECLARE_BOOST_TYPES(FileScope);
  31. DECLARE_BOOST_TYPES(FunctionScope);
  32. /**
  33. * A FileScope stores what's parsed from one single source file. It's up to
  34. * AnalysisResult objects to grab statements, functions and classes from
  35. * FileScope objects to form execution paths.
  36. */
  37. class FileScope : public BlockScope,
  38. public FunctionContainer,
  39. public JSON::DocTarget::ISerializable {
  40. public:
  41. enum Attribute {
  42. ContainsDynamicVariable = 0x001,
  43. ContainsLDynamicVariable = 0x002,
  44. VariableArgument = 0x004,
  45. ContainsExtract = 0x008, // need special VariableTable
  46. ContainsCompact = 0x010, // need RVariableTable + exists()
  47. ContainsReference = 0x020, // returns ref or has ref parameters
  48. ReferenceVariableArgument = 0x040, // like sscanf or fscanf
  49. ContainsUnset = 0x080, // need special handling
  50. NoEffect = 0x100, // does not side effect
  51. HelperFunction = 0x200, // runtime helper function
  52. ContainsGetDefinedVars = 0x400, // need VariableTable with getDefinedVars
  53. MixedVariableArgument = 0x800, // variable args, may or may not be ref'd
  54. IsFoldable = 0x1000,// function can be constant folded
  55. };
  56. typedef boost::adjacency_list<boost::setS, boost::vecS> Graph;
  57. typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  58. public:
  59. FileScope(const std::string &fileName, int fileSize);
  60. ~FileScope() {}
  61. int getSize() const { return m_size;}
  62. // implementing FunctionContainer
  63. virtual std::string getParentName() const { ASSERT(false); return "";}
  64. const std::string &getName() const { return m_fileName;}
  65. StatementListPtr getStmt() const { return m_tree;}
  66. const StringToClassScopePtrVecMap &getClasses() const {
  67. return m_classes;
  68. }
  69. void getClassesFlattened(ClassScopePtrVec &classes) const;
  70. ClassScopePtr getClass(const char *name);
  71. virtual int getFunctionCount() const;
  72. virtual void countReturnTypes(std::map<std::string, int> &counts);
  73. int getClassCount() const { return m_classes.size();}
  74. void pushAttribute();
  75. void setAttribute(Attribute attr);
  76. int getGlobalAttribute() const;
  77. int popAttribute();
  78. void serialize(JSON::DocTarget::OutputStream &out) const;
  79. /**
  80. * Whether this file has top level non-declaration statements that
  81. * have CPP implementation.
  82. */
  83. ExpressionPtr getEffectiveImpl(AnalysisResultConstPtr ar) const;
  84. bool canUseDummyPseudoMain(AnalysisResultConstPtr ar) const;
  85. /**
  86. * Parser functions. Parser only deals with a FileScope object, and these
  87. * are the only functions a parser calls upon analysis results.
  88. */
  89. FunctionScopePtr setTree(AnalysisResultConstPtr ar, StatementListPtr tree);
  90. void cleanupForError();
  91. bool addClass(AnalysisResultConstPtr ar, ClassScopePtr classScope);
  92. void addUsedLiteralString(std::string s) {
  93. m_usedLiteralStrings.insert(s);
  94. }
  95. void addUsedLitVarString(std::string s) {
  96. m_usedLitVarStrings.insert(s);
  97. }
  98. std::set<std::string> &getUsedLiteralStrings() {
  99. return m_usedLiteralStrings;
  100. }
  101. std::set<std::string> &getUsedLitVarStrings() {
  102. return m_usedLitVarStrings;
  103. }
  104. void addUsedLiteralStringHeader(std::string s) {
  105. m_usedLiteralStringsHeader.insert(s);
  106. }
  107. void addUsedLitVarStringHeader(std::string s) {
  108. m_usedLitVarStringsHeader.insert(s);
  109. }
  110. void addUsedScalarVarInteger(int64 i) {
  111. m_usedScalarVarIntegers.insert(i);
  112. }
  113. std::set<int64> &getUsedScalarVarIntegers() {
  114. return m_usedScalarVarIntegers;
  115. }
  116. void addUsedScalarVarIntegerHeader(int64 i) {
  117. m_usedScalarVarIntegersHeader.insert(i);
  118. }
  119. void addUsedScalarVarDouble(double d) {
  120. m_usedScalarVarDoubles.insert(d);
  121. }
  122. std::set<double> &getUsedScalarVarDoubles() {
  123. return m_usedScalarVarDoubles;
  124. }
  125. void addUsedScalarVarDoubleHeader(double d) {
  126. m_usedScalarVarDoublesHeader.insert(d);
  127. }
  128. void addUsedScalarArray(std::string s) {
  129. m_usedScalarArrays.insert(s);
  130. }
  131. void addUsedScalarVarArray(std::string s) {
  132. m_usedScalarVarArrays.insert(s);
  133. }
  134. void addUsedDefaultValueScalarArray(std::string s) {
  135. m_usedDefaultValueScalarArrays.insert(s);
  136. }
  137. void addUsedDefaultValueScalarVarArray(std::string s) {
  138. m_usedDefaultValueScalarVarArrays.insert(s);
  139. }
  140. void addUsedConstHeader(const std::string &s) {
  141. m_usedConstsHeader.insert(s);
  142. }
  143. void addUsedClassConstHeader(const std::string &cls, const std::string &s) {
  144. m_usedClassConstsHeader.insert(UsedClassConst(cls, s));
  145. }
  146. void addUsedClassHeader(const std::string &s) {
  147. m_usedClassesHeader.insert(s);
  148. }
  149. void addUsedClassFullHeader(const std::string &s) {
  150. m_usedClassesFullHeader.insert(s);
  151. }
  152. /**
  153. * For separate compilation
  154. * These add edges between filescopes in the other dep graph and
  155. * save the symbols for our iface.
  156. * This stuff only happens in the filechanged state.
  157. */
  158. void addConstant(const std::string &name, TypePtr type, ExpressionPtr value,
  159. AnalysisResultPtr ar, ConstructPtr con);
  160. void declareConstant(AnalysisResultPtr ar, const std::string &name);
  161. void getConstantNames(std::vector<std::string> &names);
  162. TypePtr getConstantType(const std::string &name);
  163. void addIncludeDependency(AnalysisResultPtr ar, const std::string &file,
  164. bool byInlined);
  165. void addClassDependency(AnalysisResultPtr ar,
  166. const std::string &classname);
  167. void addFunctionDependency(AnalysisResultPtr ar,
  168. const std::string &funcname, bool byInlined);
  169. void addConstantDependency(AnalysisResultPtr ar,
  170. const std::string &decname);
  171. /**
  172. * Called only by World
  173. */
  174. vertex_descriptor vertex() { return m_vertex; }
  175. void setVertex(vertex_descriptor vertex) {
  176. m_vertex = vertex;
  177. }
  178. void analyzeProgram(AnalysisResultPtr ar);
  179. void inferTypes(AnalysisResultPtr ar);
  180. void visit(AnalysisResultPtr ar,
  181. void (*cb)(AnalysisResultPtr, StatementPtr, void*),
  182. void *data);
  183. const std::string &pseudoMainName();
  184. void outputFileCPP(AnalysisResultPtr ar, CodeGenerator &cg);
  185. bool load();
  186. bool needPseudoMainVariables() const;
  187. std::string outputFilebase() const;
  188. void addPseudoMainVariable(const std::string &name) {
  189. m_pseudoMainVariables.insert(name);
  190. }
  191. std::set<std::string> &getPseudoMainVariables() {
  192. return m_pseudoMainVariables;
  193. }
  194. FunctionScopeRawPtr getPseudoMain() const {
  195. return m_pseudoMain;
  196. }
  197. void setHasNonPrivateScope() { m_hasNonPrivateInclude = true;}
  198. void outputCPPForwardStaticDecl(CodeGenerator &cg, AnalysisResultPtr ar);
  199. void outputCPPForwardDeclHeader(CodeGenerator &cg, AnalysisResultPtr ar);
  200. void outputCPPDeclHeader(CodeGenerator &cg, AnalysisResultPtr ar);
  201. void outputCPPForwardDeclarations(CodeGenerator &cg, AnalysisResultPtr ar);
  202. void outputCPPDeclarations(CodeGenerator &cg, AnalysisResultPtr ar);
  203. void outputCPPClassHeaders(AnalysisResultPtr ar,
  204. CodeGenerator::Output output);
  205. void outputCPPForwardClassHeaders(CodeGenerator &cg, AnalysisResultPtr ar,
  206. CodeGenerator::Output output);
  207. void outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar);
  208. void outputCPPPseudoMain(CodeGenerator &cg, AnalysisResultPtr ar);
  209. void outputCPPFFI(CodeGenerator &cg, AnalysisResultPtr ar);
  210. void outputHSFFI(CodeGenerator &cg, AnalysisResultPtr ar);
  211. void outputJavaFFI(CodeGenerator &cg, AnalysisResultPtr ar);
  212. void outputJavaFFICPPStub(CodeGenerator &cg, AnalysisResultPtr ar);
  213. void outputSwigFFIStubs(CodeGenerator &cg, AnalysisResultPtr ar);
  214. FileScopePtr shared_from_this() {
  215. return boost::static_pointer_cast<FileScope>
  216. (BlockScope::shared_from_this());
  217. }
  218. private:
  219. int m_size;
  220. std::vector<int> m_attributes;
  221. std::string m_fileName;
  222. StatementListPtr m_tree;
  223. StringToClassScopePtrVecMap m_classes; // name => class
  224. ClassScopePtrVec m_ignoredClasses;
  225. FunctionScopeRawPtr m_pseudoMain;
  226. vertex_descriptor m_vertex;
  227. std::set<std::string> m_usedFuncsInline;
  228. std::set<std::string> m_usedClassesHeader;
  229. std::set<std::string> m_usedClassesFullHeader;
  230. std::set<std::string> m_usedConstsHeader;
  231. typedef std::pair<std::string, std::string> UsedClassConst;
  232. std::set<UsedClassConst> m_usedClassConstsHeader;
  233. std::set<std::string> m_usedIncludesInline;
  234. std::set<std::string> m_usedLiteralStrings;
  235. std::set<std::string> m_usedLitVarStrings;
  236. std::set<std::string> m_usedLiteralStringsHeader;
  237. std::set<std::string> m_usedLitVarStringsHeader;
  238. std::set<int64> m_usedScalarVarIntegers;
  239. std::set<int64> m_usedScalarVarIntegersHeader;
  240. std::set<double> m_usedScalarVarDoubles;
  241. std::set<double> m_usedScalarVarDoublesHeader;
  242. std::set<std::string> m_usedScalarArrays;
  243. std::set<std::string> m_usedScalarVarArrays;
  244. std::set<std::string> m_usedDefaultValueScalarArrays;
  245. std::set<std::string> m_usedDefaultValueScalarVarArrays;
  246. std::string m_pseudoMainName;
  247. std::set<std::string> m_pseudoMainVariables;
  248. bool m_hasNonPrivateInclude;
  249. FunctionScopePtr createPseudoMain(AnalysisResultConstPtr ar);
  250. void setFileLevel(StatementListPtr stmt);
  251. void outputCPPHelper(CodeGenerator &cg, AnalysisResultPtr ar,
  252. bool classes = true);
  253. };
  254. ///////////////////////////////////////////////////////////////////////////////
  255. }
  256. #endif // __FILE_SCOPE_H__