|
| 1 | +//===--- PotentialMacroExpansions.h -----------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 a structure (\c PotentialMacroExpansions) to track |
| 14 | +// potential macro expansions within a given context. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | +#ifndef SWIFT_AST_POTENTIAL_MACRO_EXPANSIONS_H |
| 18 | +#define SWIFT_AST_POTENTIAL_MACRO_EXPANSIONS_H |
| 19 | + |
| 20 | +#include "swift/AST/Identifier.h" |
| 21 | +#include "llvm/ADT/PointerIntPair.h" |
| 22 | +#include "llvm/ADT/SmallPtrSet.h" |
| 23 | + |
| 24 | +namespace swift { |
| 25 | + |
| 26 | +/// Describes the potential macro expansions within a given type or |
| 27 | +/// extension context. |
| 28 | +class PotentialMacroExpansions { |
| 29 | + enum { |
| 30 | + /// Whether there are any expanded macros. |
| 31 | + AnyExpandedMacros = 0x01, |
| 32 | + |
| 33 | + /// Whether any of the expanded macros introduces arbitrary names. |
| 34 | + IntroducesArbitraryNames = 0x02, |
| 35 | + }; |
| 36 | + |
| 37 | + using NameSet = llvm::SmallPtrSet<DeclName, 4>; |
| 38 | + |
| 39 | + /// Storage for the set of potential macro expansions. |
| 40 | + llvm::PointerIntPair<NameSet *, 2, unsigned> Storage; |
| 41 | + |
| 42 | + /// Retrieve a pointer to the name set if there is one. |
| 43 | + const NameSet *getIntroducedNamesIfAvailable() const { |
| 44 | + return Storage.getPointer(); |
| 45 | + } |
| 46 | + |
| 47 | + /// Get or create a nam |
| 48 | + NameSet &getOrCreateIntroducedNames() { |
| 49 | + if (auto nameSet = Storage.getPointer()) |
| 50 | + return *nameSet; |
| 51 | + |
| 52 | + // Allocate a new set of introduced names. |
| 53 | + auto nameSet = new NameSet(); |
| 54 | + Storage.setPointer(nameSet); |
| 55 | + return *nameSet; |
| 56 | + } |
| 57 | + |
| 58 | +public: |
| 59 | + PotentialMacroExpansions() : Storage() { } |
| 60 | + |
| 61 | + PotentialMacroExpansions(const PotentialMacroExpansions &other) |
| 62 | + : Storage(nullptr, other.Storage.getInt()) |
| 63 | + { |
| 64 | + if (auto otherNameSet = other.getIntroducedNamesIfAvailable()) { |
| 65 | + Storage.setPointer(new NameSet(*otherNameSet)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + PotentialMacroExpansions(PotentialMacroExpansions &&other) |
| 70 | + : Storage(other.Storage) |
| 71 | + { |
| 72 | + other.Storage.setPointer(nullptr); |
| 73 | + } |
| 74 | + |
| 75 | + PotentialMacroExpansions &operator=(const PotentialMacroExpansions &other) { |
| 76 | + PotentialMacroExpansions tmp(other); |
| 77 | + swap(tmp, *this); |
| 78 | + return *this; |
| 79 | + } |
| 80 | + |
| 81 | + PotentialMacroExpansions &operator=(PotentialMacroExpansions &&other) { |
| 82 | + if (&other != this) { |
| 83 | + Storage = other.Storage; |
| 84 | + other.Storage.setPointer(nullptr); |
| 85 | + } |
| 86 | + return *this; |
| 87 | + } |
| 88 | + |
| 89 | + ~PotentialMacroExpansions() { |
| 90 | + delete getIntroducedNamesIfAvailable(); |
| 91 | + } |
| 92 | + |
| 93 | + /// Whether there are any expanded macros in this context. |
| 94 | + explicit operator bool() const { return hasAnyExpandedMacro(); } |
| 95 | + |
| 96 | + /// Whether there are any expanded macros in this context. |
| 97 | + bool hasAnyExpandedMacro() const { |
| 98 | + return Storage.getInt() & AnyExpandedMacros; |
| 99 | + } |
| 100 | + |
| 101 | + /// Note that we have expanded a macro. |
| 102 | + void noteExpandedMacro() { |
| 103 | + Storage.setInt(Storage.getInt() | AnyExpandedMacros); |
| 104 | + } |
| 105 | + |
| 106 | + /// Whether any expanded macro introduces arbitrary names. |
| 107 | + bool introducesArbitraryNames() const { |
| 108 | + return Storage.getInt() & IntroducesArbitraryNames; |
| 109 | + } |
| 110 | + |
| 111 | + /// Note that a macro expanded here introduced arbitrary names. |
| 112 | + void noteIntroducesArbitraryNames() { |
| 113 | + Storage.setInt(Storage.getInt() | IntroducesArbitraryNames); |
| 114 | + } |
| 115 | + |
| 116 | + /// Add a new introduced macro name. |
| 117 | + void addIntroducedMacroName(DeclName name) { |
| 118 | + getOrCreateIntroducedNames().insert(name.getBaseName()); |
| 119 | + } |
| 120 | + |
| 121 | + /// Determine whether one should expand any macros in this context because |
| 122 | + /// they could introduce a declaration with the given name. |
| 123 | + bool shouldExpandForName(DeclName name) const { |
| 124 | + // If any macro produces arbitraty names, we need to expand it. |
| 125 | + if (introducesArbitraryNames()) |
| 126 | + return true; |
| 127 | + |
| 128 | + auto introducedNames = getIntroducedNamesIfAvailable(); |
| 129 | + if (!introducedNames) |
| 130 | + return false; |
| 131 | + |
| 132 | + return introducedNames->count(name.getBaseName()); |
| 133 | + } |
| 134 | + |
| 135 | + friend bool operator==(const PotentialMacroExpansions &lhs, |
| 136 | + const PotentialMacroExpansions &rhs) { |
| 137 | + // Check has-any-expanded-macro and introduces-arbitrary-names together. |
| 138 | + if (lhs.Storage.getInt() != rhs.Storage.getInt()) |
| 139 | + return false; |
| 140 | + |
| 141 | + // If they introduced arbitrary names, ignore the name sets... they are |
| 142 | + // the same. |
| 143 | + if (lhs.introducesArbitraryNames()) |
| 144 | + return true; |
| 145 | + |
| 146 | + // Both expanded macros and did not introduce arbitrary names, so we need |
| 147 | + // to check the actual names. |
| 148 | + auto lhsIntroducedNames = lhs.getIntroducedNamesIfAvailable(); |
| 149 | + auto rhsIntroducedNames = rhs.getIntroducedNamesIfAvailable(); |
| 150 | + |
| 151 | + auto lhsIntroducedNamesCount = |
| 152 | + lhsIntroducedNames ? lhsIntroducedNames->size() : 0; |
| 153 | + auto rhsIntroducedNamesCount = |
| 154 | + rhsIntroducedNames ? rhsIntroducedNames->size() : 0; |
| 155 | + if (lhsIntroducedNamesCount != rhsIntroducedNamesCount) |
| 156 | + return false; |
| 157 | + |
| 158 | + // Check whether both are empty. |
| 159 | + if (lhsIntroducedNamesCount == 0) |
| 160 | + return true; |
| 161 | + |
| 162 | + // Make sure all of the names of one are in the other. |
| 163 | + for (auto lhsName : *lhsIntroducedNames) { |
| 164 | + if (rhsIntroducedNames->count(lhsName) == 0) |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + return true; |
| 169 | + } |
| 170 | + |
| 171 | + friend bool operator!=(const PotentialMacroExpansions &lhs, |
| 172 | + const PotentialMacroExpansions &rhs) { |
| 173 | + return !(lhs == rhs); |
| 174 | + } |
| 175 | + |
| 176 | + friend void swap( |
| 177 | + PotentialMacroExpansions &lhs, PotentialMacroExpansions &rhs) { |
| 178 | + auto tmpStorage = lhs.Storage; |
| 179 | + lhs.Storage = rhs.Storage; |
| 180 | + rhs.Storage = tmpStorage; |
| 181 | + } |
| 182 | +}; |
| 183 | + |
| 184 | +} |
| 185 | + |
| 186 | +#endif // SWIFT_AST_POTENTIAL_MACRO_EXPANSIONS_H |
0 commit comments