Update the MSVC compiler options
[hiphop-php.git] / hphp / compiler / analysis / block_scope.h
blob351323201db0430a273a688d60a4ac6160541e24
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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_BLOCK_SCOPE_H_
18 #define incl_HPHP_BLOCK_SCOPE_H_
20 #include "hphp/compiler/hphp.h"
22 #include "hphp/util/bits.h"
23 #include "hphp/util/lock.h"
25 #include <boost/noncopyable.hpp>
26 #include <tbb/concurrent_hash_map.h>
28 #include <list>
29 #include <memory>
30 #include <set>
31 #include <utility>
32 #include <vector>
34 namespace HPHP {
35 ///////////////////////////////////////////////////////////////////////////////
37 class CodeGenerator;
38 DECLARE_BOOST_TYPES(Statement);
39 DECLARE_BOOST_TYPES(AnalysisResult);
40 DECLARE_BOOST_TYPES(VariableTable);
41 DECLARE_BOOST_TYPES(ConstantTable);
42 DECLARE_BOOST_TYPES(ModifierExpression);
43 DECLARE_BOOST_TYPES(IncludeExpression);
44 DECLARE_BOOST_TYPES(StatementList);
45 DECLARE_BOOST_TYPES(BlockScope);
46 DECLARE_BOOST_TYPES(ClassScope);
47 DECLARE_BOOST_TYPES(FunctionScope);
48 DECLARE_BOOST_TYPES(FileScope);
50 typedef hphp_hash_map<BlockScopeRawPtr, int,
51 smart_pointer_hash<BlockScopeRawPtr>
52 > BlockScopeRawPtrFlagsHashMap;
53 typedef hphp_hash_set<BlockScopeRawPtr,
54 smart_pointer_hash<BlockScopeRawPtr>
55 > BlockScopeRawPtrHashSet;
57 typedef std::list<BlockScopeRawPtr> BlockScopeRawPtrQueue;
58 typedef std::vector<BlockScopeRawPtrFlagsHashMap::
59 value_type*> BlockScopeRawPtrFlagsVec;
60 typedef std::pair< BlockScopeRawPtr, int* >
61 BlockScopeRawPtrFlagsPtrPair;
62 typedef std::vector< std::pair< BlockScopeRawPtr, int* > >
63 BlockScopeRawPtrFlagsPtrVec;
65 /**
66 * Base class of ClassScope and FunctionScope.
68 class BlockScope : private boost::noncopyable,
69 public std::enable_shared_from_this<BlockScope> {
70 public:
71 enum KindOf {
72 ClassScope,
73 FunctionScope,
74 FileScope,
75 ProgramScope,
78 enum UseKinds {
79 /* Callers */
80 UseKindCallerParam = 0x1 << 1,
81 UseKindCallerReturn = 0x1 << 2,
82 UseKindCaller = (UseKindCallerParam |
83 UseKindCallerReturn),
85 /* Static references */
86 UseKindStaticRef = 0x1 << 3,
88 /* Constants */
89 UseKindConstRef = 0x1 << 20,
91 /* Other types */
92 UseKindParentRef = 0x1 << 21,
93 UseKindInclude = 0x1 << 22,
94 UseKindClosure = 0x1 << 23,
95 UseKindAny = (unsigned)-1
98 enum Marks {
99 MarkWaitingInQueue,
100 MarkProcessingDeps,
101 MarkReady,
102 MarkWaiting,
103 MarkProcessing,
104 MarkProcessedInQueue,
105 MarkProcessed
108 BlockScope(const BlockScope&) = delete;
109 BlockScope& operator=(const BlockScope&) = delete;
110 BlockScope(const std::string &name, const std::string &docComment,
111 StatementPtr stmt, KindOf kind);
112 virtual ~BlockScope() {}
113 bool is(KindOf kind) const { return kind == m_kind;}
114 const std::string &getScopeName() const { return m_scopeName;}
115 virtual bool isBuiltin() const { return false; }
116 StatementPtr getStmt() const { return m_stmt;}
117 VariableTableConstPtr getVariables() const { return m_variables;}
118 ConstantTableConstPtr getConstants() const { return m_constants;}
119 VariableTablePtr getVariables() { return m_variables;}
120 ConstantTablePtr getConstants() { return m_constants;}
121 ClassScopeRawPtr getContainingClass();
122 FunctionScopeRawPtr getContainingNonClosureFunction();
123 FunctionScopeRawPtr getContainingFunction() const {
124 return FunctionScopeRawPtr(is(FunctionScope) ?
125 (HPHP::FunctionScope*)this : 0);
127 FileScopeRawPtr getContainingFile();
128 AnalysisResultRawPtr getContainingProgram();
130 ClassScopeRawPtr findExactClass(ClassScopeRawPtr cls);
132 bool hasUser(BlockScopeRawPtr user, int useFlags) const;
133 void addUse(BlockScopeRawPtr user, int useFlags);
136 * Helpers for keeping track of break/continue nested level.
138 void incLoopNestedLevel();
139 void decLoopNestedLevel();
140 int getLoopNestedLevel() const { return m_loopNestedLevel;}
142 void setClassInfoAttribute(int flag) {
143 m_attributeClassInfo |= flag;
145 const std::string &getDocComment() const { return m_docComment;}
146 void setDocComment(const std::string &doc) { m_docComment = doc;}
149 * Code gen
151 virtual void outputPHP(CodeGenerator &cg, AnalysisResultPtr ar);
153 virtual bool inPseudoMain() const {
154 return false;
157 virtual ClassScopePtr getParentScope(AnalysisResultConstPtr ar) const {
158 return ClassScopePtr();
161 void setOuterScope(BlockScopePtr o) { m_outerScope = o; }
162 BlockScopePtr getOuterScope() { return m_outerScope.lock(); }
163 bool isOuterScope() { return m_outerScope.expired(); }
165 const BlockScopeRawPtrFlagsPtrVec &getDeps() const {
166 return m_orderedDeps;
168 const BlockScopeRawPtrFlagsVec &getOrderedUsers() const {
169 return m_orderedUsers;
172 void setMark(Marks m) { m_mark = m; }
173 Marks getMark() const { return m_mark; }
175 void setLockedMark(Marks m) { Lock l(s_jobStateMutex); m_mark = m; }
176 Marks getLockedMark() const { Lock l(s_jobStateMutex); return m_mark; }
178 void incPass() { m_pass++; }
179 bool isFirstPass() const { return !m_pass; }
181 void incRunId() { m_runId++; }
182 int getRunId() const { return m_runId; }
184 void clearUpdated() { m_updated = 0; }
185 void addUpdates(int f);
186 int getUpdated() const { return m_updated; }
188 void setRescheduleFlags(int rescheduleFlags) {
189 m_rescheduleFlags = rescheduleFlags;
191 int rescheduleFlags() const { return m_rescheduleFlags; }
193 void incEffectsTag() { m_effectsTag++; }
194 int getEffectsTag() const { return m_effectsTag; }
196 Mutex &getMutex() { return m_mutex; }
198 void setNumDepsToWaitFor(int n) {
199 assert(n >= 0);
200 m_numDepsToWaitFor = n;
202 int getNumDepsToWaitFor() const {
203 return m_numDepsToWaitFor;
205 int incNumDepsToWaitFor() {
206 return ++m_numDepsToWaitFor;
208 int decNumDepsToWaitFor() {
209 assert(m_numDepsToWaitFor > 0);
210 return --m_numDepsToWaitFor;
213 void setForceRerun(bool v) { m_forceRerun = v; }
214 bool forceRerun() const { return m_forceRerun; }
216 int selfUser() const { return m_selfUser; }
218 protected:
219 std::string m_scopeName;
220 int m_attributeClassInfo;
221 std::string m_docComment;
222 StatementPtr m_stmt;
223 KindOf m_kind;
224 VariableTablePtr m_variables;
225 ConstantTablePtr m_constants;
226 BlockScopeRawPtr m_outerScope;
228 int m_loopNestedLevel;
229 int m_pass;
230 int m_updated;
231 int m_runId;
232 private:
233 Marks m_mark;
234 BlockScopeRawPtrFlagsPtrVec m_orderedDeps;
235 BlockScopeRawPtrFlagsVec m_orderedUsers;
236 BlockScopeRawPtrFlagsHashMap m_userMap;
237 int m_effectsTag;
238 int m_numDepsToWaitFor;
239 Mutex m_mutex;
240 bool m_forceRerun; /* do we need to be re-run (allows deps to run during
241 * re-schedule) */
242 int m_rescheduleFlags; /* who do we need to run after a re-schedule */
243 int m_selfUser;
244 public:
245 static Mutex s_jobStateMutex;
246 static Mutex s_depsMutex;
247 static Mutex s_constMutex;
250 ///////////////////////////////////////////////////////////////////////////////
252 #endif // incl_HPHP_BLOCK_SCOPE_H_