fix typo serializing additionalTextEdits
[hiphop-php.git] / hphp / hhbbc / index.h
blobd54453129edac67418783da8d01c7ae48859046b
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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 +----------------------------------------------------------------------+
16 #ifndef incl_HHBBC_INDEX_H_
17 #define incl_HHBBC_INDEX_H_
19 #include <memory>
20 #include <tuple>
21 #include <vector>
22 #include <map>
23 #include <exception>
25 #include <boost/variant.hpp>
26 #include <tbb/concurrent_hash_map.h>
28 #include <folly/synchronization/Baton.h>
29 #include <folly/Optional.h>
30 #include <folly/Hash.h>
32 #include "hphp/util/compact-vector.h"
33 #include "hphp/util/either.h"
34 #include "hphp/runtime/base/repo-auth-type-array.h"
35 #include "hphp/runtime/vm/type-constraint.h"
37 #include "hphp/hhbbc/hhbbc.h"
38 #include "hphp/hhbbc/misc.h"
40 namespace HPHP { namespace HHBBC {
42 //////////////////////////////////////////////////////////////////////
44 struct Type;
45 struct Index;
46 struct PublicSPropMutations;
47 struct FuncAnalysisResult;
48 struct Context;
49 struct ContextHash;
50 struct CallContext;
52 extern const Type TTop;
54 namespace php {
55 struct Class;
56 struct Const;
57 struct Func;
58 struct Unit;
59 struct Program;
62 //////////////////////////////////////////////////////////////////////
65 * This module contains functions for building and querying an Index
66 * of data relating to "resolved" versions of the names in of a
67 * php::Program. It also records dependencies so it is possible to
68 * tell which parts of the program may be interested in new inferred
69 * information about other parts of the program.
71 * The main entry point here is the Index class. The Index is built
72 * after parse time, and then analysis can query it for information.
75 //////////////////////////////////////////////////////////////////////
78 * A DependencyContext encodes enough of the context to record a dependency - a
79 * php::Func, if we're doing private property analysis and its a suitable class,
80 * a php::Class, or a public static property with a particular name.
83 enum class DependencyContextType : uint16_t {
84 Func,
85 Class,
86 PropName
89 using DependencyContext = CompactTaggedPtr<const void, DependencyContextType>;
91 struct DependencyContextLess {
92 bool operator()(const DependencyContext& a,
93 const DependencyContext& b) const {
94 return a.getOpaque() < b.getOpaque();
98 struct DependencyContextEquals {
99 bool operator()(const DependencyContext& a,
100 const DependencyContext& b) const {
101 return a.getOpaque() == b.getOpaque();
105 struct DependencyContextHash {
106 size_t operator()(const DependencyContext& d) const {
107 return pointer_hash<void>{}(reinterpret_cast<void*>(d.getOpaque()));
111 struct DependencyContextHashCompare : DependencyContextHash {
112 bool equal(const DependencyContext& a, const DependencyContext& b) const {
113 return a.getOpaque() == b.getOpaque();
115 size_t hash(const DependencyContext& d) const { return (*this)(d); }
118 using DependencyContextSet = hphp_hash_set<DependencyContext,
119 DependencyContextHash,
120 DependencyContextEquals>;
121 using ContextSet = hphp_hash_set<Context, ContextHash>;
123 std::string show(Context);
125 using ConstantMap = hphp_hash_map<SString, Cell>;
128 * State of properties on a class. Map from property name to its
129 * Type.
131 template <typename T = Type>
132 struct PropStateElem {
133 T ty;
134 const TypeConstraint* tc = nullptr;
136 bool operator==(const PropStateElem<T>& o) const {
137 return ty == o.ty && tc == o.tc;
140 using PropState = std::map<LSString,PropStateElem<>>;
142 //////////////////////////////////////////////////////////////////////
144 // private types
145 struct ClassInfo;
147 //////////////////////////////////////////////////////////////////////
150 * References to "resolved" entities with information in the index are
151 * in the res:: namespace.
153 * These represent handles to program entities that may have variable
154 * amounts of information. For example, we may know the name of a
155 * class in a res::Class, but do not know for sure which php::Class
156 * struct is actually associated with it.
158 namespace res {
161 * A resolved runtime Class, for a particular php::Class.
163 * Provides various lookup tables that allow querying the Class'
164 * information.
166 struct Class {
168 * Returns whether two classes are definitely same at runtime. If
169 * this function returns false, they still *may* be the same at
170 * runtime.
172 bool same(const Class&) const;
175 * Returns true if this class is definitely going to be a subtype
176 * of `o' at runtime. If this function returns false, this may
177 * still be a subtype of `o' at runtime, it just may not be known.
178 * A typical example is with "non unique" classes.
180 bool subtypeOf(const Class& o) const;
183 * If this function return false, it is known that this class
184 * is in no subtype relationship with the argument Class 'o'.
185 * Returns true if this class could be a subtype of `o' at runtime.
186 * When true is returned the two classes may still be unrelated but it is
187 * not possible to tell. A typical example is with "non unique" classes.
189 bool couldBe(const Class& o) const;
192 * Returns the name of this class. Non-null guarantee.
194 SString name() const;
197 * Whether this class could possibly be an interface/interface or trait.
199 * True means it might be, false means it is not.
201 bool couldBeInterface() const;
202 bool couldBeInterfaceOrTrait() const;
205 * Returns whether this type has the no override attribute, that is, if it
206 * is a final class (explicitly marked by the user or known by the static
207 * analysis).
209 * When returning false the class is guaranteed to be final. When returning
210 * true the system cannot tell though the class may still be final.
212 bool couldBeOverriden() const;
215 * Whether this class (or its subtypes) could possibly have have
216 * certain magic methods.
218 bool couldHaveMagicGet() const;
221 * Whether this class (or its subtypes) could possibly have have
222 * a magic toBoolean() method.
224 bool couldHaveMagicBool() const;
227 * Whether this class could possibly have a derived class that is mocked.
228 * Including itself.
230 bool couldHaveMockedDerivedClass() const;
233 * Whether this class could possibly be mocked.
235 bool couldBeMocked() const;
238 * Whether this class could have reified generics
240 bool couldHaveReifiedGenerics() const;
243 * Returns whether this resolved class might distinguish being constructed
244 * dynamically versus being constructed normally (IE, might raise a notice).
246 bool mightCareAboutDynConstructs() const;
249 * Returns the Class that is the first common ancestor between 'this' and 'o'.
250 * If there is no common ancestor folly::none is returned
252 folly::Optional<Class> commonAncestor(const Class& o) const;
255 * Returns the res::Class for this Class's parent if there is one,
256 * or nullptr.
258 folly::Optional<Class> parent() const;
261 * Returns true if we have a ClassInfo for this Class.
263 bool resolved() const {
264 return val.right() != nullptr;
268 * Returns the php::Class for this Class if there is one, or
269 * nullptr.
271 const php::Class* cls() const;
273 private:
274 Class(const Index*, Either<SString,ClassInfo*>);
276 private:
277 friend std::string show(const Class&);
278 friend struct ::HPHP::HHBBC::Index;
279 friend struct ::HPHP::HHBBC::PublicSPropMutations;
280 const Index* index;
281 Either<SString,ClassInfo*> val;
285 * This is an abstraction layer to represent possible runtime function
286 * resolutions.
288 * Internally, this may only know the name of the function (or method), or we
289 * may know exactly which source-code-level function it refers to, or we may
290 * only have ruled it down to one of a few functions in a class hierarchy. The
291 * interpreter can treat all these cases the same way using this.
293 struct Func {
295 * Returns whether two res::Funcs definitely mean the func at
296 * runtime.
298 * Note: this is potentially pessimistic for its use in ActRec state
299 * merging right now, but not incorrect.
301 bool same(const Func&) const;
304 * Returns the name of this function. Non-null guarantee.
306 SString name() const;
309 * If this resolved function represents exactly one php::Func, return it.
311 const php::Func* exactFunc() const;
314 * Returns whether this resolved function could possibly be going through a
315 * magic call, in the magic way.
317 * That is, if was resolved as part of a direct call to an __call method,
318 * this will say true. If it was resolved as part as some normal method
319 * call, and we haven't proven that there's no way an __call dispatch could
320 * be involved, this will say false.
322 bool cantBeMagicCall() const;
325 * Returns whether this resolved function is definitely safe to constant fold.
327 bool isFoldable() const;
330 * Whether this function could have reified generics
332 bool couldHaveReifiedGenerics() const;
335 * Returns whether this resolved function might distinguish being called
336 * dynamically versus being called normally (IE, might raise a notice).
338 bool mightCareAboutDynCalls() const;
341 * Returns whether this resolved function might be a builtin.
343 bool mightBeBuiltin() const;
345 struct FuncInfo;
346 struct MethTabEntryPair;
347 struct FuncFamily;
349 private:
350 friend struct ::HPHP::HHBBC::Index;
351 struct FuncName {
352 FuncName(SString n, bool r) : name{n}, renamable{r} {}
353 bool operator==(FuncName o) const { return name == o.name; }
354 SString name;
355 bool renamable;
357 struct MethodName {
358 bool operator==(MethodName o) const { return name == o.name; }
359 SString name;
361 using Rep = boost::variant< FuncName
362 , MethodName
363 , FuncInfo*
364 , const MethTabEntryPair*
365 , FuncFamily*
368 private:
369 Func(const Index*, Rep);
370 friend std::string show(const Func&);
372 private:
373 const Index* index;
374 Rep val;
378 * Produce a trace-able string for a res::Func or res::Class.
380 std::string show(const Func&);
381 std::string show(const Class&);
385 //////////////////////////////////////////////////////////////////////
388 * This class encapsulates the known facts about the program, with a
389 * whole-program view.
391 * This structure contains unowned pointers into the php::Program it
392 * was created for. It should not out-live the Program.
394 * The const member functions of this class are thread safe for
395 * concurrent reads and writes. The non-const functions should be
396 * called in a single threaded context only (they are used during the
397 * "update" step in between whole program analysis rounds).
399 struct Index {
401 * Throwing a rebuild exception indicates that the index needs to
402 * be rebuilt.
404 * The exception should be passed to the Index constructor.
406 struct rebuild : std::exception {
407 explicit rebuild(std::vector<std::pair<SString, SString>> ca) :
408 class_aliases(std::move(ca)) {}
409 private:
410 friend struct Index;
411 std::vector<std::pair<SString, SString>> class_aliases;
415 * Create an Index for a php::Program. Performs some initial
416 * analysis of the program.
418 explicit Index(php::Program*, rebuild* = nullptr);
421 * This class must not be destructed after its associated
422 * php::Program.
424 ~Index();
427 * The index operates in two modes: frozen, and unfrozen.
429 * Conceptually, the index is mutable and may acquire new
430 * information until it has been frozen, and once frozen, it retains
431 * the information it had at the point it was frozen.
433 * The reason this exists is because certain functions on the index
434 * may cause it to need to consult information in the bodies of
435 * functions other than the Context passed in. Specifically, if the
436 * interpreter tries to look up the return type for a callee in a
437 * given CallContext, the index may choose to recursively invoke
438 * type inference on that callee's function body to see if more
439 * precise information can be determined, unless it is frozen.
441 * This is fine until the final pass, because all bytecode is
442 * read-only at that stage. However, in the final pass, other
443 * threads might be optimizing a callee's bytecode and changing it,
444 * so we should not be reading from it to perform type inference
445 * concurrently. Freezing the index tells it it can't do that
446 * anymore.
448 * These are the functions to query and transition to frozen state.
450 bool frozen() const;
451 void freeze();
452 void thaw();
455 * Throw away data structures that won't be needed during the emit
456 * stage (or beyond).
458 void cleanup_for_emit(folly::Baton<>* done);
461 * The Index contains a Builder for an ArrayTypeTable.
463 * If we're creating assert types with options.InsertAssertions, we
464 * need to keep track of which array types exist in the whole
465 * program in order to include it in the repo.
467 std::unique_ptr<ArrayTypeTable::Builder>& array_table_builder() const;
470 * Find all the closures created inside the context of a given
471 * php::Class.
473 const CompactVector<const php::Class*>*
474 lookup_closures(const php::Class*) const;
477 * Find all the extra methods associated with a class from its
478 * traits.
480 const hphp_fast_set<php::Func*>*
481 lookup_extra_methods(const php::Class*) const;
484 * Attempt to record a new alias in the index. May be called from
485 * multi-threaded contexts, so doesn't actually update the index
486 * (call update_class_aliases to do that). Returns false if it would
487 * violate any current uniqueness assumptions.
489 bool register_class_alias(SString orig, SString alias) const;
492 * Add any aliases that have been registered since the last call to
493 * update_class_aliases to the index. Must be called from a single
494 * threaded context.
496 void update_class_aliases();
499 * Try to find a res::Class for a given php::Class.
501 * Note, the returned class may or may not be *defined* at the
502 * program point you care about (it could be non-hoistable, even
503 * though it's unique, for example).
505 * Returns a name-only resolution if there are no legal
506 * instantiations of the class, or if there is more than one.
508 res::Class resolve_class(const php::Class*) const;
511 * Try to resolve which class will be the class named `name' from a
512 * given context, if we can resolve it to a single class.
514 * Note, the returned class may or may not be *defined* at the
515 * program point you care about (it could be non-hoistable, even
516 * though it's unique, for example).
518 * Returns folly::none if we can't prove the supplied name must be a
519 * object type. (E.g. if there are type aliases.)
521 folly::Optional<res::Class> resolve_class(Context, SString name) const;
524 * Try to resolve self/parent types for the given context
526 folly::Optional<res::Class> selfCls(const Context& ctx) const;
527 folly::Optional<res::Class> parentCls(const Context& ctx) const;
529 template <typename T>
530 struct ResolvedInfo {
531 AnnotType type;
532 bool nullable;
533 T value;
537 * Try to resolve name, looking through TypeAliases and enums.
539 ResolvedInfo<folly::Optional<res::Class>>
540 resolve_type_name(SString name) const;
543 * Resolve a closure class.
545 * Returns both a resolved Class, and the actual php::Class for the
546 * closure.
548 std::pair<res::Class,php::Class*>
549 resolve_closure_class(Context ctx, int32_t idx) const;
552 * Return a resolved class for a builtin class.
554 * Pre: `name' must be the name of a class defined in a systemlib.
556 res::Class builtin_class(SString name) const;
559 * Try to resolve a function named `name' from a given context.
561 * Note, the returned function may or may not be defined at the
562 * program point (it could require a function autoload that might
563 * fail).
565 res::Func resolve_func(Context, SString name) const;
568 * Try to resolve a class method named `name' with a given Context
569 * and class type.
571 * Pre: clsType.subtypeOf(BCls)
573 res::Func resolve_method(Context, Type clsType, SString name) const;
576 * Try to resolve a class constructor for the supplied class type.
578 * Returns: folly::none if it can't at least figure out a func
579 * family for the call.
581 folly::Optional<res::Func>
582 resolve_ctor(Context, res::Class rcls, bool exact) const;
585 * Give the Type in our type system that matches an hhvm
586 * TypeConstraint, subject to the information in this Index.
588 * This function returns a subtype of Cell, although TypeConstraints
589 * at runtime can match reference parameters. The caller should
590 * make sure to handle that case.
592 * For soft constraints (@), this function returns Cell.
594 * For some non-soft constraints (such as "Stringish"), this
595 * function may return a Type that is a strict supertype of the
596 * constraint's type.
598 * If something is known about the type of the object against which
599 * the constraint will be checked, it can be passed in to help
600 * disambiguate certain constraints (useful because we don't support
601 * arbitrary unions, or intersection).
603 Type lookup_constraint(Context, const TypeConstraint&,
604 const Type& t = TTop) const;
607 * If this function returns true, it is safe to assume that Type t
608 * will always satisfy TypeConstraint tc at run time.
610 bool satisfies_constraint(Context, const Type& t,
611 const TypeConstraint& tc) const;
614 * Returns true if the given type-hint (declared on the given class) might not
615 * be enforced at runtime (IE, it might map to mixed or be soft).
617 bool prop_tc_maybe_unenforced(const php::Class& propCls,
618 const TypeConstraint& tc) const;
621 * Returns true if the type constraint can contain a reified type
622 * Currently, only classes and interfaces are supported
624 bool could_have_reified_type(const TypeConstraint& tc) const;
627 * Lookup what the best known Type for a class constant would be,
628 * using a given Index and Context, if a class of that name were
629 * loaded.
630 * If allow_tconst is not set, type constants will not be returned.
631 * lookup_class_const_ptr version returns the statically known version
632 * of the const if it can find it, otherwise returns nullptr.
634 Type lookup_class_constant(Context, res::Class, SString cns,
635 bool allow_tconst) const;
636 const php::Const* lookup_class_const_ptr(Context, res::Class, SString cns,
637 bool allow_tconst) const;
640 * Lookup what the best known Type for a constant would be, using a
641 * given Index and Context, if a constant of that name were defined.
643 * Returns folly::none if the constant isn't in the index.
645 folly::Optional<Type> lookup_constant(Context ctx,
646 SString cnsName) const;
649 * See if the named constant has a unique scalar definition, and
650 * return its value if so.
652 folly::Optional<Cell> lookup_persistent_constant(SString cnsName) const;
655 * Return true if the return value of the function might depend on arg.
657 bool func_depends_on_arg(const php::Func* func, int arg) const;
660 * If func is effect-free when called with args, and it returns a constant,
661 * return that constant; otherwise return TTop.
663 Type lookup_foldable_return_type(Context ctx,
664 const php::Func* func,
665 Type ctxType,
666 CompactVector<Type> args) const;
668 * Return the best known return type for a resolved function, in a
669 * context insensitive way. Returns TInitGen at worst.
671 Type lookup_return_type(Context, res::Func) const;
674 * Return the best known return type for a resolved function, given
675 * the supplied calling context. Returns TInitGen at worst.
677 * During analyze phases, this function may re-enter analyze in
678 * order to interpret the callee with these argument types.
680 Type lookup_return_type(Context caller,
681 const CompactVector<Type>& args,
682 const Type& context,
683 res::Func) const;
686 * Look up the return type for an unresolved function. The
687 * interpreter should not use this routine---it's for stats or debug
688 * dumps.
690 * Nothing may be writing to the index when this function is used,
691 * but concurrent readers are allowed.
693 Type lookup_return_type_raw(const php::Func*) const;
696 * As lookup_return_type_raw, but also clean out the FuncInfo struct.
697 * For use during emit, to keep memory usage down.
699 Type lookup_return_type_and_clear(const php::Func*) const;
702 * Return the best known types of a closure's used variables (on
703 * entry to the closure). The function is the closure body.
705 * If move is true, the value will be moved out of the index. This
706 * should only be done at emit time.
708 CompactVector<Type>
709 lookup_closure_use_vars(const php::Func*,
710 bool move = false) const;
713 * Return the availability of $this on entry to the provided method.
714 * If the Func provided is not a method of a class false is
715 * returned.
717 bool lookup_this_available(const php::Func*) const;
720 * Returns the parameter preparation kind (if known) for parameter
721 * `paramId' on the given resolved Func.
723 PrepKind lookup_param_prep(Context, res::Func, uint32_t paramId) const;
726 * Returns the control-flow insensitive inferred private instance
727 * property types for a Class. The Class doesn't need to be
728 * resolved, because private properties don't depend on the
729 * inheritance hierarchy.
731 * The Index tracks the largest types for private properties that
732 * are guaranteed to hold at any program point.
734 * If move is true, the value will be moved out of the index. This
735 * should only be done at emit time.
737 PropState lookup_private_props(const php::Class*,
738 bool move = false) const;
741 * Returns the control-flow insensitive inferred private static
742 * property types for a Class. The class doesn't need to be
743 * resolved for the same reasons as for instance properties.
745 * The Index tracks the largest types for private static properties
746 * that are guaranteed to hold at any program point.
748 * If move is true, the value will be moved out of the index. This
749 * should only be done at emit time.
751 PropState lookup_private_statics(const php::Class*,
752 bool move = false) const;
755 * Lookup the best known type for a public static property, with a given
756 * class and name.
758 * This function will always return TInitGen before refine_public_statics has
759 * been called, or if the AnalyzePublicStatics option is off.
761 Type lookup_public_static(Context ctx, const Type& cls,
762 const Type& name) const;
763 Type lookup_public_static(Context ctx, const php::Class*,
764 SString name) const;
767 * Lookup if initializing (which is a side-effect of several bytecodes) the
768 * given class might raise.
770 bool lookup_class_init_might_raise(Context, res::Class) const;
773 * Lookup if a public static property with the given class and name might be
774 * AttrLateInit.
776 bool lookup_public_static_maybe_late_init(const Type& cls,
777 const Type& name) const;
780 * Returns whether a public static property is known to be immutable. This
781 * is used to add AttrPersistent flags to static properties, and relies on
782 * AnalyzePublicStatics (without this flag it will always return false).
784 bool lookup_public_static_immutable(const php::Class*,
785 SString name) const;
788 * Lookup the best known type for a public (non-static) property. Since we
789 * don't do analysis on public properties, this just inferred from the
790 * property's type-hint (if enforced).
792 Type lookup_public_prop(const Type& cls, const Type& name) const;
793 Type lookup_public_prop(const php::Class* cls, SString name) const;
796 * Returns the computed vtable slot for the given class, if it's an interface
797 * that was given a vtable slot. No two interfaces implemented by the same
798 * class will share the same vtable slot. May return kInvalidSlot, if the
799 * given class isn't an interface or if it wasn't assigned a slot.
801 Slot lookup_iface_vtable_slot(const php::Class*) const;
804 * Return the DependencyContext for ctx.
806 DependencyContext dependency_context(const Context& ctx) const;
809 * Determine whether to use class-at-a-time, or function-at-a-time
810 * dependencies.
812 * Must be called in single-threaded context.
814 void use_class_dependencies(bool f);
817 * Initialize the initial types for public static properties. This should be
818 * done after rewriting initial property values, as that affects the types.
820 void init_public_static_prop_types();
823 * Refine the types of the class constants defined by an 86cinit,
824 * based on a round of analysis.
826 * No other threads should be using ctx.cls->constants or deps when
827 * this function is called.
829 * Merges the set of Contexts that depended on the constants defined
830 * by this 86cinit.
832 void refine_class_constants(
833 const Context& ctx,
834 const CompactVector<std::pair<size_t, TypedValue>>& resolved,
835 DependencyContextSet& deps);
838 * Refine the types of the constants defined by a function, based on
839 * a round of analysis.
841 * Constants not defined by a pseudomain are considered unknowable
843 * No other threads should be calling functions on this Index when
844 * this function is called.
846 * Merges the set of Contexts that depended on the constants defined
847 * by this php::Func into deps.
849 void refine_constants(const FuncAnalysisResult& fa,
850 DependencyContextSet& deps);
853 * Refine the return type for a function, based on a round of
854 * analysis.
856 * No other threads should be calling functions on this Index when
857 * this function is called.
859 * Merges the set of Contexts that depended on the return type of
860 * this php::Func into deps.
862 void refine_return_info(const FuncAnalysisResult& fa,
863 DependencyContextSet& deps);
866 * Refine the used var types for a closure, based on a round of
867 * analysis.
869 * No other threads should be calling functions on this Index when
870 * this function is called.
872 * Returns: true if the types have changed.
874 bool refine_closure_use_vars(const php::Class*,
875 const CompactVector<Type>&);
878 * Refine the private property types for a class, based on a round
879 * of analysis.
881 * No other threads should be calling functions on this Index when
882 * this function is called.
884 void refine_private_props(const php::Class* cls,
885 const PropState&);
888 * Refine the static private property types for a class, based on a
889 * round of analysis.
891 * No other threads should be calling functions on this Index when
892 * this function is called.
894 void refine_private_statics(const php::Class* cls,
895 const PropState&);
898 * Record in the index that the given set of public static property mutations
899 * has been found while analyzing the given function. During a round of
900 * analysis, the mutations are gathered from the analysis results for each
901 * function, recorded in the index, and then refine_public_statics is called
902 * to process the mutations and update the index.
904 * No other threads should be calling functions on this Index when this
905 * function is called.
907 void record_public_static_mutations(const php::Func& func,
908 PublicSPropMutations mutations);
912 * If we resolve the intial value of a public property, we need to
913 * tell the refine_public_statics phase about it, because the init
914 * value won't be included in the mutations any more.
916 * Note that we can't modify the initial value here, because other
917 * threads might be reading it (via loookup_public_static), so we
918 * set a flag to tell us to update it during the next
919 * refine_public_statics pass.
921 void update_static_prop_init_val(const php::Class* cls,
922 SString name) const;
924 * After a round of analysis with all the public static property mutations
925 * being recorded with record_public_static_mutations, the types can be
926 * reflected into the index for use during another type inference pass.
928 * No other threads should be calling functions on this Index when this
929 * function is called.
931 * Merges the set of Contexts that depended on a public static property whose
932 * type has changed.
934 void refine_public_statics(DependencyContextSet& deps);
937 * Refine whether the given class has properties with initial values which
938 * might violate their type-hints.
940 * No other threads should be calling functions on this Index when this
941 * function is called.
943 void refine_bad_initial_prop_values(const php::Class* cls,
944 bool value,
945 DependencyContextSet& deps);
948 * Identify the persistent classes, functions and typeAliases.
950 void mark_persistent_classes_and_functions(php::Program& program);
953 * Mark any properties in cls that definitely do not redeclare a property in
954 * the parent, which has an inequivalent type-hint.
956 void mark_no_bad_redeclare_props(php::Class& cls) const;
959 * Rewrite the initial values of any AttrSystemInitialValue properties to
960 * something more suitable for its type-hint, and add AttrNoImplicitNullable
961 * where appropriate.
963 * This must be done before any analysis is done, as the initial values
964 * affects the analysis.
966 void rewrite_default_initial_values(php::Program&) const;
969 * Return true if the resolved function supports async eager return.
971 folly::Optional<bool> supports_async_eager_return(res::Func rfunc) const;
974 * Return true if the resolved function is effect free.
976 bool is_effect_free(res::Func rfunc) const;
979 * Return true if there are any interceptable functions
981 bool any_interceptable_functions() const;
984 * Do any necessary fixups to a return type.
986 * Note that eg for an async function it will map Type to
987 * WaitH<Type>.
989 void fixup_return_type(const php::Func*, Type&) const;
992 * Return true if we know for sure that one php::Class must derive
993 * from another at runtime, in all possible instantiations.
995 bool must_be_derived_from(const php::Class*,
996 const php::Class*) const;
998 struct IndexData;
999 private:
1000 Index(const Index&) = delete;
1001 Index& operator=(Index&&) = delete;
1003 private:
1004 friend struct PublicSPropMutations;
1006 template<class FuncRange>
1007 res::Func resolve_func_helper(const FuncRange&, SString) const;
1008 res::Func do_resolve(const php::Func*) const;
1009 bool could_be_related(const php::Class*,
1010 const php::Class*) const;
1012 template<bool getSuperType>
1013 Type get_type_for_constraint(Context,
1014 const TypeConstraint&,
1015 const Type&) const;
1017 struct ConstraintResolution;
1020 * Try to resolve name in the given context. Follows TypeAliases.
1022 ConstraintResolution resolve_class_or_type_alias(
1023 const Context& ctx, SString name, const Type& candidate) const;
1025 ConstraintResolution get_type_for_annotated_type(
1026 Context ctx, AnnotType annot, bool nullable,
1027 SString name, const Type& candidate) const;
1029 void init_return_type(const php::Func* func);
1031 ResolvedInfo<Either<SString,ClassInfo*>>
1032 resolve_type_name_internal(SString name) const;
1034 private:
1035 std::unique_ptr<IndexData> const m_data;
1038 //////////////////////////////////////////////////////////////////////
1041 * Used for collecting all mutations of public static property types.
1043 struct PublicSPropMutations {
1045 * This function must be called anywhere the interpreter does something that
1046 * could change the type of public static properties named `name' on classes
1047 * of type `cls' to `val'.
1049 * Note that if cls and name are both too generic this object will have to
1050 * give up all information it knows about any public static properties.
1052 void merge(const Index& index, Context ctx, const Type& cls,
1053 const Type& name, const Type& val);
1054 void merge(const Index& index, Context ctx, ClassInfo* cinfo,
1055 const Type& name, const Type& val);
1056 void merge(const Index& index, Context ctx, const php::Class& cls,
1057 const Type& name, const Type& val);
1059 private:
1060 friend struct Index;
1062 struct KnownKey {
1063 bool operator<(KnownKey o) const {
1064 if (cinfo != o.cinfo) return cinfo < o.cinfo;
1065 return prop < o.prop;
1068 ClassInfo* cinfo;
1069 SString prop;
1072 using UnknownMap = std::map<SString,Type>;
1073 using KnownMap = std::map<KnownKey,Type>;
1075 // Public static property mutations are actually rare, so defer allocating the
1076 // maps until we actually see one.
1077 struct Data {
1078 bool m_nothing_known{false};
1079 UnknownMap m_unknown;
1080 KnownMap m_known;
1082 std::unique_ptr<Data> m_data;
1085 //////////////////////////////////////////////////////////////////////
1089 #endif