Rizin
unix-like reverse engineering framework and cli tools
types_storage.c File Reference
#include <rz_types.h>
#include <rz_list.h>
#include <rz_util/rz_str.h>
#include <rz_util/rz_assert.h>
#include <rz_type.h>
#include <tree_sitter/api.h>
#include <types_parser.h>

Go to the source code of this file.

Functions

RzBaseTypec_parser_base_type_find (CParserState *state, RZ_NONNULL const char *name)
 
bool c_parser_base_type_is_forward_definition (CParserState *state, RZ_NONNULL const char *name)
 
bool c_parser_base_type_exists (CParserState *state, RZ_NONNULL const char *name)
 
bool c_parser_base_type_store (CParserState *state, RZ_NONNULL const char *name, ParserTypePair *tpair)
 
bool c_parser_forward_definition_store (CParserState *state, RZ_NONNULL const char *name)
 
bool c_parser_forward_definition_remove (CParserState *state, RZ_NONNULL const char *name)
 
RzCallablec_parser_callable_type_find (CParserState *state, RZ_NONNULL const char *name)
 
bool c_parser_callable_type_exists (CParserState *state, RZ_NONNULL const char *name)
 
bool c_parser_callable_type_store (CParserState *state, RZ_NONNULL const char *name, RZ_NONNULL RzType *type)
 
RZ_OWN ParserTypePairc_parser_new_unspecified_naked_type (CParserState *state, RZ_NONNULL const char *name, bool is_const)
 Creates new unspecified naked type (without base type) based on the name. More...
 
RZ_OWN ParserTypePairc_parser_new_primitive_type (CParserState *state, RZ_NONNULL const char *name, bool is_const)
 Creates new primitive type based on the name. More...
 
RZ_OWN ParserTypePairc_parser_get_primitive_type (CParserState *state, RZ_NONNULL const char *name, bool is_const)
 Returns the primitive type if matching in the types hashtable. More...
 
RZ_OWN ParserTypePairc_parser_new_structure_naked_type (CParserState *state, RZ_NONNULL const char *name)
 Creates new structure naked type (without base type) based on the name. More...
 
RZ_OWN ParserTypePairc_parser_new_structure_type (CParserState *state, RZ_NONNULL const char *name, size_t members_count)
 Creates new structure "type + base type" pair based on the name. More...
 
RZ_OWN ParserTypePairc_parser_get_structure_type (CParserState *state, RZ_NONNULL const char *name)
 Returns the structure type if matching in the types hashtable. More...
 
RZ_OWN ParserTypePairc_parser_new_structure_forward_definition (CParserState *state, RZ_NONNULL const char *name)
 Creates new structure forward definition. More...
 
RZ_OWN ParserTypePairc_parser_new_union_naked_type (CParserState *state, RZ_NONNULL const char *name)
 Creates new union naked type (without base type) based on the name. More...
 
RZ_OWN ParserTypePairc_parser_new_union_type (CParserState *state, RZ_NONNULL const char *name, size_t members_count)
 Creates new union "type + base type" pair based on the name. More...
 
RZ_OWN ParserTypePairc_parser_get_union_type (CParserState *state, RZ_NONNULL const char *name)
 Returns the union type if matching in the types hashtable. More...
 
RZ_OWN ParserTypePairc_parser_new_union_forward_definition (CParserState *state, RZ_NONNULL const char *name)
 Creates new union forward definition. More...
 
RZ_OWN ParserTypePairc_parser_new_enum_naked_type (CParserState *state, RZ_NONNULL const char *name)
 Creates new enum naked type (without base type) based on the name. More...
 
RZ_OWN ParserTypePairc_parser_new_enum_type (CParserState *state, RZ_NONNULL const char *name, size_t cases_count)
 Creates new enumeration type based on the name. More...
 
RZ_OWN ParserTypePairc_parser_get_enum_type (CParserState *state, RZ_NONNULL const char *name)
 Returns the enum type if matching in the types hashtable. More...
 
RZ_OWN ParserTypePairc_parser_new_enum_forward_definition (CParserState *state, RZ_NONNULL const char *name)
 Creates new enum forward definition. More...
 
RZ_OWN ParserTypePairc_parser_new_typedef_naked_type (CParserState *state, RZ_NONNULL const char *name)
 Creates new type alias naked type (without base type) based on the name. More...
 
RZ_OWN ParserTypePairc_parser_new_typedef_forward_definition (CParserState *state, RZ_NONNULL const char *name)
 Creates new type alias forward definition. More...
 
RZ_OWN ParserTypePairc_parser_new_typedef (CParserState *state, RZ_NONNULL const char *name, RZ_NONNULL const char *base)
 Creates new type alias based on the name. More...
 
RZ_OWN ParserTypePairc_parser_get_typedef (CParserState *state, RZ_NONNULL const char *name)
 Returns the type if matching in the types hashtable. More...
 
RZ_OWN RzTypec_parser_new_naked_callable (CParserState *state)
 Creates new naked callable without storing it. More...
 
RZ_OWN RzTypec_parser_new_callable (CParserState *state, RZ_NONNULL const char *name)
 Creates new callable based on the name. More...
 
bool c_parser_new_callable_argument (CParserState *state, RZ_NONNULL RzCallable *callable, RZ_NONNULL const char *name, RZ_OWN RZ_NONNULL RzType *type)
 Adds a new argument to the callable. More...
 
RZ_OWN ParserTypePairc_parser_type_wrap_to_pointer (CParserState *state, ParserTypePair *tpair, bool is_const)
 
RZ_OWN ParserTypePairc_parser_type_wrap_to_array (CParserState *state, ParserTypePair *tpair, size_t size)
 
