Compiler Implementation#

Preprocessing#

class bshark.compiler._compiler.Preprocessor(unit: Unit)[source]#

A preprocessor for AIDL files.

This simple class can be used to view and inspect basic characteristics of AIDL (and Java) files. The base Unit must be loaded first, e.g. by a BaseLoader.

property declared_class: Node | None#

Returns the body of the unit.

get_creator() Node | None[source]#

Resolves the CREATOR field in a parcelable class.

get_parcel_constructor() Node | None[source]#

Resolves the constructor in a parcelable class.

is_compiled() bool[source]#

Returns whether the unit is already compiled.

is_valid() bool[source]#

Returns whether the unit is valid.

property lang: Language#

Returns the language of the unit.

property qname: str#

Returns the qualified name of the unit.

property rpath: str#

Returns the relative path of the unit.

Internal Model Types#

class bshark.compiler._compiler.TypeHandler[source]#

A special base class to support a mapping of type names to their corresponding Parcel calls.

call_from_expr(expr: Node, tracker: str, compiler: Compiler) str[source]#
call_of(type_decl: Node, compiler: Compiler) str[source]#

Resolves the corresponding call in the Parcel class of a type name.

const_val_of(expr: Node, compiler: Compiler) str[source]#
class bshark.compiler._compiler.NodeVisitor(compiler: Compiler)[source]#

A simple visitor class for traversing the AST.

This class will be responsible for generating FieldDef and ConditionDef instances.

visit(node: Node, tracker: str, index: int) List[FieldDef][source]#

Traverses the given node and returns a list of FieldDef instances (optional).

visit_assignment_expression(expr: Node, tracker: str, index: int) List[FieldDef][source]#

Parse an assignment statement and return a member definitions.

visit_if_statement(expr: Node, tracker: str, index: int) List[FieldDef] | None[source]#
visit_local_variable_declaration(expr: Node, tracker: str, index: int) List[FieldDef] | None[source]#

Parse a local variable declaration and return a member definition.

visit_method_invocation(expr: Node, tracker: str, index: int) List[FieldDef] | None[source]#
visit_return_statement(expr: Node, tracker: str, index: int) List[FieldDef] | None[source]#

Public API#

class bshark.compiler._compiler.Compiler(unit: Unit, loader: BaseLoader, type_handler: TypeHandler | None = None, visitor_cls: Type[NodeVisitor] = None)[source]#

The _compiler_ class is used to translate given AIDL definitions into a pre-defined structure that can be used to decode and potentially encode data.

The internal processing depends on which type the underlying unit was associated with. For instance, BINDER declarations will result in a different output than parsed PARCELABLE_JAVA declarations.

In general, the compiler tries to describe what operations need to be performed on a Unit in order to decode or encode data. There are predefined methods, which will be mapped to their Python equivalents:

Method

Python Type

readInt, readLong, readShort, readByte

int

readBoolean

bool

readString

str (utf-16-le codec)

readString8

str (utf-8 codec)

readFloat, readDouble

float

  • Binder: All defined methods will be inspected and their parameters will be translated according to the scheme introduced before.

Parameters:
  • unit (Unit) – The unit to compile.

  • loader (BaseLoader) – The BaseLoader object to use.

as_binder() BinderDef[source]#

Returns the given unit as a BinderDef.

as_parcelable() ParcelableDef[source]#

Returns the given unit as a ParcelableDef.

compile() BinderDef | ParcelableDef[source]#

Compiles the stored unit. (if possible)

get_assigned_member(expr: Node) str[source]#

Returns the name of the member that is assigned to.

get_import(qname: str) ImportDef[source]#

Returns the import with the given qualified name (or tries to import it).

get_invocation_arguments(invocation: Node) List[str][source]#
get_local_member(expr: Node) str | None[source]#

Returns the name of the member that is assigned to.

get_method_call(expr: Node, tracker: str) Node | None[source]#

Tries to resolve a method invocation node from the given start node.

is_delegate(expr: Node, tracker: str) bool[source]#

Check if a method is a delegate to another internal method (not constructor)

is_local_assignment(expr: Node, tracker: str) bool[source]#

Check if an expression is a local assignment with the tracker.

is_target_assignment(expr: Node, tracker: str) bool[source]#

Check if an expression is an assignment with the tracker.

parse_condition(expr: Node, tracker: str) ConditionDef | None[source]#

Parse an if statement and return a condition definition.

resolve_parcel_tracker(method: Node) str[source]#

Tries to retrieve the parameter name of the parcel argument

property resolved_imports: ImportDefList#

Returns the list of resolved imports.

trace_local(body: Node, tracker: str) str[source]#
property unit: Unit#

Returns the current unit.