1467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski/*
2467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * Copyright (C) 2015 The Android Open Source Project
3467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski *
4467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
5467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * you may not use this file except in compliance with the License.
6467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * You may obtain a copy of the License at
7467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski *
8467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
9467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski *
10467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * Unless required by applicable law or agreed to in writing, software
11467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
12467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * See the License for the specific language governing permissions and
14467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * limitations under the License.
15467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski */
16467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
17467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#ifndef AAPT_LINKER_REFERENCELINKER_H
18467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#define AAPT_LINKER_REFERENCELINKER_H
19467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
20467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "Resource.h"
21467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "ResourceValues.h"
22467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "ValueVisitor.h"
23467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "link/Linkers.h"
24467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "process/IResourceTableConsumer.h"
25467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "process/SymbolTable.h"
26467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "xml/XmlDom.h"
27467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
28467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include <cassert>
29467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
30467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinskinamespace aapt {
31467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
32467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski/**
33467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * Resolves all references to resources in the ResourceTable and assigns them IDs.
34467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * The ResourceTable must already have IDs assigned to each resource.
35467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * Once the ResourceTable is processed by this linker, it is ready to be flattened.
36467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski */
37467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinskistruct ReferenceLinker : public IResourceTableConsumer {
38467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    /**
39467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Returns true if the symbol is visible by the reference and from the callsite.
40467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     */
4164587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    static bool isSymbolVisible(const SymbolTable::Symbol& symbol, const Reference& ref,
42467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                                const CallSite& callSite);
43467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
44467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    /**
45467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Performs name mangling and looks up the resource in the symbol table. Returns nullptr
46467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * if the symbol was not found.
47467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     */
4864587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    static const SymbolTable::Symbol* resolveSymbol(const Reference& reference,
4964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                    NameMangler* mangler, SymbolTable* symbols);
50467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
51467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    /**
52467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Performs name mangling and looks up the resource in the symbol table. If the symbol is
53467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * not visible by the reference at the callsite, nullptr is returned. outError holds
54467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * the error message.
55467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     */
5664587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    static const SymbolTable::Symbol* resolveSymbolCheckVisibility(const Reference& reference,
5764587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                   NameMangler* nameMangler,
5864587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                   SymbolTable* symbols,
5964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                   CallSite* callSite,
6064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                   std::string* outError);
61467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
62467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    /**
63467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Same as resolveSymbolCheckVisibility(), but also makes sure the symbol is an attribute.
64467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * That is, the return value will have a non-null value for ISymbolTable::Symbol::attribute.
65467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     */
6664587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    static const SymbolTable::Symbol* resolveAttributeCheckVisibility(const Reference& reference,
6764587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                      NameMangler* nameMangler,
6864587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                      SymbolTable* symbols,
6964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                      CallSite* callSite,
7064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                                      std::string* outError);
71467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
72467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    /**
73467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Resolves the attribute reference and returns an xml::AaptAttribute if successful.
74467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * If resolution fails, outError holds the error message.
75467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     */
76467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    static Maybe<xml::AaptAttribute> compileXmlAttribute(const Reference& reference,
77467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                                                         NameMangler* nameMangler,
7864587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski                                                         SymbolTable* symbols,
79467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                                                         CallSite* callSite,
80467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                                                         std::string* outError);
81467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
82467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    /**
8328cacf091ad2b1c2749e77f590e9523e58735252Adam Lesinski     * Writes the resource name to the DiagMessage, using the "orig_name (aka <transformed_name>)"
8428cacf091ad2b1c2749e77f590e9523e58735252Adam Lesinski     * syntax.
8528cacf091ad2b1c2749e77f590e9523e58735252Adam Lesinski     */
8628cacf091ad2b1c2749e77f590e9523e58735252Adam Lesinski    static void writeResourceName(DiagMessage* outMsg, const Reference& orig,
8728cacf091ad2b1c2749e77f590e9523e58735252Adam Lesinski                                  const Reference& transformed);
8828cacf091ad2b1c2749e77f590e9523e58735252Adam Lesinski
8928cacf091ad2b1c2749e77f590e9523e58735252Adam Lesinski    /**
90467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Transforms the package name of the reference to the fully qualified package name using
91467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * the xml::IPackageDeclStack, then mangles and looks up the symbol. If the symbol is visible
92467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * to the reference at the callsite, the reference is updated with an ID.
93467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Returns false on failure, and an error message is logged to the IDiagnostics in the context.
94467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     */
9564587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski    static bool linkReference(Reference* reference, IAaptContext* context, SymbolTable* symbols,
96467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski                              xml::IPackageDeclStack* decls, CallSite* callSite);
97467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
98467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    /**
99467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     * Links all references in the ResourceTable.
100467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski     */
101467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski    bool consume(IAaptContext* context, ResourceTable* table) override;
102467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski};
103467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
104467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski} // namespace aapt
105467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski
106467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#endif /* AAPT_LINKER_REFERENCELINKER_H */
107