bool c_parser_pointer_set_subtype (CParserState *state, RZ_BORROW ParserTypePair *tpair, RZ_OWN ParserTypePair *subpair)
 
bool c_parser_array_set_subtype (CParserState *state, RZ_BORROW ParserTypePair *tpair, RZ_OWN ParserTypePair *subpair)
 
RZ_OWN char * c_parser_new_anonymous_structure_name (CParserState *state)
 
RZ_OWN char * c_parser_new_anonymous_union_name (CParserState *state)
 
RZ_OWN char * c_parser_new_anonymous_enum_name (CParserState *state)
 
RZ_OWN char * c_parser_new_anonymous_callable_name (CParserState *state)
 

Function Documentation

◆ c_parser_array_set_subtype()

bool c_parser_array_set_subtype ( CParserState state,
RZ_BORROW ParserTypePair tpair,
RZ_OWN ParserTypePair subpair 
)

Definition at line 889 of file types_storage.c.

889  {
890  rz_return_val_if_fail(state && tpair, false);
891  rz_return_val_if_fail(tpair->type->kind == RZ_TYPE_KIND_ARRAY, false);
892  tpair->type->array.type = subpair->type;
893  tpair->btype = subpair->btype;
894  return true;
895 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
@ RZ_TYPE_KIND_ARRAY
Definition: rz_type.h:130
Definition: dis.h:43

References rz_return_val_if_fail, and RZ_TYPE_KIND_ARRAY.

◆ c_parser_base_type_exists()

◆ c_parser_base_type_find()

RzBaseType* c_parser_base_type_find ( CParserState state,
RZ_NONNULL const char *  name 
)

Definition at line 17 of file types_storage.c.

17  {
18  bool found = false;
19  RzBaseType *base_type = ht_pp_find(state->types, name, &found);
20  if (!found || !base_type) {
21  return NULL;
22  }
23  return base_type;
24 }
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130

References found, and NULL.

Referenced by c_parser_base_type_exists(), c_parser_get_enum_type(), c_parser_get_primitive_type(), c_parser_get_structure_type(), c_parser_get_typedef(), and c_parser_get_union_type().

◆ c_parser_base_type_is_forward_definition()

◆ c_parser_base_type_store()

bool c_parser_base_type_store ( CParserState state,
RZ_NONNULL const char *  name,
ParserTypePair tpair 
)

Definition at line 36 of file types_storage.c.

36  {
37  rz_return_val_if_fail(state && name && tpair && tpair->btype, -1);
38 
40  // We don't create the type if it exists already in the parser
41  // state with the same name
42  return false;
43  }
44 
45  // We store only RzBaseType part of the type pair
46  ht_pp_insert(state->types, name, tpair->btype);
47  return true;
48 }
RzBaseType * btype
Definition: types_parser.h:23
bool c_parser_base_type_exists(CParserState *state, RZ_NONNULL const char *name)
Definition: types_storage.c:32

References ParserTypePair::btype, c_parser_base_type_exists(), and rz_return_val_if_fail.

Referenced by parse_enum_node(), parse_primitive_type(), parse_sized_primitive_type(), parse_struct_node(), parse_typedef_node(), and parse_union_node().

◆ c_parser_callable_type_exists()

bool c_parser_callable_type_exists ( CParserState state,
RZ_NONNULL const char *  name 
)

Definition at line 94 of file types_storage.c.

94  {
96 }
RzCallable * c_parser_callable_type_find(CParserState *state, RZ_NONNULL const char *name)
Definition: types_storage.c:85

References c_parser_callable_type_find(), and NULL.

Referenced by c_parser_callable_type_store().

◆ c_parser_callable_type_find()

RzCallable* c_parser_callable_type_find ( CParserState state,
RZ_NONNULL const char *  name 
)

Definition at line 85 of file types_storage.c.

85  {
86  bool found = false;
87  RzCallable *callable = ht_pp_find(state->callables, name, &found);
88  if (!found || !callable) {
89  return NULL;
90  }
91  return callable;
92 }

References found, and NULL.

Referenced by c_parser_callable_type_exists().

◆ c_parser_callable_type_store()

bool c_parser_callable_type_store ( CParserState state,
RZ_NONNULL const char *  name,
RZ_NONNULL RzType type 
)

Definition at line 98 of file types_storage.c.

98  {
101  rz_return_val_if_fail(type->callable, -1);
102 
104  // We don't create the type if it exists already in the parser
105  // state with the same name
106  return false;
107  }
108 
109  ht_pp_insert(state->callables, name, type->callable);
110  parser_debug(state, "Stored \"%s\" callable type\n", name);
111  return true;
112 }
int type
Definition: mipsasm.c:17
@ RZ_TYPE_KIND_CALLABLE
Definition: rz_type.h:131
void parser_debug(CParserState *state, const char *fmt,...)
Definition: types_parser.c:37
bool c_parser_callable_type_exists(CParserState *state, RZ_NONNULL const char *name)
Definition: types_storage.c:94

References c_parser_callable_type_exists(), parser_debug(), rz_return_val_if_fail, RZ_TYPE_KIND_CALLABLE, and type.

Referenced by parse_type_abstract_declarator_node(), and parse_type_declarator_node().

◆ c_parser_forward_definition_remove()

bool c_parser_forward_definition_remove ( CParserState state,
RZ_NONNULL const char *  name 
)

Definition at line 70 of file types_storage.c.

70  {
72 
74  // We don't create the forward definition if it exists already in the parser
75  // types table state with the same name
76  return false;
77  }
78 
79  ht_pp_delete(state->forward, name);
80  return true;
81 }

