Skip to content

Commit

Permalink
Add ConstAnnotationDomain
Browse files Browse the repository at this point in the history
Summary: Add TypedefAnnotationDomain for StringDef/IntDef TypeInference. TypedefAnnotationDomain is just a SingletonDexTypeDomain that contains a DexType representing the annotation.

Reviewed By: thezhangwei

Differential Revision: D47887207

fbshipit-source-id: 12e744ab198c0805f27e62405d482a9a68062550
  • Loading branch information
itang00 authored and facebook-github-bot committed Aug 7, 2023
1 parent 42c9278 commit 868a8ed
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 33 deletions.
58 changes: 41 additions & 17 deletions libredex/DexTypeEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ class SmallSetDexTypeDomain final
sparta::AbstractValueKind m_kind;
};

/*
* Domain for StringDef and IntDef annotations, where the DexType will track the
* annotation name. This will enforce null safety and prevent the joins of two
* different annotations.
* https://developer.android.com/studio/write/annotations#enum-annotations
*/
using TypedefAnnotationDomain = SingletonDexTypeDomain;

/*
*
* ArrayConstNullnessDomain X SingletonDexTypeDomain X SmallSetDexTypeDomain
Expand All @@ -245,15 +253,17 @@ class DexTypeDomain final
: public sparta::ReducedProductAbstractDomain<DexTypeDomain,
ArrayConstNullnessDomain,
SingletonDexTypeDomain,
SmallSetDexTypeDomain> {
SmallSetDexTypeDomain,
TypedefAnnotationDomain> {
public:
using ReducedProductAbstractDomain::ReducedProductAbstractDomain;

using BaseType =
sparta::ReducedProductAbstractDomain<DexTypeDomain,
ArrayConstNullnessDomain,
SingletonDexTypeDomain,
SmallSetDexTypeDomain>;
SmallSetDexTypeDomain,
TypedefAnnotationDomain>;

// Some older compilers complain that the class is not default
// constructible. We intended to use the default constructors of the base
Expand All @@ -265,33 +275,40 @@ class DexTypeDomain final
: ReducedProductAbstractDomain(
std::make_tuple(ConstNullnessDomain(v),
SingletonDexTypeDomain(),
SmallSetDexTypeDomain::top())) {}
SmallSetDexTypeDomain::top(),
TypedefAnnotationDomain())) {}

explicit DexTypeDomain(const DexType* array_type, uint32_t array_length)
: ReducedProductAbstractDomain(
std::make_tuple(ArrayNullnessDomain(array_length),
SingletonDexTypeDomain(array_type),
SmallSetDexTypeDomain(array_type))) {}
SmallSetDexTypeDomain(array_type),
TypedefAnnotationDomain())) {}

explicit DexTypeDomain(const DexType* dex_type)
explicit DexTypeDomain(const DexType* dex_type,
const DexType* annotation = nullptr)
: ReducedProductAbstractDomain(
std::make_tuple(ConstNullnessDomain(NOT_NULL),
SingletonDexTypeDomain(dex_type),
SmallSetDexTypeDomain(dex_type))) {}
SmallSetDexTypeDomain(dex_type),
TypedefAnnotationDomain(annotation))) {}

explicit DexTypeDomain(const DexType* dex_type,
const Nullness nullness,
bool is_dex_type_exact)
: ReducedProductAbstractDomain(std::make_tuple(
ConstNullnessDomain(nullness),
SingletonDexTypeDomain(dex_type),
is_dex_type_exact ? SmallSetDexTypeDomain(dex_type)
: SmallSetDexTypeDomain::top())) {}
bool is_dex_type_exact,
const DexType* annotation = nullptr)
: ReducedProductAbstractDomain(
std::make_tuple(ConstNullnessDomain(nullness),
SingletonDexTypeDomain(dex_type),
is_dex_type_exact ? SmallSetDexTypeDomain(dex_type)
: SmallSetDexTypeDomain::top(),
TypedefAnnotationDomain(annotation))) {}

static void reduce_product(std::tuple<ArrayConstNullnessDomain,
SingletonDexTypeDomain,
SmallSetDexTypeDomain>& /* product */) {
}
static void reduce_product(
std::tuple<ArrayConstNullnessDomain,
SingletonDexTypeDomain,
SmallSetDexTypeDomain,
TypedefAnnotationDomain>& /* product */) {}

static DexTypeDomain null() { return DexTypeDomain(IS_NULL); }

Expand Down Expand Up @@ -351,10 +368,16 @@ class DexTypeDomain final

SingletonDexTypeDomain get_single_domain() const { return get<1>(); }

TypedefAnnotationDomain get_annotation_domain() const { return get<3>(); }

boost::optional<const DexType*> get_dex_type() const {
return get<1>().get_dex_type();
}

boost::optional<const DexType*> get_annotation_type() const {
return get<3>().get_dex_type();
}

boost::optional<const DexClass*> get_dex_cls() const {
auto dex_type = get<1>().get_dex_type();
if (!dex_type) {
Expand All @@ -375,7 +398,8 @@ class DexTypeDomain final
: ReducedProductAbstractDomain(
std::make_tuple(ConstNullnessDomain(nullness),
SingletonDexTypeDomain::none(),
SmallSetDexTypeDomain())) {}
SmallSetDexTypeDomain(),
TypedefAnnotationDomain::none())) {}
};

/*
Expand Down
Loading

0 comments on commit 868a8ed

Please sign in to comment.