1 
2 #ifndef R_ANNOTATEDCODE_H
3 #define R_ANNOTATEDCODE_H
4 
5 // #include <r_core.h>
6 #include <r_types.h>
7 #include <r_vector.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 typedef enum r_syntax_highlight_type_t {
14 	R_SYNTAX_HIGHLIGHT_TYPE_KEYWORD,
15 	R_SYNTAX_HIGHLIGHT_TYPE_COMMENT,
16 	R_SYNTAX_HIGHLIGHT_TYPE_DATATYPE,
17 	R_SYNTAX_HIGHLIGHT_TYPE_FUNCTION_NAME,
18 	R_SYNTAX_HIGHLIGHT_TYPE_FUNCTION_PARAMETER,
19 	R_SYNTAX_HIGHLIGHT_TYPE_LOCAL_VARIABLE,
20 	R_SYNTAX_HIGHLIGHT_TYPE_CONSTANT_VARIABLE,
21 	R_SYNTAX_HIGHLIGHT_TYPE_GLOBAL_VARIABLE,
22 } RSyntaxHighlightType;
23 
24 /** Represents the type of annnotation. */
25 typedef enum r_code_annotation_type_t {
26 	R_CODE_ANNOTATION_TYPE_OFFSET, /*!< Gives the offset of the specified range in annotation. */
27 	R_CODE_ANNOTATION_TYPE_SYNTAX_HIGHLIGHT, /*!< Represents the kind of data the specified range represents for highlighting purposes. */
28 	R_CODE_ANNOTATION_TYPE_FUNCTION_NAME, /*!< Specified range in annotation represents a function name. */
29 	R_CODE_ANNOTATION_TYPE_GLOBAL_VARIABLE, /*!< Specified range in annotation represents a global variable. */
30 	R_CODE_ANNOTATION_TYPE_CONSTANT_VARIABLE, /*!< Specified range in annotation represents a constant variable with an address. */
31 	R_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE, /*!< Specified range in annotation represents a local variable. */
32 	R_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER, /*!< Specified range in annotation represents a function parameter. */
33 	// ...
34 } RCodeAnnotationType;
35 
36 /**
37  * \brief Annotations for the decompiled code are represented using this structure.
38  */
39 typedef struct r_code_annotation_t {
40 	size_t start; /**< Start of the range in the annotation(inclusive). */
41 	size_t end; /**< End of the range in the annotation(exclusive). */
42 	RCodeAnnotationType type;
43 	union {
44 		/** If the annotation is of type R_CODE_ANNOTATION_TYPE_OFFSET,
45 		 * offset should be stored in the struct named offset in this union.
46 		 */
47 		struct {
48 			ut64 offset;
49 		} offset;
50 		/** If the annotation is of type R_CODE_ANNOTATION_TYPE_SYNTAX_HIGHLIGHT,
51 		 * type of the syntax highlight will be stored in the struct named syntax_highlight
52 		 * in this union.
53 		 */
54 		struct {
55 			RSyntaxHighlightType type;
56 		} syntax_highlight;
57 
58 		/** Information in annotations of type R_CODE_ANNOTATION_TYPE_FUNCTION_NAME,
59 		 * R_CODE_ANNOTATION_TYPE_GLOBAL_VARIABLE, and R_CODE_ANNOTATION_TYPE_CONSTANT_VARIABLE
60 		 * will be stored in the struct named reference in this union.
61 		 */
62 		struct {
63 			char *name;
64 			ut64 offset;
65 		} reference;
66 
67 		/** Information in annotations of type R_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE
68 		 * and R_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER will be stored in the
69 		 * struct named variable in this union.
70 		 */
71 		struct {
72 			char *name;
73 		} variable;
74 	};
75 } RCodeAnnotation;
76 /**
77  * \brief This structure contains the decompiled code and all the annotations for the decompiled code.
78  */
79 typedef struct r_annotated_code_t {
80 	char *code; /**< Decompiled code. RAnnotatedCode owns this string and it must free it. */
81 	RVector annotations; /**< @ref RVector <RCodeAnnotation> contains the list of annotations for the decompiled code. */
82 } RAnnotatedCode;
83 
84 /**
85  * @brief Create and initialize a RAnnotatedCode structure and returns its pointer.
86  *
87  * This function creates and initializes a new RAnnotatedCode
88  * structure with the specified decompiled code that's passed
89  * as an argument. Here, the argument code must be a string that can be deallocated.
90  * This will initialize @ref RVector <RCodeAnnotation> annotations as well.
91  *
92  * @param code A deallocatable character array.
93  * @return Pointer to the new RAnnotatedCode structure created.
94  */
95 R_API RAnnotatedCode *r_annotated_code_new(char *code);
96 /**
97  * @brief Deallocates the dynamically allocated memory for the specified RAnnotatedCode.
98  *
99  * @param code Pointer to a RAnnotatedCode.
100  */
101 R_API void r_annotated_code_free(RAnnotatedCode *code);
102 /**
103  * @brief Deallocates dynamically allocated memory for the specified annotation.
104  *
105  * This function recognizes the type of the specified annotation and
106  * frees memory that is dynamically allocated for it.
107  *
108  * @param e Pointer to the annotation.
109  * @param user Always NULL for this function. Present here for this function to be of the type @ref RVectorFree.
110  */
111 R_API void r_annotation_free(void *e, void *user);
112 /**
113  * @brief Checks if the specified annotation is a reference.
114  *
115  * This function recognizes the type of the specified annotation and returns true if its
116  * type is any of the following three: R_CODE_ANNOTATION_TYPE_GLOBAL_VARIABLE,
117  * R_CODE_ANNOTATION_TYPE_CONSTANT_VARIABLE, R_CODE_ANNOTATION_TYPE_FUNCTION_NAME
118  *
119  * @param annotation Pointer to an annotation.
120  * @return Returns true if the specified annotation is a reference.
121  */
122 R_API bool r_annotation_is_reference(RCodeAnnotation *annotation);
123 /**
124  * @brief Checks if the specified annotation is a function variable.
125  *
126  * This function recognizes the type of the specified annotation and returns true if its
127  * type is any of the following two: R_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE,
128  * R_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER
129  *
130  * @param annotation Pointer to an annotation.
131  * @return Returns true if the specified annotation is a function variable.
132  */
133 R_API bool r_annotation_is_variable(RCodeAnnotation *annotation);
134 /**
135  * @brief Inserts the specified annotation into the list of annotations in the specified RAnnotatedCode.
136  *
137  * @param code Pointer to a RAnnotatedCode.
138  * @param annotation Pointer to an annotation.
139  */
140 R_API void r_annotated_code_add_annotation(RAnnotatedCode *code, RCodeAnnotation *annotation);
141 /**
142  * @brief Returns all annotations with range that contains the given offset.
143  *
144  * Creates a @ref RPVector <RCodeAnnotation> and inserts the pointers to all annotations in which
145  * annotation->start <= offset < annotation->end.
146  *
147  * @param code Pointer to a RAnnotatedCode.
148  * @param offset Offset.
149  * @return Pointer to the @ref RPVector created.
150  */
151 R_API RPVector *r_annotated_code_annotations_in(RAnnotatedCode *code, size_t offset);
152 /**
153  * @brief Returns all annotations with range that overlap with the specified range.
154  *
155  * Creates an @ref RPVector <RCodeAnnotation> and inserts the pointers to all annotations whose
156  * range overlap with range specified.
157  *
158  * @param code Pointer to a RAnnotatedCode.
159  * @param start Start of the range(inclusive).
160  * @param end End of the range(exclusive).
161  * @return Pointer to the @ref RPVector created.
162  */
163 R_API RPVector *r_annotated_code_annotations_range(RAnnotatedCode *code, size_t start, size_t end);
164 /**
165  * @brief Returns the offset for every line of decompiled code in the specified RAnnotatedCode.
166  *
167  * Creates an @ref RVector <ut64> and inserts the offsets for every seperate line of decompiled code in
168  * the specified RAnnotatedCode.
169  * If a line of decompiled code doesn't have a unique offset, UT64_MAX is inserted as its offset.
170  *
171  * @param code Pointer to a RAnnotatedCode.
172  * @return Pointer to the @ref RVector created.
173  */
174 R_API RVector *r_annotated_code_line_offsets(RAnnotatedCode *code);
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 #endif //R_ANNOTATEDCODE_H
181