References c_parser_base_type_exists(), and rz_return_val_if_fail.

Referenced by parse_enum_node(), parse_struct_node(), parse_typedef_node(), and parse_union_node().

◆ c_parser_forward_definition_store()

bool c_parser_forward_definition_store ( CParserState state,
RZ_NONNULL const char *  name 
)

Definition at line 50 of file types_storage.c.

50  {
52 
54  // We don't create the forward definition if it exists already in the parser
55  // types table state with the same name
56  return false;
57  }
58 
60  // We don't create the forward definition if it already stored
61  // as the forward definition with the same name
62  return false;
63  }
64 
65  // We store only the type name
66  ht_pp_insert(state->forward, name, NULL);
67  return true;
68 }
bool c_parser_base_type_is_forward_definition(CParserState *state, RZ_NONNULL const char *name)
Definition: types_storage.c:26

References c_parser_base_type_exists(), c_parser_base_type_is_forward_definition(), NULL, and rz_return_val_if_fail.

Referenced by c_parser_new_typedef(), and parse_sole_type_name().

◆ c_parser_get_enum_type()

RZ_OWN ParserTypePair* c_parser_get_enum_type ( CParserState state,
RZ_NONNULL const char *  name 
)

Returns the enum type if matching in the types hashtable.

If the name matches with the name of one of the base types that are in the hashtable of the existing types in the parser state, then it creates new RzType with the found RzBaseType as a base. Then it wraps boths in the "type pair"

Parameters
stateThe parser state
nameName of the enum type to fetch

Definition at line 573 of file types_storage.c.

573  {
575 
577  if (!base_type || base_type->kind != RZ_BASE_TYPE_KIND_ENUM) {
578  return NULL;
579  }
580 
582  if (!tpair) {
583  return NULL;
584  }
585 
586  tpair->btype = base_type;
587  return tpair;
588 }
@ RZ_BASE_TYPE_KIND_ENUM
Definition: rz_type.h:75
RzBaseTypeKind kind
Definition: rz_type.h:115
RZ_OWN ParserTypePair * c_parser_new_enum_naked_type(CParserState *state, RZ_NONNULL const char *name)
Creates new enum naked type (without base type) based on the name.

References ParserTypePair::btype, c_parser_base_type_find(), c_parser_new_enum_naked_type(), rz_base_type_t::kind, NULL, RZ_BASE_TYPE_KIND_ENUM, and rz_return_val_if_fail.

Referenced by parse_enum_node().

◆ c_parser_get_primitive_type()

RZ_OWN ParserTypePair* c_parser_get_primitive_type ( CParserState state,
RZ_NONNULL const char *  name,
bool  is_const 
)

Returns the primitive type if matching in the types hashtable.

If the name matches with the name of one of the base types that are in the hashtable of the existing types in the parser state, then it creates new RzType with the found RzBaseType as a base. Then it wraps boths in the "type pair"

Parameters
stateThe parser state
nameName of the primitive type to fetch
is_constIf the primitive type is const

Definition at line 209 of file types_storage.c.

209  {
211 
213  if (!base_type || base_type->kind != RZ_BASE_TYPE_KIND_ATOMIC) {
214  return NULL;
215  }
216 
218  if (!type) {
219  return NULL;
220  }
222  type->identifier.is_const = is_const;
223  type->identifier.name = strdup(name);
224  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED;
225 
227  if (!tpair) {
229  return NULL;
230  }
231  tpair->btype = base_type;
232  tpair->type = type;
233  return tpair;
234 }
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
@ RZ_BASE_TYPE_KIND_ATOMIC
Definition: rz_type.h:77
@ RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED
Definition: rz_type.h:135
@ RZ_TYPE_KIND_IDENTIFIER
Definition: rz_type.h:128
#define RZ_NEW0(x)
Definition: rz_types.h:284
RzType * type
Definition: types_parser.h:24
RZ_API void rz_type_free(RZ_NULLABLE RzType *type)
Frees the RzType.
Definition: type.c:1273

References ParserTypePair::btype, c_parser_base_type_find(), rz_base_type_t::kind, NULL, RZ_BASE_TYPE_KIND_ATOMIC, RZ_NEW0, rz_return_val_if_fail, rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by parse_primitive_type(), parse_sized_primitive_type(), and parse_sole_type_name().

◆ c_parser_get_structure_type()

RZ_OWN ParserTypePair* c_parser_get_structure_type ( CParserState state,
RZ_NONNULL const char *  name 
)

Returns the structure type if matching in the types hashtable.

If the name matches with the name of one of the base types that are in the hashtable of the existing types in the parser state, then it creates new RzType with the found RzBaseType as a base. Then it wraps boths in the "type pair"

Parameters
stateThe parser state
nameName of the structure type to fetch

Definition at line 317 of file types_storage.c.

317  {
319 
321  if (!base_type || base_type->kind != RZ_BASE_TYPE_KIND_STRUCT) {
322  return NULL;
323  }
324 
326  if (!tpair) {
327  return NULL;
328  }
329 
330  tpair->btype = base_type;
331  return tpair;
332 }
@ RZ_BASE_TYPE_KIND_STRUCT
Definition: rz_type.h:73
RZ_OWN ParserTypePair * c_parser_new_structure_naked_type(CParserState *state, RZ_NONNULL const char *name)
Creates new structure naked type (without base type) based on the name.

References ParserTypePair::btype, c_parser_base_type_find(), c_parser_new_structure_naked_type(), rz_base_type_t::kind, NULL, RZ_BASE_TYPE_KIND_STRUCT, and rz_return_val_if_fail.

Referenced by parse_sole_type_name(), and parse_struct_node().

