Home | History | Annotate | Download | only in types
      1 /*
      2  * Copyright (C) 2011 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 ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_H_
     18 #define ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_H_
     19 
     20 #include "safe_map.h"
     21 #include "dex_file-inl.h"
     22 #include "sea_ir/types/types.h"
     23 
     24 namespace sea_ir {
     25 
     26 class SeaGraph;
     27 class InstructionNode;
     28 
     29 // The type inference in SEA IR is different from the verifier in that it is concerned
     30 // with a rich type hierarchy (TODO) usable in optimization and does not perform
     31 // precise verification (which is the job of the verifier).
     32 class TypeInference {
     33  public:
     34   TypeInference() : type_cache_(new art::verifier::RegTypeCache(false)) {
     35   }
     36 
     37   // Computes the types for the method with SEA IR representation provided by @graph.
     38   void ComputeTypes(SeaGraph* graph);
     39 
     40   art::SafeMap<int, const Type*>* GetTypeMap() {
     41     return type_data_.GetTypeMap();
     42   }
     43   // Returns true if @descriptor corresponds to a primitive type.
     44   static bool IsPrimitiveDescriptor(char descriptor);
     45   TypeData type_data_;    // TODO: Make private, add accessor and not publish a SafeMap above.
     46   art::verifier::RegTypeCache* const type_cache_;    // TODO: Make private.
     47 };
     48 
     49 // Stores information about the exact type of  a function.
     50 class FunctionTypeInfo {
     51  public:
     52   // Finds method information about the method encoded by a SEA IR graph.
     53   // @graph provides the input method SEA IR representation.
     54   // @types provides the input cache of types from which the
     55   //        parameter types of the function are found.
     56   FunctionTypeInfo(const SeaGraph* graph, art::verifier::RegTypeCache* types);
     57   // Finds method information about the method encoded by
     58   // an invocation instruction in a SEA IR graph.
     59   // @graph provides the input method SEA IR representation.
     60   // @inst  is an invocation instruction for the desired method.
     61   // @types provides the input cache of types from which the
     62   //        parameter types of the function are found.
     63   FunctionTypeInfo(const SeaGraph* graph, InstructionNode* inst,
     64       art::verifier::RegTypeCache* types);
     65   // Returns the ordered vector of types corresponding to the function arguments.
     66   std::vector<const Type*> GetDeclaredArgumentTypes();
     67   // Returns the declared return value type.
     68   const Type* GetReturnValueType();
     69   // Returns the type corresponding to the class that declared the method.
     70   const Type& GetDeclaringClass() {
     71     return *declaring_class_;
     72   }
     73 
     74   bool IsConstructor() const {
     75     return (method_access_flags_ & kAccConstructor) != 0;
     76   }
     77 
     78   bool IsStatic() const {
     79     return (method_access_flags_ & kAccStatic) != 0;
     80   }
     81 
     82  protected:
     83   const Type* declaring_class_;
     84   const art::DexFile* dex_file_;
     85   const uint32_t dex_method_idx_;
     86   art::verifier::RegTypeCache* type_cache_;
     87   const uint32_t method_access_flags_;  // Method's access flags.
     88 };
     89 }  // namespace sea_ir
     90 
     91 #endif  // ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_H_
     92