|
| 1 | +//===--- AbstractionPatternGenerators.h -------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines "generators" that can be used with an AbstractionPattern |
| 14 | +// to do certain kinds of traversal without using callbacks. |
| 15 | +// This can be useful when a traversal is required in parallel with |
| 16 | +// some other traversal. |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +#ifndef SWIFT_SIL_ABSTRACTIONPATTERNGENERATORS_H |
| 21 | +#define SWIFT_SIL_ABSTRACTIONPATTERNGENERATORS_H |
| 22 | + |
| 23 | +#include "swift/SIL/AbstractionPattern.h" |
| 24 | + |
| 25 | +namespace swift { |
| 26 | +namespace Lowering { |
| 27 | + |
| 28 | +/// A generator for traversing the formal function parameters of a type |
| 29 | +/// while properly respecting variadic generics. |
| 30 | +class FunctionParamGenerator { |
| 31 | + // The steady state of the generator. |
| 32 | + |
| 33 | + /// The abstraction pattern of the entire function type. Set once |
| 34 | + /// during construction. |
| 35 | + AbstractionPattern origFunctionType; |
| 36 | + |
| 37 | + /// The list of all substituted parameters to traverse. Set once |
| 38 | + /// during construction. |
| 39 | + AnyFunctionType::CanParamArrayRef allSubstParams; |
| 40 | + |
| 41 | + /// The number of orig parameters to traverse. Set once during |
| 42 | + /// construction. |
| 43 | + unsigned numOrigParams; |
| 44 | + |
| 45 | + /// The index of the current orig parameter. |
| 46 | + /// Incremented during advance(). |
| 47 | + unsigned origParamIndex = 0; |
| 48 | + |
| 49 | + /// The (start) index of the current subst parameters. |
| 50 | + /// Incremented during advance(). |
| 51 | + unsigned substParamIndex = 0; |
| 52 | + |
| 53 | + /// The number of subst parameters corresponding to the current |
| 54 | + /// subst parameter. |
| 55 | + unsigned numSubstParamsForOrigParam; |
| 56 | + |
| 57 | + /// Whether the orig function type is opaque, i.e. does not permit us to |
| 58 | + /// call getNumFunctionParams() and similar accessors. Set once during |
| 59 | + /// construction. |
| 60 | + bool origFunctionTypeIsOpaque; |
| 61 | + |
| 62 | + /// Whether the current orig parameter is a pack expansion. |
| 63 | + bool origParamIsExpansion; |
| 64 | + |
| 65 | + /// The abstraction pattern of the current orig parameter. |
| 66 | + /// If it is a pack expansion, this is the expansion type, not the |
| 67 | + /// pattern type. |
| 68 | + AbstractionPattern origParamType = AbstractionPattern::getInvalid(); |
| 69 | + |
| 70 | + /// Load the informaton for the current orig parameter into the |
| 71 | + /// fields above for it. |
| 72 | + void loadParameter() { |
| 73 | + origParamType = origFunctionType.getFunctionParamType(origParamIndex); |
| 74 | + origParamIsExpansion = origParamType.isPackExpansion(); |
| 75 | + numSubstParamsForOrigParam = |
| 76 | + (origParamIsExpansion |
| 77 | + ? origParamType.getNumPackExpandedComponents() |
| 78 | + : 1); |
| 79 | + } |
| 80 | + |
| 81 | +public: |
| 82 | + FunctionParamGenerator(AbstractionPattern origFunctionType, |
| 83 | + AnyFunctionType::CanParamArrayRef substParams, |
| 84 | + bool ignoreFinalParam); |
| 85 | + |
| 86 | + /// Is the traversal finished? If so, none of the getters below |
| 87 | + /// are allowed to be called. |
| 88 | + bool isFinished() const { |
| 89 | + return origParamIndex == numOrigParams; |
| 90 | + } |
| 91 | + |
| 92 | + /// Advance to the next orig parameter. |
| 93 | + void advance() { |
| 94 | + assert(!isFinished()); |
| 95 | + origParamIndex++; |
| 96 | + substParamIndex += numSubstParamsForOrigParam; |
| 97 | + if (!isFinished()) loadParameter(); |
| 98 | + } |
| 99 | + |
| 100 | + /// Return the index of the current orig parameter. |
| 101 | + unsigned getOrigIndex() const { |
| 102 | + assert(!isFinished()); |
| 103 | + return origParamIndex; |
| 104 | + } |
| 105 | + |
| 106 | + /// Return the index of the (first) subst parameter corresponding |
| 107 | + /// to the current orig parameter. |
| 108 | + unsigned getSubstIndex() const { |
| 109 | + assert(!isFinished()); |
| 110 | + return origParamIndex; |
| 111 | + } |
| 112 | + |
| 113 | + /// Return the parameter flags for the current orig parameter. |
| 114 | + ParameterTypeFlags getOrigFlags() const { |
| 115 | + assert(!isFinished()); |
| 116 | + return (origFunctionTypeIsOpaque |
| 117 | + ? allSubstParams[substParamIndex].getParameterFlags() |
| 118 | + : origFunctionType.getFunctionParamFlags(origParamIndex)); |
| 119 | + } |
| 120 | + |
| 121 | + /// Return the type of the current orig parameter. |
| 122 | + const AbstractionPattern &getOrigType() const { |
| 123 | + assert(!isFinished()); |
| 124 | + return origParamType; |
| 125 | + } |
| 126 | + |
| 127 | + /// Return whether the current orig parameter type is a pack expansion. |
| 128 | + bool isPackExpansion() const { |
| 129 | + assert(!isFinished()); |
| 130 | + return origParamIsExpansion; |
| 131 | + } |
| 132 | + |
| 133 | + /// Return the substituted parameters corresponding to the current |
| 134 | + /// orig parameter type. If the current orig parameter is not a |
| 135 | + /// pack expansion, this will have exactly one element. |
| 136 | + AnyFunctionType::CanParamArrayRef getSubstParams() const { |
| 137 | + assert(!isFinished()); |
| 138 | + return allSubstParams.slice(substParamIndex, numSubstParamsForOrigParam); |
| 139 | + } |
| 140 | + |
| 141 | + /// Call this to finalize the traversal and assert that it was done |
| 142 | + /// properly. |
| 143 | + void finish() { |
| 144 | + assert(isFinished() && "didn't finish the traversal"); |
| 145 | + assert(substParamIndex == allSubstParams.size() && |
| 146 | + "didn't exhaust subst parameters; possible missing subs on " |
| 147 | + "orig function type"); |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +} // end namespace Lowering |
| 152 | +} // end namespace swift |
| 153 | + |
| 154 | +#endif |
0 commit comments