◆ c_parser_get_typedef()

RZ_OWN ParserTypePair* c_parser_get_typedef ( CParserState state,
RZ_NONNULL const char *  name 
)

Returns the type if matching in the types hashtable.

If the name matches with the name of one of the type aliases that are in the hashtable of the existing types in the parser state, then it creates new RzType with the found RzBaseType as a base. Then it wraps boths in the "type pair"

Parameters
stateThe parser state
nameName of the type alias to fetch

Definition at line 739 of file types_storage.c.

739  {
741 
743  if (!base_type || base_type->kind != RZ_BASE_TYPE_KIND_TYPEDEF) {
744  return NULL;
745  }
746 
748  if (!type) {
749  return NULL;
750  }
752  type->identifier.is_const = false;
753  type->identifier.name = strdup(name);
754  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED;
755 
757  if (!tpair) {
759  return NULL;
760  }
761  tpair->btype = base_type;
762  tpair->type = type;
763  return tpair;
764 }
@ RZ_BASE_TYPE_KIND_TYPEDEF
Definition: rz_type.h:76

References ParserTypePair::btype, c_parser_base_type_find(), rz_base_type_t::kind, NULL, RZ_BASE_TYPE_KIND_TYPEDEF, RZ_NEW0, rz_return_val_if_fail, rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by parse_sole_type_name().

◆ c_parser_get_union_type()

RZ_OWN ParserTypePair* c_parser_get_union_type ( CParserState state,
RZ_NONNULL const char *  name 
)

Returns the union type if matching in the types hashtable.

If the name matches with the name of one of the base types that are in the hashtable of the existing types in the parser state, then it creates new RzType with the found RzBaseType as a base. Then it wraps boths in the "type pair"

Parameters
stateThe parser state
nameName of the union type to fetch

Definition at line 445 of file types_storage.c.

445  {
447 
449  if (!base_type || base_type->kind != RZ_BASE_TYPE_KIND_UNION) {
450  return NULL;
451  }
452 
454  if (!tpair) {
455  return NULL;
456  }
457 
458  tpair->btype = base_type;
459  return tpair;
460 }
@ RZ_BASE_TYPE_KIND_UNION
Definition: rz_type.h:74
RZ_OWN ParserTypePair * c_parser_new_union_naked_type(CParserState *state, RZ_NONNULL const char *name)
Creates new union naked type (without base type) based on the name.

References ParserTypePair::btype, c_parser_base_type_find(), c_parser_new_union_naked_type(), rz_base_type_t::kind, NULL, RZ_BASE_TYPE_KIND_UNION, and rz_return_val_if_fail.

Referenced by parse_sole_type_name(), and parse_union_node().

◆ c_parser_new_anonymous_callable_name()

RZ_OWN char* c_parser_new_anonymous_callable_name ( CParserState state)

Definition at line 915 of file types_storage.c.

915  {
916  char *name = rz_str_newf("anonymous function %zu", state->anon.callables);
917  state->anon.enums++;
918  return name;
919 }
const char * name
Definition: op.c:541
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1

References name, and rz_str_newf().

Referenced by parse_type_abstract_declarator_node().

◆ c_parser_new_anonymous_enum_name()

RZ_OWN char* c_parser_new_anonymous_enum_name ( CParserState state)

Definition at line 909 of file types_storage.c.

909  {
910  char *name = rz_str_newf("anonymous enum %zu", state->anon.enums);
911  state->anon.enums++;
912  return name;
913 }

References name, and rz_str_newf().

Referenced by parse_enum_node().

◆ c_parser_new_anonymous_structure_name()

RZ_OWN char* c_parser_new_anonymous_structure_name ( CParserState state)

Definition at line 897 of file types_storage.c.

897  {
898  char *name = rz_str_newf("anonymous struct %zu", state->anon.structs);
899  state->anon.structs++;
900  return name;
901 }

References name, and rz_str_newf().

Referenced by parse_struct_node().

◆ c_parser_new_anonymous_union_name()

RZ_OWN char* c_parser_new_anonymous_union_name ( CParserState state)

Definition at line 903 of file types_storage.c.

903  {
904  char *name = rz_str_newf("anonymous union %zu", state->anon.unions);
905  state->anon.unions++;
906  return name;
907 }

References name, and rz_str_newf().

Referenced by parse_union_node().

◆ c_parser_new_callable()

RZ_OWN RzType* c_parser_new_callable ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new callable based on the name.

If the name matches with the name of one of the base types that are in the hashtable of the existing types in the parser state, then it creates new RzType with the found RzCallable as a base.

In case of the callable found in the hashtable the ownership transfer doesn't happen. If not - it does.

Parameters
stateThe parser state
nameName of the callable type to create

Definition at line 802 of file types_storage.c.

