blob: cc07a6e1925bc9326c513a78cc3195bbaa2e32c9 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "link/Linkers.h"
18
19#include <algorithm>
20#include <iterator>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "android-base/logging.h"
23
24#include "ResourceTable.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026namespace aapt {
27
28template <typename InputContainer, typename OutputIterator, typename Predicate>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029OutputIterator move_if(InputContainer& input_container, OutputIterator result,
30 Predicate pred) {
31 const auto last = input_container.end();
32 auto new_end =
33 std::find_if(input_container.begin(), input_container.end(), pred);
34 if (new_end == last) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035 return result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 *result = std::move(*new_end);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 auto first = new_end;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 ++first;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 for (; first != last; ++first) {
44 if (bool(pred(*first))) {
45 // We want to move this guy
46 *result = std::move(*first);
47 ++result;
48 } else {
49 // We want to keep this guy, but we will need to move it up the list to
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 // replace missing items.
51 *new_end = std::move(*first);
52 ++new_end;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 }
55
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 input_container.erase(new_end, last);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return result;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058}
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060bool PrivateAttributeMover::Consume(IAaptContext* context,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 ResourceTable* table) {
62 for (auto& package : table->packages) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 ResourceTableType* type = package->FindType(ResourceType::kAttr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 if (!type) {
65 continue;
66 }
67
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 if (type->symbol_status.state != SymbolState::kPublic) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 // No public attributes, so we can safely leave these private attributes
70 // where they are.
71 return true;
72 }
73
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 ResourceTableType* priv_attr_type =
75 package->FindOrCreateType(ResourceType::kAttrPrivate);
76 CHECK(priv_attr_type->entries.empty());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 move_if(type->entries, std::back_inserter(priv_attr_type->entries),
79 [](const std::unique_ptr<ResourceEntry>& entry) -> bool {
80 return entry->symbol_status.state != SymbolState::kPublic;
81 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 break;
83 }
84 return true;
85}
86
87} // namespace aapt