|
| 1 | +// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#include "Luau/Type.h" |
| 5 | +#include "Luau/TypePack.h" |
| 6 | +#include "Luau/Set.h" |
| 7 | + |
| 8 | +#include <string> |
| 9 | + |
| 10 | +namespace Luau |
| 11 | +{ |
| 12 | + |
| 13 | +struct IterativeTypeVisitor |
| 14 | +{ |
| 15 | + using SeenSet = Set<const void*>; |
| 16 | + |
| 17 | + // We avoid Luau::Variant here because we can move the tag bit and make this struct 64 bits shorter. |
| 18 | + struct WorkItem |
| 19 | + { |
| 20 | + WorkItem(TypeId ty, int32_t parent); |
| 21 | + WorkItem(TypePackId tp, int32_t parent); |
| 22 | + |
| 23 | + const TypeId* asType() const; |
| 24 | + const TypePackId* asTypePack() const; |
| 25 | + |
| 26 | + bool operator==(TypeId ty) const; |
| 27 | + bool operator==(TypePackId tp) const; |
| 28 | + |
| 29 | + private: |
| 30 | + // TypeId if isType, else TypePackId |
| 31 | + const void* t; |
| 32 | + bool isType; |
| 33 | + |
| 34 | + public: |
| 35 | + // -1 indicates no parent |
| 36 | + int32_t parent; |
| 37 | + }; |
| 38 | + |
| 39 | + explicit IterativeTypeVisitor(std::string visitorName, bool skipBoundTypes); |
| 40 | + explicit IterativeTypeVisitor(std::string visitorName, bool visitOnce, bool skipBoundTypes); |
| 41 | + explicit IterativeTypeVisitor(std::string visitorName, SeenSet seen, bool visitOnce, bool skipBoundTypes); |
| 42 | + |
| 43 | + IterativeTypeVisitor(const IterativeTypeVisitor&) = delete; |
| 44 | + IterativeTypeVisitor& operator=(const IterativeTypeVisitor&) = delete; |
| 45 | + |
| 46 | + virtual ~IterativeTypeVisitor() = default; |
| 47 | + |
| 48 | + virtual void cycle(TypeId); |
| 49 | + virtual void cycle(TypePackId); |
| 50 | + |
| 51 | + virtual bool visit(TypeId ty); |
| 52 | + virtual bool visit(TypeId ty, const BoundType& btv); |
| 53 | + virtual bool visit(TypeId ty, const FreeType& ftv); |
| 54 | + virtual bool visit(TypeId ty, const GenericType& gtv); |
| 55 | + virtual bool visit(TypeId ty, const ErrorType& etv); |
| 56 | + virtual bool visit(TypeId ty, const PrimitiveType& ptv); |
| 57 | + virtual bool visit(TypeId ty, const FunctionType& ftv); |
| 58 | + virtual bool visit(TypeId ty, const TableType& ttv); |
| 59 | + virtual bool visit(TypeId ty, const MetatableType& mtv); |
| 60 | + virtual bool visit(TypeId ty, const ExternType& etv); |
| 61 | + virtual bool visit(TypeId ty, const AnyType& atv); |
| 62 | + virtual bool visit(TypeId ty, const NoRefineType& nrt); |
| 63 | + virtual bool visit(TypeId ty, const UnknownType& utv); |
| 64 | + virtual bool visit(TypeId ty, const NeverType& ntv); |
| 65 | + virtual bool visit(TypeId ty, const UnionType& utv); |
| 66 | + virtual bool visit(TypeId ty, const IntersectionType& itv); |
| 67 | + virtual bool visit(TypeId ty, const BlockedType& btv); |
| 68 | + virtual bool visit(TypeId ty, const PendingExpansionType& petv); |
| 69 | + virtual bool visit(TypeId ty, const SingletonType& stv); |
| 70 | + virtual bool visit(TypeId ty, const NegationType& ntv); |
| 71 | + virtual bool visit(TypeId ty, const TypeFunctionInstanceType& tfit); |
| 72 | + |
| 73 | + virtual bool visit(TypePackId tp); |
| 74 | + virtual bool visit(TypePackId tp, const BoundTypePack& btp); |
| 75 | + virtual bool visit(TypePackId tp, const FreeTypePack& ftp); |
| 76 | + virtual bool visit(TypePackId tp, const GenericTypePack& gtp); |
| 77 | + virtual bool visit(TypePackId tp, const ErrorTypePack& etp); |
| 78 | + virtual bool visit(TypePackId tp, const TypePack& pack); |
| 79 | + virtual bool visit(TypePackId tp, const VariadicTypePack& vtp); |
| 80 | + virtual bool visit(TypePackId tp, const BlockedTypePack& btp); |
| 81 | + virtual bool visit(TypePackId tp, const TypeFunctionInstanceTypePack& tfitp); |
| 82 | + |
| 83 | + void run(TypeId ty); |
| 84 | + void run(TypePackId tp); |
| 85 | + |
| 86 | +protected: |
| 87 | + /// Add this type (or pack) to the queue of things to traverse. Does not |
| 88 | + /// immediately process the thing! You cannot use this to effect in-order |
| 89 | + /// traversal. |
| 90 | + void traverse(TypeId ty); |
| 91 | + void traverse(TypePackId tp); |
| 92 | + |
| 93 | +private: |
| 94 | + void process(TypeId ty); |
| 95 | + void process(TypePackId tp); |
| 96 | + |
| 97 | + bool hasSeen(const void* tv); |
| 98 | + void unsee(const void* tv); |
| 99 | + |
| 100 | + template<typename TID> |
| 101 | + bool isCyclic(TID ty) const; |
| 102 | + |
| 103 | + void processWorkQueue(); |
| 104 | + |
| 105 | + SeenSet seen{nullptr}; |
| 106 | + |
| 107 | + std::vector<WorkItem> workQueue; |
| 108 | + int32_t parentCursor = -1; |
| 109 | + uint32_t workCursor = 0; |
| 110 | + |
| 111 | + const std::string visitorName; |
| 112 | + bool skipBoundTypes = false; |
| 113 | + bool visitOnce = true; |
| 114 | +}; |
| 115 | + |
| 116 | +} // namespace Luau |
0 commit comments