802  {
805  if (!type) {
806  return NULL;
807  }
808  // We check if there is already a callable in the hashtable with the same name
809  bool found = false;
810  RzCallable *callable = ht_pp_find(state->callables, name, &found);
811  if (!found || !callable) {
812  // If not found - create a new one
813  callable = RZ_NEW0(RzCallable);
814  if (!callable) {
815  free(type);
816  return NULL;
817  }
818  callable->name = strdup(name);
820  }
821  type->kind = RZ_TYPE_KIND_CALLABLE;
822  type->callable = callable;
823  return type;
824 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API RzPVector * rz_pvector_new(RzPVectorFree free)
Definition: vector.c:302
void(* RzPVectorFree)(void *e)
Definition: rz_vector.h:43
RzPVector * args
optional for the time being
Definition: rz_type.h:149
RZ_NULLABLE char * name
Definition: rz_type.h:147
RZ_API void rz_type_callable_arg_free(RzCallableArg *arg)
Frees the RzCallableArg.
Definition: function.c:101

References rz_callable_at::args, found, free(), rz_callable_at::name, NULL, RZ_NEW0, rz_pvector_new(), rz_return_val_if_fail, rz_type_callable_arg_free(), RZ_TYPE_KIND_CALLABLE, strdup(), and type.

Referenced by parse_type_abstract_declarator_node().

◆ c_parser_new_callable_argument()

bool c_parser_new_callable_argument ( CParserState state,
RZ_NONNULL RzCallable callable,
RZ_NONNULL const char *  name,
RZ_OWN RZ_NONNULL RzType type 
)

Adds a new argument to the callable.

Parameters
stateThe parser state
callableCallable type
nameName of the argument
typeType of the argument

Definition at line 834 of file types_storage.c.

834  {
835  rz_return_val_if_fail(state && callable && name && type, false);
836  // At first we check if there is an argument with the same name already - error if yes
837  void **it;
838  rz_pvector_foreach (callable->args, it) {
839  RzCallableArg *arg = *it;
840  if (!strcmp(arg->name, name)) {
841  return false;
842  }
843  }
844  // And only if there is no argument with the same name - proceed to insert it
846  if (!arg) {
847  return false;
848  }
849  arg->name = strdup(name);
850  arg->type = type;
851  rz_pvector_push(callable->args, arg);
852  return true;
853 }
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
const char * name
Definition: sparc-opc.c:1838

References arg::name, RZ_NEW0, rz_pvector_foreach, rz_pvector_push(), rz_return_val_if_fail, strdup(), and type.

Referenced by parse_parameter_list().

◆ c_parser_new_enum_forward_definition()

RZ_OWN ParserTypePair* c_parser_new_enum_forward_definition ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new enum forward definition.

Parameters
stateThe parser state
nameName of the enum C type to create

Definition at line 596 of file types_storage.c.

596  {
598 
600  // We don't create the enum if it exists already in the parser
601  // state with the same name
602  return NULL;
603  }
604 
606  // We don't create the enum if it exists already in the forward
607  // definitions table with the same name
608  return NULL;
609  }
610 
612  if (!tpair) {
613  return NULL;
614  }
615 
616  return tpair;
617 }

References c_parser_base_type_exists(), c_parser_base_type_is_forward_definition(), c_parser_new_enum_naked_type(), NULL, and rz_return_val_if_fail.

Referenced by parse_enum_node().

◆ c_parser_new_enum_naked_type()

RZ_OWN ParserTypePair* c_parser_new_enum_naked_type ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new enum naked type (without base type) based on the name.

Parameters
stateThe parser state
nameName of the enum C type to create

Definition at line 497 of file types_storage.c.

497  {
499 
501  if (!type) {
502  return NULL;
503  }
505  type->identifier.is_const = false;
506  type->identifier.name = strdup(name);
507  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_ENUM;
508 
510  if (!tpair) {
512  return NULL;
513  }
514  tpair->btype = NULL;
515  tpair->type = type;
516  return tpair;
517 }
@ RZ_TYPE_IDENTIFIER_KIND_ENUM
Definition: rz_type.h:138

References ParserTypePair::btype, NULL, RZ_NEW0, rz_return_val_if_fail, rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_ENUM, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by c_parser_get_enum_type(), c_parser_new_enum_forward_definition(), c_parser_new_enum_type(), and parse_enum_node().

◆ c_parser_new_enum_type()

RZ_OWN ParserTypePair* c_parser_new_enum_type ( CParserState state,
RZ_NONNULL const char *  name,
size_t  cases_count 
)

Creates new enumeration type based on the name.

Parameters
stateThe parser state
nameName of the primitive C type to create
cases_countThe count of the enum cases

Definition at line 526 of file types_storage.c.

526  {
528 
530  // We don't create the structure if it exists already in the parser
531  // state with the same name
532  parser_error(state, "Enum type \"%s\" already exists\n", name);
533  return NULL;
534  }
535 
537  if (!tpair) {
538  return NULL;
539  }
540 
542  if (!base_type) {
543  rz_type_free(tpair->type);
544  free(tpair);
545  return NULL;
546  }
547 
548  base_type->name = strdup(name);
549  base_type->type = NULL;
550  tpair->btype = base_type;
551 
552  RzVector *cases = &base_type->enum_data.cases;
553  if (cases_count > 0 && !rz_vector_reserve(cases, cases_count)) {
554  rz_type_free(tpair->type);
556  free(tpair);
557  return NULL;
558  }
559  return tpair;
560 }
RZ_API void rz_type_base_type_free(RzBaseType *type)
Frees the RzBaseType instance and all of its members.
Definition: base.c:132
RZ_API RZ_OWN RzBaseType * rz_type_base_type_new(RzBaseTypeKind kind)
Allocates a new instance of RzBaseType given the kind.
Definition: base.c:162
RZ_API void * rz_vector_reserve(RzVector *vec, size_t capacity)
Definition: vector.c:214
RzVector cases
Definition: rz_type.h:108
char * name
Definition: rz_type.h:112
RzType * type
Definition: rz_type.h:113
RzBaseTypeEnum enum_data
Definition: rz_type.h:119
void parser_error(CParserState *state, const char *fmt,...)
Definition: types_parser.c:47

References ParserTypePair::btype, c_parser_base_type_exists(), c_parser_new_enum_naked_type(), rz_base_type_enum_t::cases, rz_base_type_t::enum_data, free(), rz_base_type_t::name, NULL, parser_error(), RZ_BASE_TYPE_KIND_ENUM, rz_return_val_if_fail, rz_type_base_type_free(), rz_type_base_type_new(), rz_type_free(), rz_vector_reserve(), strdup(), rz_base_type_t::type, and ParserTypePair::type.

