Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 1 | ///===-- Representation.cpp - ClangDoc Representation -----------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the merging of different types of infos. The data in the |
| 10 | // calling Info is preserved during a merge unless that field is empty or |
| 11 | // default. In that case, the data from the parameter Info is used to replace |
| 12 | // the empty or default data. |
| 13 | // |
| 14 | // For most fields, the first decl seen provides the data. Exceptions to this |
| 15 | // include the location and description fields, which are collections of data on |
| 16 | // all decls related to a given definition. All other fields are ignored in new |
| 17 | // decls unless the first seen decl didn't, for whatever reason, incorporate |
| 18 | // data on that field (e.g. a forward declared class wouldn't have information |
| 19 | // on members on the forward declaration, but would have the class name). |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | #include "Representation.h" |
| 23 | #include "llvm/Support/Error.h" |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace doc { |
| 27 | |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | const SymbolID EmptySID = SymbolID(); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 31 | |
| 32 | template <typename T> |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 33 | llvm::Expected<std::unique_ptr<Info>> |
| 34 | reduce(std::vector<std::unique_ptr<Info>> &Values) { |
| 35 | if (Values.empty()) |
| 36 | return llvm::make_error<llvm::StringError>(" No values to reduce.\n", |
| 37 | llvm::inconvertibleErrorCode()); |
| 38 | std::unique_ptr<Info> Merged = llvm::make_unique<T>(Values[0]->USR); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 39 | T *Tmp = static_cast<T *>(Merged.get()); |
| 40 | for (auto &I : Values) |
| 41 | Tmp->merge(std::move(*static_cast<T *>(I.get()))); |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 42 | return std::move(Merged); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 45 | // Return the index of the matching child in the vector, or -1 if merge is not |
| 46 | // necessary. |
| 47 | template <typename T> |
| 48 | int getChildIndexIfExists(std::vector<T> &Children, T &ChildToMerge) { |
| 49 | for (unsigned long I = 0; I < Children.size(); I++) { |
| 50 | if (ChildToMerge.USR == Children[I].USR) |
| 51 | return I; |
| 52 | } |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | // For References, we don't need to actually merge them, we just don't want |
| 57 | // duplicates. |
| 58 | void reduceChildren(std::vector<Reference> &Children, |
| 59 | std::vector<Reference> &&ChildrenToMerge) { |
| 60 | for (auto &ChildToMerge : ChildrenToMerge) { |
| 61 | if (getChildIndexIfExists(Children, ChildToMerge) == -1) |
| 62 | Children.push_back(std::move(ChildToMerge)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void reduceChildren(std::vector<FunctionInfo> &Children, |
| 67 | std::vector<FunctionInfo> &&ChildrenToMerge) { |
| 68 | for (auto &ChildToMerge : ChildrenToMerge) { |
| 69 | int mergeIdx = getChildIndexIfExists(Children, ChildToMerge); |
| 70 | if (mergeIdx == -1) { |
| 71 | Children.push_back(std::move(ChildToMerge)); |
| 72 | continue; |
| 73 | } |
| 74 | Children[mergeIdx].merge(std::move(ChildToMerge)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void reduceChildren(std::vector<EnumInfo> &Children, |
| 79 | std::vector<EnumInfo> &&ChildrenToMerge) { |
| 80 | for (auto &ChildToMerge : ChildrenToMerge) { |
| 81 | int mergeIdx = getChildIndexIfExists(Children, ChildToMerge); |
| 82 | if (mergeIdx == -1) { |
| 83 | Children.push_back(std::move(ChildToMerge)); |
| 84 | continue; |
| 85 | } |
| 86 | Children[mergeIdx].merge(std::move(ChildToMerge)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 92 | // Dispatch function. |
| 93 | llvm::Expected<std::unique_ptr<Info>> |
| 94 | mergeInfos(std::vector<std::unique_ptr<Info>> &Values) { |
| 95 | if (Values.empty()) |
| 96 | return llvm::make_error<llvm::StringError>("No info values to merge.\n", |
Julie Hockett | eb50a2e | 2018-07-20 18:49:55 +0000 | [diff] [blame] | 97 | llvm::inconvertibleErrorCode()); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 98 | |
| 99 | switch (Values[0]->IT) { |
| 100 | case InfoType::IT_namespace: |
| 101 | return reduce<NamespaceInfo>(Values); |
| 102 | case InfoType::IT_record: |
| 103 | return reduce<RecordInfo>(Values); |
| 104 | case InfoType::IT_enum: |
| 105 | return reduce<EnumInfo>(Values); |
| 106 | case InfoType::IT_function: |
| 107 | return reduce<FunctionInfo>(Values); |
| 108 | default: |
| 109 | return llvm::make_error<llvm::StringError>("Unexpected info type.\n", |
| 110 | llvm::inconvertibleErrorCode()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void Info::mergeBase(Info &&Other) { |
| 115 | assert(mergeable(Other)); |
| 116 | if (USR == EmptySID) |
| 117 | USR = Other.USR; |
| 118 | if (Name == "") |
| 119 | Name = Other.Name; |
| 120 | if (Namespace.empty()) |
| 121 | Namespace = std::move(Other.Namespace); |
| 122 | // Unconditionally extend the description, since each decl may have a comment. |
| 123 | std::move(Other.Description.begin(), Other.Description.end(), |
| 124 | std::back_inserter(Description)); |
| 125 | } |
| 126 | |
| 127 | bool Info::mergeable(const Info &Other) { |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 128 | return IT == Other.IT && USR == Other.USR; |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void SymbolInfo::merge(SymbolInfo &&Other) { |
| 132 | assert(mergeable(Other)); |
| 133 | if (!DefLoc) |
| 134 | DefLoc = std::move(Other.DefLoc); |
| 135 | // Unconditionally extend the list of locations, since we want all of them. |
| 136 | std::move(Other.Loc.begin(), Other.Loc.end(), std::back_inserter(Loc)); |
| 137 | mergeBase(std::move(Other)); |
| 138 | } |
| 139 | |
| 140 | void NamespaceInfo::merge(NamespaceInfo &&Other) { |
| 141 | assert(mergeable(Other)); |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 142 | // Reduce children if necessary. |
| 143 | reduceChildren(ChildNamespaces, std::move(Other.ChildNamespaces)); |
| 144 | reduceChildren(ChildRecords, std::move(Other.ChildRecords)); |
| 145 | reduceChildren(ChildFunctions, std::move(Other.ChildFunctions)); |
| 146 | reduceChildren(ChildEnums, std::move(Other.ChildEnums)); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 147 | mergeBase(std::move(Other)); |
| 148 | } |
| 149 | |
| 150 | void RecordInfo::merge(RecordInfo &&Other) { |
| 151 | assert(mergeable(Other)); |
| 152 | if (!TagType) |
| 153 | TagType = Other.TagType; |
| 154 | if (Members.empty()) |
| 155 | Members = std::move(Other.Members); |
| 156 | if (Parents.empty()) |
| 157 | Parents = std::move(Other.Parents); |
| 158 | if (VirtualParents.empty()) |
| 159 | VirtualParents = std::move(Other.VirtualParents); |
Julie Hockett | 8899c29 | 2018-08-02 20:10:17 +0000 | [diff] [blame] | 160 | // Reduce children if necessary. |
| 161 | reduceChildren(ChildRecords, std::move(Other.ChildRecords)); |
| 162 | reduceChildren(ChildFunctions, std::move(Other.ChildFunctions)); |
| 163 | reduceChildren(ChildEnums, std::move(Other.ChildEnums)); |
Julie Hockett | d0f9a87 | 2018-06-04 17:22:20 +0000 | [diff] [blame] | 164 | SymbolInfo::merge(std::move(Other)); |
| 165 | } |
| 166 | |
| 167 | void EnumInfo::merge(EnumInfo &&Other) { |
| 168 | assert(mergeable(Other)); |
| 169 | if (!Scoped) |
| 170 | Scoped = Other.Scoped; |
| 171 | if (Members.empty()) |
| 172 | Members = std::move(Other.Members); |
| 173 | SymbolInfo::merge(std::move(Other)); |
| 174 | } |
| 175 | |
| 176 | void FunctionInfo::merge(FunctionInfo &&Other) { |
| 177 | assert(mergeable(Other)); |
| 178 | if (!IsMethod) |
| 179 | IsMethod = Other.IsMethod; |
| 180 | if (!Access) |
| 181 | Access = Other.Access; |
| 182 | if (ReturnType.Type.USR == EmptySID && ReturnType.Type.Name == "") |
| 183 | ReturnType = std::move(Other.ReturnType); |
| 184 | if (Parent.USR == EmptySID && Parent.Name == "") |
| 185 | Parent = std::move(Other.Parent); |
| 186 | if (Params.empty()) |
| 187 | Params = std::move(Other.Params); |
| 188 | SymbolInfo::merge(std::move(Other)); |
| 189 | } |
| 190 | |
| 191 | } // namespace doc |
| 192 | } // namespace clang |