-
Notifications
You must be signed in to change notification settings - Fork 276
[TG-1287] Parsing generic information of base classes #1763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7d041f0
6d691d7
54df3a1
50dcec8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ Author: Daniel Kroening, [email protected] | |
#include <util/std_expr.h> | ||
|
||
#include <linking/zero_initializer.h> | ||
#include <util/suffix.h> | ||
|
||
class java_bytecode_convert_classt:public messaget | ||
{ | ||
|
@@ -85,6 +86,83 @@ class java_bytecode_convert_classt:public messaget | |
static void add_array_types(symbol_tablet &symbol_table); | ||
}; | ||
|
||
/// Auxiliary function to extract the generic superclass reference from the | ||
/// class signature. If the signature is empty or the superclass is not generic | ||
/// it returns empty. | ||
/// Example: | ||
/// - class: A<T> extends B<T, Integer> implements C, D<T> | ||
/// - signature: <T:Ljava/lang/Object;>B<TT;Ljava/lang/Integer;>;LC;LD<TT;>; | ||
/// - returned superclass reference: B<TT;Ljava/lang/Integer;>; | ||
/// \param signature Signature of the class | ||
/// \return Reference of the generic superclass, or empty if the superclass | ||
/// is not generic | ||
static optionalt<std::string> | ||
extract_generic_superclass_reference(const optionalt<std::string> &signature) | ||
{ | ||
if(signature.has_value()) | ||
{ | ||
// skip the (potential) list of generic parameters at the beginning of the | ||
// signature | ||
const size_t start = | ||
signature.value().front() == '<' | ||
? find_closing_delimiter(signature.value(), 0, '<', '>') + 1 | ||
: 0; | ||
|
||
// extract the superclass reference | ||
const size_t end = | ||
find_closing_semi_colon_for_reference_type(signature.value(), start); | ||
const std::string superclass_ref = | ||
signature.value().substr(start, (end - start) + 1); | ||
|
||
// if the superclass is generic then the reference is of form | ||
// Lsuperclass-name<generic-types;>; | ||
if(has_suffix(superclass_ref, ">;")) | ||
return superclass_ref; | ||
} | ||
return {}; | ||
} | ||
|
||
/// Auxiliary function to extract the generic interface reference of an | ||
/// interface with the specified name from the class signature. If the | ||
/// signature is empty or the interface is not generic it returns empty. | ||
/// Example: | ||
/// - class: A<T> extends B<T, Integer> implements C, D<T> | ||
/// - signature: <T:Ljava/lang/Object;>B<TT;Ljava/lang/Integer;>;LC;LD<TT;>; | ||
/// - returned interface reference for C: LC; | ||
/// - returned interface reference for D: LD<TT;>; | ||
/// \param signature Signature of the class | ||
/// \param interface_name The interface name | ||
/// \return Reference of the generic interface, or empty if the interface | ||
/// is not generic | ||
static optionalt<std::string> extract_generic_interface_reference( | ||
const optionalt<std::string> &signature, | ||
const std::string &interface_name) | ||
{ | ||
if(signature.has_value()) | ||
{ | ||
// skip the (potential) list of generic parameters at the beginning of the | ||
// signature | ||
size_t start = | ||
signature.value().front() == '<' | ||
? find_closing_delimiter(signature.value(), 0, '<', '>') + 1 | ||
: 0; | ||
|
||
// skip the superclass reference (if there is at least one interface | ||
// reference in the signature, then there is a superclass reference) | ||
start = | ||
find_closing_semi_colon_for_reference_type(signature.value(), start) + 1; | ||
|
||
start = signature.value().find("L" + interface_name + "<", start); | ||
if(start != std::string::npos) | ||
{ | ||
const size_t &end = | ||
find_closing_semi_colon_for_reference_type(signature.value(), start); | ||
return signature.value().substr(start, (end - start) + 1); | ||
} | ||
} | ||
return {}; | ||
} | ||
|
||
void java_bytecode_convert_classt::convert(const classt &c) | ||
{ | ||
std::string qualified_classname="java::"+id2string(c.name); | ||
|
@@ -145,10 +223,26 @@ void java_bytecode_convert_classt::convert(const classt &c) | |
|
||
if(!c.extends.empty()) | ||
{ | ||
symbol_typet base("java::"+id2string(c.extends)); | ||
class_type.add_base(base); | ||
const symbol_typet base("java::" + id2string(c.extends)); | ||
|
||
// if the superclass is generic then the class has the superclass reference | ||
// including the generic info in its signature | ||
// e.g., signature for class 'A<T>' that extends | ||
// 'Generic<Integer>' is '<T:Ljava/lang/Object;>LGeneric<LInteger;>;' | ||
const optionalt<std::string> &superclass_ref = | ||
extract_generic_superclass_reference(c.signature); | ||
if(superclass_ref.has_value()) | ||
{ | ||
const java_generic_symbol_typet generic_base( | ||
base, superclass_ref.value(), qualified_classname); | ||
class_type.add_base(generic_base); | ||
} | ||
else | ||
{ | ||
class_type.add_base(base); | ||
} | ||
class_typet::componentt base_class_field; | ||
base_class_field.type()=base; | ||
base_class_field.type() = class_type.bases().at(0).type(); | ||
base_class_field.set_name("@"+id2string(c.extends)); | ||
base_class_field.set_base_name("@"+id2string(c.extends)); | ||
base_class_field.set_pretty_name("@"+id2string(c.extends)); | ||
|
@@ -158,8 +252,24 @@ void java_bytecode_convert_classt::convert(const classt &c) | |
// interfaces are recorded as bases | ||
for(const auto &interface : c.implements) | ||
{ | ||
symbol_typet base("java::"+id2string(interface)); | ||
class_type.add_base(base); | ||
const symbol_typet base("java::" + id2string(interface)); | ||
|
||
// if the interface is generic then the class has the interface reference | ||
// including the generic info in its signature | ||
// e.g., signature for class 'A implements GenericInterface<Integer>' is | ||
// 'Ljava/lang/Object;LGenericInterface<LInteger;>;' | ||
const optionalt<std::string> interface_ref = | ||
extract_generic_interface_reference(c.signature, id2string(interface)); | ||
if(interface_ref.has_value()) | ||
{ | ||
const java_generic_symbol_typet generic_base( | ||
base, interface_ref.value(), qualified_classname); | ||
class_type.add_base(generic_base); | ||
} | ||
else | ||
{ | ||
class_type.add_base(base); | ||
} | ||
} | ||
|
||
// produce class symbol | ||
|
@@ -598,6 +708,15 @@ static void find_and_replace_parameters( | |
find_and_replace_parameters(argument, replacement_parameters); | ||
} | ||
} | ||
else if(is_java_generic_symbol_type(type)) | ||
{ | ||
java_generic_symbol_typet &generic_base = to_java_generic_symbol_type(type); | ||
std::vector<reference_typet> &gen_types = generic_base.generic_types(); | ||
for(auto &gen_type : gen_types) | ||
{ | ||
find_and_replace_parameters(gen_type, replacement_parameters); | ||
} | ||
} | ||
} | ||
|
||
/// Checks if the class is implicitly generic, i.e., it is an inner class of | ||
|
@@ -675,5 +794,11 @@ void mark_java_implicitly_generic_class_type( | |
find_and_replace_parameters( | ||
field.type(), implicit_generic_type_parameters); | ||
} | ||
|
||
for(auto &base : class_type.bases()) | ||
{ | ||
find_and_replace_parameters( | ||
base.type(), implicit_generic_type_parameters); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -522,6 +522,65 @@ to_java_specialized_generic_class_type(typet &type) | |
return static_cast<const java_specialized_generic_class_typet &>(type); | ||
} | ||
|
||
/// Type for a generic symbol, extends symbol_typet with a | ||
/// vector of java generic types. | ||
/// This is used to store the type of generic superclasses and interfaces. | ||
class java_generic_symbol_typet : public symbol_typet | ||
{ | ||
public: | ||
typedef std::vector<reference_typet> generic_typest; | ||
|
||
java_generic_symbol_typet( | ||
const symbol_typet &type, | ||
const std::string &base_ref, | ||
const std::string &class_name_prefix) | ||
: symbol_typet(type) | ||
{ | ||
set(ID_C_java_generic_symbol, true); | ||
const typet &base_type = java_type_from_string(base_ref, class_name_prefix); | ||
PRECONDITION(is_java_generic_type(base_type)); | ||
const java_generic_typet gen_base_type = to_java_generic_type(base_type); | ||
generic_types().insert( | ||
generic_types().end(), | ||
gen_base_type.generic_type_arguments().begin(), | ||
gen_base_type.generic_type_arguments().end()); | ||
} | ||
|
||
const generic_typest &generic_types() const | ||
{ | ||
return (const generic_typest &)(find(ID_generic_types).get_sub()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, putting a static cast here causes a compile error. |
||
} | ||
|
||
generic_typest &generic_types() | ||
{ | ||
return (generic_typest &)(add(ID_generic_types).get_sub()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was confused by the name of the function |
||
} | ||
}; | ||
|
||
/// \param type: the type to check | ||
/// \return true if the type is a symbol type with generics | ||
inline bool is_java_generic_symbol_type(const typet &type) | ||
{ | ||
return type.get_bool(ID_C_java_generic_symbol); | ||
} | ||
|
||
/// \param type: the type to convert | ||
/// \return the converted type | ||
inline const java_generic_symbol_typet & | ||
to_java_generic_symbol_type(const typet &type) | ||
{ | ||
PRECONDITION(is_java_generic_symbol_type(type)); | ||
return static_cast<const java_generic_symbol_typet &>(type); | ||
} | ||
|
||
/// \param type: the type to convert | ||
/// \return the converted type | ||
inline java_generic_symbol_typet &to_java_generic_symbol_type(typet &type) | ||
{ | ||
PRECONDITION(is_java_generic_symbol_type(type)); | ||
return static_cast<java_generic_symbol_typet &>(type); | ||
} | ||
|
||
/// Take a signature string and remove everything in angle brackets allowing for | ||
/// the type to be parsed normally, for example | ||
/// `java.util.HashSet<java.lang.Integer>` will be turned into | ||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did this work if
base
was anexprt
andtype()
was atypet
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
base
was asymbol_typet
, only once called.add_base(base)
it was wrapped into anexprt
.