1 /* 2 * Copyright (C) 2017 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 #pragma once 18 19 #include "common.h" 20 #include "dex_ir.h" 21 22 #include <memory> 23 24 namespace ir { 25 26 // Packing together the name components which identify a Java method 27 // (depending on the use some fields, ex. signature, are optional) 28 // 29 // NOTE: the "signature" uses the JNI signature syntax: 30 // https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures 31 // 32 struct MethodId { 33 const char* class_descriptor; 34 const char* method_name; 35 const char* signature; 36 37 MethodId(const char* class_descriptor, const char* method_name, const char* signature = nullptr) class_descriptorMethodId38 : class_descriptor(class_descriptor), method_name(method_name), signature(signature) { 39 assert(class_descriptor != nullptr); 40 assert(method_name != nullptr); 41 } 42 43 bool Match(MethodDecl* method_decl) const; 44 }; 45 46 // This class enables modifications to a .dex IR 47 class Builder { 48 public: Builder(std::shared_ptr<ir::DexFile> dex_ir)49 explicit Builder(std::shared_ptr<ir::DexFile> dex_ir) : dex_ir_(dex_ir) {} 50 51 // No copy/move semantics 52 Builder(const Builder&) = delete; 53 Builder& operator=(const Builder&) = delete; 54 55 // Get/Create .dex IR nodes 56 // (get existing instance or create a new one) 57 String* GetAsciiString(const char* cstr); 58 Type* GetType(String* descriptor); 59 Proto* GetProto(Type* return_type, TypeList* param_types); 60 FieldDecl* GetFieldDecl(String* name, Type* type, Type* parent); 61 MethodDecl* GetMethodDecl(String* name, Proto* proto, Type* parent); 62 TypeList* GetTypeList(const std::vector<Type*>& types); 63 64 // Convenience overloads GetType(const char * descriptor)65 Type* GetType(const char* descriptor) { 66 return GetType(GetAsciiString(descriptor)); 67 } 68 69 // Locate an existing method definition 70 // (returns nullptr if the method is not found) 71 EncodedMethod* FindMethod(const MethodId& method_id) const; 72 73 private: 74 // Locate an existing .dex IR string 75 // (returns nullptr if the string is not found) 76 String* FindAsciiString(const char* cstr) const; 77 78 // Locate an existing .dex IR prototype 79 // (returns nullptr if the prototype is not found) 80 Proto* FindPrototype(const char* signature) const; 81 82 private: 83 std::shared_ptr<ir::DexFile> dex_ir_; 84 }; 85 86 } // namespace ir 87