Referenced by parse_enum_node().

◆ c_parser_new_naked_callable()

RZ_OWN RzType* c_parser_new_naked_callable ( CParserState state)

Creates new naked callable without storing it.

Parameters
stateThe parser state

Definition at line 771 of file types_storage.c.

771  {
774  if (!type) {
775  return NULL;
776  }
777  RzCallable *callable = RZ_NEW0(RzCallable);
778  if (!callable) {
779  free(type);
780  return NULL;
781  }
782  callable->name = NULL;
784  type->kind = RZ_TYPE_KIND_CALLABLE;
785  type->callable = callable;
786  return type;
787 }

References rz_callable_at::args, free(), rz_callable_at::name, NULL, RZ_NEW0, rz_pvector_new(), rz_return_val_if_fail, rz_type_callable_arg_free(), RZ_TYPE_KIND_CALLABLE, and type.

Referenced by parse_type_declarator_node().

◆ c_parser_new_primitive_type()

RZ_OWN ParserTypePair* c_parser_new_primitive_type ( CParserState state,
RZ_NONNULL const char *  name,
bool  is_const 
)

Creates new primitive type based on the name.

Parameters
stateThe parser state
nameName of the primitive C type to create
is_constIf the primitive type is const

Definition at line 150 of file types_storage.c.

150  {
152 
154  // We don't create the type if it exists already in the parser
155  // state with the same name
156  parser_error(state, "Primitive type \"%s\" already exists\n", name);
157  return NULL;
158  }
159 
161  if (!type) {
162  return NULL;
163  }
165  type->identifier.is_const = is_const;
166  type->identifier.name = strdup(name);
167  if (!type->identifier.name) {
168  free(type);
169  return NULL;
170  }
171  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED;
172 
174  if (!base_type) {
176  return NULL;
177  }
178  base_type->name = strdup(name);
179  if (!base_type->name) {
181  rz_type_base_type_free(base_type);
182  return NULL;
183  }
184 
186  if (!tpair) {
188  rz_type_base_type_free(base_type);
189  return NULL;
190  }
191  tpair->btype = base_type;
192  tpair->type = type;
193 
194  return tpair;
195 }

References ParserTypePair::btype, c_parser_base_type_exists(), free(), rz_base_type_t::name, NULL, parser_error(), RZ_BASE_TYPE_KIND_ATOMIC, RZ_NEW0, rz_return_val_if_fail, rz_type_base_type_free(), rz_type_base_type_new(), rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by parse_primitive_type(), parse_sized_primitive_type(), and parse_sole_type_name().

◆ c_parser_new_structure_forward_definition()

RZ_OWN ParserTypePair* c_parser_new_structure_forward_definition ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new structure forward definition.

Parameters
stateThe parser state
nameName of the structure C type to create

Definition at line 340 of file types_storage.c.

340  {
342 
344  // We don't create the structure if it exists already in the parser
345  // state with the same name
346  return NULL;
347  }
348 
350  // We don't create the structure if it exists already in the forward
351  // definitions table with the same name
352  return NULL;
353  }
354 
356  if (!tpair) {
357  return NULL;
358  }
359 
360  return tpair;
361 }

References c_parser_base_type_exists(), c_parser_base_type_is_forward_definition(), c_parser_new_structure_naked_type(), NULL, and rz_return_val_if_fail.

Referenced by parse_struct_node().

◆ c_parser_new_structure_naked_type()

RZ_OWN ParserTypePair* c_parser_new_structure_naked_type ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new structure naked type (without base type) based on the name.

Parameters
stateThe parser state
nameName of the structure C type to create

Definition at line 242 of file types_storage.c.

242  {
244 
246  if (!type) {
247  return NULL;
248  }
250  type->identifier.is_const = false;
251  type->identifier.name = strdup(name);
252  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_STRUCT;
253 
255  if (!tpair) {
257  return NULL;
258  }
259  tpair->btype = NULL;
260  tpair->type = type;
261  return tpair;
262 }
@ RZ_TYPE_IDENTIFIER_KIND_STRUCT
Definition: rz_type.h:136

References ParserTypePair::btype, NULL, RZ_NEW0, rz_return_val_if_fail, rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_STRUCT, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by c_parser_get_structure_type(), c_parser_new_structure_forward_definition(), c_parser_new_structure_type(), and parse_struct_node().

◆ c_parser_new_structure_type()

RZ_OWN ParserTypePair* c_parser_new_structure_type ( CParserState state,
RZ_NONNULL const char *  name,
size_t  members_count 
)

Creates new structure "type + base type" pair based on the name.

Parameters
stateThe parser state
nameName of the structure C type to create
members_countThe count of the structure members

Definition at line 271 of file types_storage.c.

271  {
273 
275  // We don't create the structure if it exists already in the parser
276  // state with the same name
277  parser_error(state, "Structure type \"%s\" already exists\n", name);
278  return NULL;
279  }
280 
282  if (!tpair) {
283  return NULL;
284  }
285 
287  if (!base_type) {
288  rz_type_free(tpair->type);
289  free(tpair);
290  return NULL;
291  }
292  base_type->name = strdup(name);
293  base_type->type = NULL;
294  tpair->btype = base_type;
295 
296  RzVector *members = &base_type->struct_data.members;
297  if (members_count > 0 && !rz_vector_reserve(members, members_count)) {
298  rz_type_free(tpair->type);
300  free(tpair);
301  return NULL;
302  }
303  return tpair;
304 }
RzBaseTypeStruct struct_data
Definition: rz_type.h:118

