2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_FILE_SCOPE_H_
18 #define incl_HPHP_FILE_SCOPE_H_
20 #include "hphp/compiler/analysis/block_scope.h"
21 #include "hphp/compiler/analysis/function_container.h"
22 #include "hphp/compiler/analysis/code_error.h"
23 #include "hphp/compiler/code_generator.h"
24 #include <boost/graph/adjacency_list.hpp>
25 #include "hphp/util/json.h"
26 #include "hphp/runtime/base/md5.h"
29 ///////////////////////////////////////////////////////////////////////////////
32 DECLARE_BOOST_TYPES(StatementList
);
33 DECLARE_BOOST_TYPES(ClassScope
);
34 DECLARE_BOOST_TYPES(Location
);
35 DECLARE_BOOST_TYPES(FileScope
);
36 DECLARE_BOOST_TYPES(FunctionScope
);
39 * A FileScope stores what's parsed from one single source file. It's up to
40 * AnalysisResult objects to grab statements, functions and classes from
41 * FileScope objects to form execution paths.
43 class FileScope
: public BlockScope
,
44 public FunctionContainer
,
45 public JSON::DocTarget::ISerializable
{
48 ContainsDynamicVariable
= 0x001,
49 ContainsLDynamicVariable
= 0x002,
50 VariableArgument
= 0x004,
51 ContainsExtract
= 0x008, // contains call to extract()
52 ContainsCompact
= 0x010, // contains call to compact()
53 ContainsReference
= 0x020, // returns ref or has ref parameters
54 ReferenceVariableArgument
= 0x040, // like sscanf or fscanf
55 ContainsUnset
= 0x080, // need special handling
56 NoEffect
= 0x100, // does not side effect
57 HelperFunction
= 0x200, // runtime helper function
58 ContainsGetDefinedVars
= 0x400, // need VariableTable with getDefinedVars
59 MixedVariableArgument
= 0x800, // variable args, may or may not be ref'd
60 IsFoldable
= 0x1000,// function can be constant folded
61 NeedsActRec
= 0x2000,// builtin function needs ActRec
62 AllowOverride
= 0x4000,// allow override of systemlib or builtin
65 typedef boost::adjacency_list
<boost::setS
, boost::vecS
> Graph
;
66 typedef boost::graph_traits
<Graph
>::vertex_descriptor vertex_descriptor
;
69 FileScope(const std::string
&fileName
, int fileSize
, const MD5
&md5
);
70 ~FileScope() { delete m_redeclaredFunctions
; }
71 int getSize() const { return m_size
;}
73 // implementing FunctionContainer
74 virtual std::string
getParentName() const { assert(false); return "";}
76 const std::string
&getName() const { return m_fileName
;}
77 const MD5
& getMd5() const { return m_md5
; }
78 StatementListPtr
getStmt() const { return m_tree
;}
79 const StringToClassScopePtrVecMap
&getClasses() const {
82 void getClassesFlattened(ClassScopePtrVec
&classes
) const;
83 ClassScopePtr
getClass(const char *name
);
84 void getScopesSet(BlockScopeRawPtrQueue
&v
);
86 int getFunctionCount() const;
87 void countReturnTypes(std::map
<std::string
, int> &counts
);
88 int getClassCount() const { return m_classes
.size();}
91 void setAttribute(Attribute attr
);
92 int getGlobalAttribute() const;
95 void serialize(JSON::DocTarget::OutputStream
&out
) const;
98 * Whether this file has top level non-declaration statements that
99 * have CPP implementation.
101 ExpressionPtr
getEffectiveImpl(AnalysisResultConstPtr ar
) const;
104 * Parser functions. Parser only deals with a FileScope object, and these
105 * are the only functions a parser calls upon analysis results.
107 FunctionScopePtr
setTree(AnalysisResultConstPtr ar
, StatementListPtr tree
);
108 void cleanupForError(AnalysisResultConstPtr ar
,
109 int line
, const std::string
&msg
);
111 bool addFunction(AnalysisResultConstPtr ar
, FunctionScopePtr funcScope
);
112 bool addClass(AnalysisResultConstPtr ar
, ClassScopePtr classScope
);
113 const StringToFunctionScopePtrVecMap
*getRedecFunctions() {
114 return m_redeclaredFunctions
;
118 * For separate compilation
119 * These add edges between filescopes in the other dep graph and
120 * save the symbols for our iface.
121 * This stuff only happens in the filechanged state.
123 void addConstant(const std::string
&name
, TypePtr type
, ExpressionPtr value
,
124 AnalysisResultPtr ar
, ConstructPtr con
);
125 void declareConstant(AnalysisResultPtr ar
, const std::string
&name
);
126 void getConstantNames(std::vector
<std::string
> &names
);
127 TypePtr
getConstantType(const std::string
&name
);
129 void addIncludeDependency(AnalysisResultPtr ar
, const std::string
&file
,
131 void addClassDependency(AnalysisResultPtr ar
,
132 const std::string
&classname
);
133 void addFunctionDependency(AnalysisResultPtr ar
,
134 const std::string
&funcname
, bool byInlined
);
135 void addConstantDependency(AnalysisResultPtr ar
,
136 const std::string
&decname
);
139 * Called only by World
141 vertex_descriptor
vertex() { return m_vertex
; }
142 void setVertex(vertex_descriptor vertex
) {
146 void setModule() { m_module
= true; }
147 void setPrivateInclude() { m_privateInclude
= true; }
148 bool isPrivateInclude() const { return m_privateInclude
&& !m_externInclude
; }
149 void setExternInclude() { m_externInclude
= true; }
151 void analyzeProgram(AnalysisResultPtr ar
);
152 void analyzeIncludes(AnalysisResultPtr ar
);
153 void analyzeIncludesHelper(AnalysisResultPtr ar
);
154 bool insertClassUtil(AnalysisResultPtr ar
, ClassScopeRawPtr cls
, bool def
);
156 bool checkClass(const std::string
&cls
);
157 ClassScopeRawPtr
resolveClass(ClassScopeRawPtr cls
);
158 FunctionScopeRawPtr
resolveFunction(FunctionScopeRawPtr func
);
159 void visit(AnalysisResultPtr ar
,
160 void (*cb
)(AnalysisResultPtr
, StatementPtr
, void*),
162 const std::string
&pseudoMainName();
163 void outputFileCPP(AnalysisResultPtr ar
, CodeGenerator
&cg
);
165 bool needPseudoMainVariables() const;
166 std::string
outputFilebase() const;
168 void addPseudoMainVariable(const std::string
&name
) {
169 m_pseudoMainVariables
.insert(name
);
171 std::set
<std::string
> &getPseudoMainVariables() {
172 return m_pseudoMainVariables
;
175 FunctionScopeRawPtr
getPseudoMain() const {
179 FileScopePtr
shared_from_this() {
180 return boost::static_pointer_cast
<FileScope
>
181 (BlockScope::shared_from_this());
186 unsigned m_module
: 1;
187 unsigned m_privateInclude
: 1;
188 unsigned m_externInclude
: 1;
189 unsigned m_includeState
: 2;
191 std::vector
<int> m_attributes
;
192 std::string m_fileName
;
193 StatementListPtr m_tree
;
194 StringToFunctionScopePtrVecMap
*m_redeclaredFunctions
;
195 StringToClassScopePtrVecMap m_classes
; // name => class
196 FunctionScopeRawPtr m_pseudoMain
;
198 vertex_descriptor m_vertex
;
200 std::string m_pseudoMainName
;
201 std::set
<std::string
> m_pseudoMainVariables
;
202 BlockScopeSet m_providedDefs
;
203 std::set
<std::string
> m_redecBases
;
205 FunctionScopePtr
createPseudoMain(AnalysisResultConstPtr ar
);
206 void setFileLevel(StatementListPtr stmt
);
209 ///////////////////////////////////////////////////////////////////////////////
211 #endif // incl_HPHP_FILE_SCOPE_H_