2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_VARIABLE_TABLE_H_
18 #define incl_HPHP_VARIABLE_TABLE_H_
20 #include "hphp/compiler/analysis/symbol_table.h"
24 #include "hphp/compiler/statement/statement.h"
25 #include "hphp/compiler/analysis/class_scope.h"
26 #include "hphp/util/hash-map-typedefs.h"
29 ///////////////////////////////////////////////////////////////////////////////
31 DECLARE_BOOST_TYPES(ModifierExpression
);
32 DECLARE_BOOST_TYPES(CodeError
);
33 DECLARE_BOOST_TYPES(VariableTable
);
34 DECLARE_BOOST_TYPES(Expression
);
35 DECLARE_BOOST_TYPES(ClassScope
);
36 DECLARE_BOOST_TYPES(FunctionScope
);
39 * These are the only places that a new variable can be declared:
41 * variable = expr|variable|new obj(...)
42 * static_var_list: T_STATIC T_VARIABLE = static_scalar,...
43 * class_variable_declaration: class { T_VARIABLE = static_scalar,...}
44 * T_LIST (variable, T_LIST(...), ...) = ...
45 * try {...} catch (T obj) {...}
46 * extract(name_value_pair)
48 struct VariableTable
: SymbolTable
{
49 friend struct AssignmentExpression
;
52 ContainsDynamicVariable
= 1,
53 ContainsLDynamicVariable
= ContainsDynamicVariable
| 2,
54 InsideStaticStatement
= 16,
55 InsideGlobalStatement
= 32,
58 ContainsDynamicStatic
= 512,
61 enum AlteredVarClass
{
62 NonPrivateNonStaticVars
= 1,
63 NonPrivateStaticVars
= 2,
64 PrivateNonStaticVars
= 4,
65 PrivateStaticVars
= 8,
67 AnyNonStaticVars
= NonPrivateNonStaticVars
| PrivateNonStaticVars
,
68 AnyStaticVars
= NonPrivateStaticVars
| PrivateStaticVars
,
69 AnyNonPrivateVars
= NonPrivateNonStaticVars
| NonPrivateStaticVars
,
70 AnyPrivateVars
= PrivateNonStaticVars
| PrivateStaticVars
,
72 AnyVars
= AnyStaticVars
| AnyNonStaticVars
75 static int GetVarClassMask(bool privates
, bool statics
) {
76 return (statics
? 2 : 1) << (privates
? 2 : 0);
79 static int GetVarClassMaskForSym(const Symbol
*sym
) {
80 return GetVarClassMask(sym
->isPrivate(), sym
->isStatic());
84 explicit VariableTable(BlockScope
&blockScope
);
89 void setAttribute(Attribute attr
) { m_attribute
|= attr
;}
90 void clearAttribute(Attribute attr
) { m_attribute
&= ~attr
;}
91 bool getAttribute(Attribute attr
) const {
92 return (m_attribute
& attr
) == attr
;
95 bool isParameter(const std::string
&name
) const;
96 bool isPublic(const std::string
&name
) const;
97 bool isProtected(const std::string
&name
) const;
98 bool isPrivate(const std::string
&name
) const;
99 bool isStatic(const std::string
&name
) const;
100 bool isGlobal(const std::string
&name
) const;
101 bool isSuperGlobal(const std::string
&name
) const;
102 bool isLocal(const std::string
&name
) const;
103 bool isLocal(const Symbol
*sym
) const;
104 bool isRedeclared(const std::string
&name
) const;
105 bool isLocalGlobal(const std::string
&name
) const;
106 bool isNestedStatic(const std::string
&name
) const;
107 bool isLvalParam(const std::string
&name
) const;
108 bool isUsed(const std::string
&name
) const;
109 bool isNeeded(const std::string
&name
) const;
111 bool isPseudoMainTable() const;
112 bool hasPrivate() const;
113 bool hasNonStaticPrivate() const;
114 bool hasStatic() const { return m_hasStatic
; }
116 virtual bool isInherited(const std::string
&name
) const;
118 void getLocalVariableNames(std::vector
<std::string
> &syms
) const;
121 * Get all variable's names.
123 void getNames(std::set
<std::string
> &names
,
124 bool collectPrivate
= true) const;
126 Symbol
*addSymbol(const std::string
&name
) {
127 return genSymbol(name
, false);
130 Symbol
*addDeclaredSymbol(const std::string
&name
, ConstructPtr construct
) {
131 return genSymbol(name
, false, construct
);
135 * Add a function's parameter to this table.
137 void addParam(const std::string
&name
,
138 AnalysisResultConstPtr ar
, ConstructPtr construct
);
140 void addParamLike(const std::string
&name
,
141 AnalysisResultPtr ar
, ConstructPtr construct
,
145 * Called when a variable is declared or being assigned (l-value).
147 void add(const std::string
&name
, bool implicit
,
148 AnalysisResultConstPtr ar
, ConstructPtr construct
,
149 ModifierExpressionPtr modifiers
);
150 void add(Symbol
*sym
, bool implicit
,
151 AnalysisResultConstPtr ar
, ConstructPtr construct
,
152 ModifierExpressionPtr modifiers
);
155 * Called to note whether a class variable overrides
156 * a definition in a base class. Returns whether or not there
157 * was an error in marking as override.
159 bool markOverride(AnalysisResultPtr ar
, const std::string
&name
);
162 * Called when a variable is used or being evaluated (r-value).
164 void checkVariable(const std::string
&name
,
165 AnalysisResultConstPtr ar
, ConstructPtr construct
);
166 void checkVariable(Symbol
*sym
,
167 AnalysisResultConstPtr ar
, ConstructPtr construct
);
169 * Find the class which contains the property, and return
172 Symbol
*findProperty(ClassScopePtr
&cls
,
173 const std::string
&name
,
174 AnalysisResultConstPtr ar
);
177 * Walk up to find first parent that has the specified symbol.
179 ClassScopePtr
findParent(AnalysisResultConstPtr ar
,
180 const std::string
&name
,
181 const Symbol
*&sym
) const;
183 ClassScopePtr
findParent(AnalysisResultConstPtr ar
,
184 const std::string
&name
,
187 ClassScopePtr p
= findParent(ar
, name
, ss
); // const version
188 sym
= const_cast<Symbol
*>(ss
);
193 * Called when analyze global and static statement.
195 bool checkRedeclared(const std::string
&name
, Statement::KindOf kindOf
);
196 void addLocalGlobal(const std::string
&name
);
197 void addNestedStatic(const std::string
&name
);
200 * Helper for static variable default value
202 ConstructPtr
getStaticInitVal(std::string varName
);
203 bool setStaticInitVal(std::string varName
, ConstructPtr value
);
206 * Helper for class variable default value
208 ConstructPtr
getClassInitVal(std::string varName
);
209 bool setClassInitVal(std::string varName
, ConstructPtr value
);
212 * Called when analyze simple variable
214 void addLvalParam(const std::string
&name
);
215 void addUsed(const std::string
&name
);
216 bool checkUnused(Symbol
*sym
);
217 void addNeeded(const std::string
&name
);
219 void addStaticVariable(Symbol
*sym
, AnalysisResultConstPtr ar
,
220 bool member
= false);
221 void addStaticVariable(Symbol
*sym
, AnalysisResultPtr ar
,
222 bool member
= false);
223 void cleanupForError(AnalysisResultConstPtr ar
);
226 * Keep track of $GLOBALS['var'].
228 void addSuperGlobal(const std::string
&name
);
229 bool isConvertibleSuperGlobal(const std::string
&name
) const;
232 * Generate all variable declarations for this symbol table.
234 void outputPHP(CodeGenerator
&cg
, AnalysisResultPtr ar
);
237 enum StaticSelection
{
243 enum PrivateSelection
{
251 unsigned m_hasGlobal
: 1;
252 unsigned m_hasStatic
: 1;
253 unsigned m_hasPrivate
: 1;
254 unsigned m_hasNonStaticPrivate
: 1;
255 unsigned m_forcedVariants
: 4;
257 bool isGlobalTable(AnalysisResultConstPtr ar
) const;
260 ///////////////////////////////////////////////////////////////////////////////
263 #endif // incl_HPHP_VARIABLE_TABLE_H_