Home | History | Annotate | Download | only in link
      1 /*
      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 
     17 #ifndef AAPT_LINKER_REFERENCELINKER_H
     18 #define AAPT_LINKER_REFERENCELINKER_H
     19 
     20 #include "android-base/macros.h"
     21 
     22 #include "Resource.h"
     23 #include "ResourceValues.h"
     24 #include "link/Linkers.h"
     25 #include "process/IResourceTableConsumer.h"
     26 #include "process/SymbolTable.h"
     27 #include "xml/XmlDom.h"
     28 
     29 namespace aapt {
     30 
     31 // Resolves all references to resources in the ResourceTable and assigns them IDs.
     32 // The ResourceTable must already have IDs assigned to each resource.
     33 // Once the ResourceTable is processed by this linker, it is ready to be flattened.
     34 class ReferenceLinker : public IResourceTableConsumer {
     35  public:
     36   ReferenceLinker() = default;
     37 
     38   // Performs name mangling and looks up the resource in the symbol table. Uses the callsite's
     39   // package if the reference has no package name defined (implicit).
     40   // Returns nullptr if the symbol was not found.
     41   static const SymbolTable::Symbol* ResolveSymbol(const Reference& reference,
     42                                                   const CallSite& callsite, SymbolTable* symbols);
     43 
     44   // Performs name mangling and looks up the resource in the symbol table. If the symbol is not
     45   // visible by the reference at the callsite, nullptr is returned.
     46   // `out_error` holds the error message.
     47   static const SymbolTable::Symbol* ResolveSymbolCheckVisibility(const Reference& reference,
     48                                                                  const CallSite& callsite,
     49                                                                  SymbolTable* symbols,
     50                                                                  std::string* out_error);
     51 
     52   // Same as ResolveSymbolCheckVisibility(), but also makes sure the symbol is an attribute.
     53   // That is, the return value will have a non-null value for ISymbolTable::Symbol::attribute.
     54   static const SymbolTable::Symbol* ResolveAttributeCheckVisibility(const Reference& reference,
     55                                                                     const CallSite& callsite,
     56                                                                     SymbolTable* symbols,
     57                                                                     std::string* out_error);
     58 
     59   // Resolves the attribute reference and returns an xml::AaptAttribute if successful.
     60   // If resolution fails, outError holds the error message.
     61   static Maybe<xml::AaptAttribute> CompileXmlAttribute(const Reference& reference,
     62                                                        const CallSite& callsite,
     63                                                        SymbolTable* symbols,
     64                                                        std::string* out_error);
     65 
     66   // Writes the resource name to the DiagMessage, using the
     67   // "orig_name (aka <transformed_name>)" syntax.
     68   static void WriteResourceName(const Reference& orig, const CallSite& callsite,
     69                                 const xml::IPackageDeclStack* decls, DiagMessage* out_msg);
     70 
     71   // Same as WriteResourceName but omits the 'attr' part.
     72   static void WriteAttributeName(const Reference& ref, const CallSite& callsite,
     73                                  const xml::IPackageDeclStack* decls, DiagMessage* out_msg);
     74 
     75   // Transforms the package name of the reference to the fully qualified package name using
     76   // the xml::IPackageDeclStack, then mangles and looks up the symbol. If the symbol is visible
     77   // to the reference at the callsite, the reference is updated with an ID.
     78   // Returns false on failure, and an error message is logged to the IDiagnostics in the context.
     79   static bool LinkReference(const CallSite& callsite, Reference* reference, IAaptContext* context,
     80                             SymbolTable* symbols, const xml::IPackageDeclStack* decls);
     81 
     82   // Links all references in the ResourceTable.
     83   bool Consume(IAaptContext* context, ResourceTable* table) override;
     84 
     85  private:
     86   DISALLOW_COPY_AND_ASSIGN(ReferenceLinker);
     87 };
     88 
     89 }  // namespace aapt
     90 
     91 #endif /* AAPT_LINKER_REFERENCELINKER_H */
     92