References ParserTypePair::btype, c_parser_base_type_exists(), c_parser_new_structure_naked_type(), free(), rz_base_type_struct_t::members, rz_base_type_t::name, NULL, parser_error(), RZ_BASE_TYPE_KIND_STRUCT, rz_return_val_if_fail, rz_type_base_type_free(), rz_type_base_type_new(), rz_type_free(), rz_vector_reserve(), strdup(), rz_base_type_t::struct_data, rz_base_type_t::type, and ParserTypePair::type.

Referenced by parse_struct_node().

◆ c_parser_new_typedef()

RZ_OWN ParserTypePair* c_parser_new_typedef ( CParserState state,
RZ_NONNULL const char *  name,
RZ_NONNULL const char *  base 
)

Creates new type alias based on the name.

Parameters
stateThe parser state
nameName of the primitive C type to create
cases_countThe count of the enum cases

Definition at line 683 of file types_storage.c.

683  {
685 
687  // We don't create the type alias if it exists already in the parser
688  // state with the same name
689  parser_error(state, "Typedef \"%s\" already exists\n", name);
690  return NULL;
691  }
692 
694  if (!tpair) {
695  return NULL;
696  }
697 
699  if (!base_type) {
700  rz_type_free(tpair->type);
701  free(tpair);
702  return NULL;
703  }
704  base_type->name = strdup(name);
705 
706  if (!c_parser_base_type_exists(state, base)) {
707  // If it not exists already in the parser
708  // we create a forward type
709  base_type->type = NULL;
711  } else {
713  if (!type) {
714  rz_type_base_type_free(base_type);
715  free(tpair);
716  return NULL;
717  }
719  type->identifier.name = strdup(base);
720  type->identifier.is_const = false;
721  base_type->type = type;
722  }
723 
724  tpair->btype = base_type;
725  return tpair;
726 }
bool c_parser_forward_definition_store(CParserState *state, RZ_NONNULL const char *name)
Definition: types_storage.c:50
RZ_OWN ParserTypePair * c_parser_new_typedef_naked_type(CParserState *state, RZ_NONNULL const char *name)
Creates new type alias naked type (without base type) based on the name.

References ParserTypePair::btype, c_parser_base_type_exists(), c_parser_forward_definition_store(), c_parser_new_typedef_naked_type(), free(), rz_base_type_t::name, NULL, parser_error(), RZ_BASE_TYPE_KIND_TYPEDEF, RZ_NEW0, rz_return_val_if_fail, rz_type_base_type_free(), rz_type_base_type_new(), rz_type_free(), RZ_TYPE_KIND_IDENTIFIER, strdup(), type, rz_base_type_t::type, and ParserTypePair::type.

Referenced by parse_typedef_node().

◆ c_parser_new_typedef_forward_definition()

RZ_OWN ParserTypePair* c_parser_new_typedef_forward_definition ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new type alias forward definition.

Parameters
stateThe parser state
nameName of the typedef C type to create

Definition at line 653 of file types_storage.c.

653  {
655 
657  // We don't create the typedef if it exists already in the parser
658  // state with the same name
659  return NULL;
660  }
661 
663  // We don't create the typedef if it exists already in the forward
664  // definitions table with the same name
665  return NULL;
666  }
667 
669  if (!tpair) {
670  return NULL;
671  }
672 
673  return tpair;
674 }

References c_parser_base_type_exists(), c_parser_base_type_is_forward_definition(), c_parser_new_typedef_naked_type(), NULL, and rz_return_val_if_fail.

◆ c_parser_new_typedef_naked_type()

RZ_OWN ParserTypePair* c_parser_new_typedef_naked_type ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new type alias naked type (without base type) based on the name.

Parameters
stateThe parser state
nameName of the typedef C type to create

Definition at line 625 of file types_storage.c.

625  {
627 
629  if (!type) {
630  return NULL;
631  }
633  type->identifier.is_const = false;
634  type->identifier.name = strdup(name);
635  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED;
636 
638  if (!tpair) {
640  return NULL;
641  }
642  tpair->btype = NULL;
643  tpair->type = type;
644  return tpair;
645 }

References ParserTypePair::btype, NULL, RZ_NEW0, rz_return_val_if_fail, rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by c_parser_new_typedef(), and c_parser_new_typedef_forward_definition().

◆ c_parser_new_union_forward_definition()

RZ_OWN ParserTypePair* c_parser_new_union_forward_definition ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new union forward definition.

Parameters
stateThe parser state
nameName of the union C type to create

Definition at line 468 of file types_storage.c.

468  {
470 
472  // We don't create the union if it exists already in the parser
473  // state with the same name
474  return NULL;
475  }
476 
478  // We don't create the union if it exists already in the forward
479  // definitions table with the same name
480  return NULL;
481  }
482 
484  if (!tpair) {
485  return NULL;
486  }
487 
488  return tpair;
489 }

References c_parser_base_type_exists(), c_parser_base_type_is_forward_definition(), c_parser_new_union_naked_type(), NULL, and rz_return_val_if_fail.

Referenced by parse_union_node().

◆ c_parser_new_union_naked_type()

RZ_OWN ParserTypePair* c_parser_new_union_naked_type ( CParserState state,
RZ_NONNULL const char *  name 
)

Creates new union naked type (without base type) based on the name.

Parameters
stateThe parser state
nameName of the union C type to create

Definition at line 369 of file types_storage.c.

369  {
371 
373  if (!type) {
374  return NULL;
375  }
377  type->identifier.is_const = false;
378  type->identifier.name = strdup(name);
379  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_UNION;
380 
382  if (!tpair) {
384  return NULL;
385  }
386  tpair->btype = NULL;
387  tpair->type = type;
388  return tpair;
389 }
@ RZ_TYPE_IDENTIFIER_KIND_UNION
Definition: rz_type.h:137

References ParserTypePair::btype, NULL, RZ_NEW0, rz_return_val_if_fail, rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_UNION, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by c_parser_get_union_type(), c_parser_new_union_forward_definition(), c_parser_new_union_type(), and parse_union_node().

◆ c_parser_new_union_type()

RZ_OWN ParserTypePair* c_parser_new_union_type ( CParserState state,
RZ_NONNULL const char *  name,
size_t  members_count 
)

Creates new union "type + base type" pair based on the name.

Parameters
stateThe parser state
nameName of the union C type to create
members_countThe count of the union members

Definition at line 398 of file types_storage.c.

398  {
400 
402  // We don't create the structure if it exists already in the parser
403  // state with the same name
404  parser_error(state, "Union type \"%s\" already exists\n", name);
405  return NULL;
406  }
407 
409  if (!tpair) {
410  return NULL;
411  }
412 
414  if (!base_type) {
415  rz_type_free(tpair->type);
416  free(tpair);
417  return NULL;
418  }
419 
420  base_type->name = strdup(name);
421  base_type->type = NULL;
422  tpair->btype = base_type;
423 
424  RzVector *members = &base_type->union_data.members;
425  if (members_count > 0 && !rz_vector_reserve(members, members_count)) {
426  rz_type_free(tpair->type);
428  free(tpair);
429  return NULL;
430  }
431  return tpair;
432 }
RzBaseTypeUnion union_data
Definition: rz_type.h:120
RzVector members
Definition: rz_type.h:104

References ParserTypePair::btype, c_parser_base_type_exists(), c_parser_new_union_naked_type(), free(), rz_base_type_union_t::members, rz_base_type_t::name, NULL, parser_error(), RZ_BASE_TYPE_KIND_UNION, rz_return_val_if_fail, rz_type_base_type_free(), rz_type_base_type_new(), rz_type_free(), rz_vector_reserve(), strdup(), rz_base_type_t::type, ParserTypePair::type, and rz_base_type_t::union_data.

Referenced by parse_union_node().

◆ c_parser_new_unspecified_naked_type()

RZ_OWN ParserTypePair* c_parser_new_unspecified_naked_type ( CParserState state,
RZ_NONNULL const char *  name,
bool  is_const 
)

Creates new unspecified naked type (without base type) based on the name.

Parameters
stateThe parser state
nameName of the type to create
is_constIf the identifier is constant

Definition at line 121 of file types_storage.c.

121  {
123 
125  if (!type) {
126  return NULL;
127  }
129  type->identifier.is_const = is_const;
130  type->identifier.name = strdup(name);
131  type->identifier.kind = RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED;
132 
134  if (!tpair) {
136  return NULL;
137  }
138  tpair->btype = NULL;
139  tpair->type = type;
140  return tpair;
141 }

References ParserTypePair::btype, NULL, RZ_NEW0, rz_return_val_if_fail, rz_type_free(), RZ_TYPE_IDENTIFIER_KIND_UNSPECIFIED, RZ_TYPE_KIND_IDENTIFIER, strdup(), type, and ParserTypePair::type.

Referenced by parse_sole_type_name().

◆ c_parser_pointer_set_subtype()

bool c_parser_pointer_set_subtype ( CParserState state,
RZ_BORROW ParserTypePair tpair,
RZ_OWN ParserTypePair subpair 
)

Definition at line 881 of file types_storage.c.

881  {
882  rz_return_val_if_fail(state && tpair, false);
883  rz_return_val_if_fail(tpair->type->kind == RZ_TYPE_KIND_POINTER, false);
884  tpair->type->pointer.type = subpair->type;
885  tpair->btype = subpair->btype;
886  return true;
887 }
@ RZ_TYPE_KIND_POINTER
Definition: rz_type.h:129

References rz_return_val_if_fail, and RZ_TYPE_KIND_POINTER.

◆ c_parser_type_wrap_to_array()

RZ_OWN ParserTypePair* c_parser_type_wrap_to_array ( CParserState state,
ParserTypePair tpair,
size_t  size 
)

Definition at line 869 of file types_storage.c.

869  {
870  rz_return_val_if_fail(state && tpair, NULL);
872  type->kind = RZ_TYPE_KIND_ARRAY;
873  type->array.count = size;
874  type->array.type = tpair->type;
876  newtpair->btype = tpair->btype;
877  newtpair->type = type;
878  return newtpair;
879 }
voidpf void uLong size
Definition: ioapi.h:138

References ParserTypePair::btype, NULL, RZ_NEW0, rz_return_val_if_fail, RZ_TYPE_KIND_ARRAY, type, and ParserTypePair::type.

◆ c_parser_type_wrap_to_pointer()

RZ_OWN ParserTypePair* c_parser_type_wrap_to_pointer ( CParserState state,
ParserTypePair tpair,
bool  is_const 
)

Definition at line 857 of file types_storage.c.

857  {
858  rz_return_val_if_fail(state && tpair, NULL);
860  type->kind = RZ_TYPE_KIND_POINTER;
861  type->pointer.is_const = is_const;
862  type->pointer.type = tpair->type;
864  newtpair->btype = tpair->btype;
865  newtpair->type = type;
866  return newtpair;
867 }

References ParserTypePair::btype, NULL, RZ_NEW0, rz_return_val_if_fail, RZ_TYPE_KIND_POINTER, type, and ParserTypePair::type.