gwenhywfar  5.10.1
tm2c_struct.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Thu Jul 02 2009
3  copyright : (C) 2018 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 
15 #include "tm2c_struct.h"
16 
17 #include <gwenhywfar/debug.h>
18 
19 #include <ctype.h>
20 
21 
22 static void _addFlagspecificElements(TYPEMAKER2_TYPE *ty, uint32_t flags, GWEN_BUFFER *tbuf);
23 static int _addMembers(TYPEMAKER2_TYPE *ty, GWEN_BUFFER *tbuf);
24 static int _addMember(TYPEMAKER2_MEMBER *tm, GWEN_BUFFER *tbuf);
25 static int _addVirtualFunctions(TYPEMAKER2_TYPE *ty, GWEN_BUFFER *tbuf);
26 
27 
28 
29 
31 {
32  GWEN_BUFFER *tbuf;
33  uint32_t flags;
34  const char *sTypeId;
35  int rv;
36 
37  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
38 
39  flags=Typemaker2_Type_GetFlags(ty);
40 
42 
43  GWEN_Buffer_AppendArgs(tbuf, "struct %s {\n", sTypeId);
44 
45  _addFlagspecificElements(ty, flags, tbuf);
46 
48  GWEN_Buffer_AppendString(tbuf, " GWEN_SIGNALOBJECT *_signalObject;\n");
49 
51  GWEN_Buffer_AppendString(tbuf, " int _refCount;\n");
52 
53  rv=_addMembers(ty, tbuf);
54  if (rv<0) {
55  DBG_INFO(NULL, "here (%d)", rv);
56  GWEN_Buffer_free(tbuf);
57  return rv;
58  }
59 
60  rv=_addVirtualFunctions(ty, tbuf);
61  if (rv<0) {
62  DBG_INFO(NULL, "here (%d)", rv);
63  GWEN_Buffer_free(tbuf);
64  return rv;
65  }
66 
67 
68  GWEN_Buffer_AppendString(tbuf, "};\n");
69 
71  GWEN_Buffer_free(tbuf);
72 
73  return 0;
74 }
75 
76 
77 
78 void _addFlagspecificElements(TYPEMAKER2_TYPE *ty, uint32_t flags, GWEN_BUFFER *tbuf)
79 {
80  const char *sTypeId;
81 
83 
85  GWEN_Buffer_AppendArgs(tbuf, " GWEN_INHERIT_ELEMENT(%s)\n", sTypeId);
86 
88  GWEN_Buffer_AppendArgs(tbuf, " GWEN_LIST_ELEMENT(%s)\n", sTypeId);
89 
91  GWEN_Buffer_AppendArgs(tbuf, " GWEN_TREE_ELEMENT(%s)\n", sTypeId);
92 
94  GWEN_Buffer_AppendArgs(tbuf, " GWEN_TREE2_ELEMENT(%s)\n", sTypeId);
95 }
96 
97 
98 
100 {
101  TYPEMAKER2_MEMBER_LIST *tml;
102 
104  if (tml) {
105  TYPEMAKER2_MEMBER *tm;
106 
107  tm=Typemaker2_Member_List_First(tml);
108  while (tm) {
109  int rv;
110 
111  rv=_addMember(tm, tbuf);
112  if (rv<0) {
113  DBG_INFO(NULL, "here (%d)", rv);
114  return rv;
115  }
116 
117  tm=Typemaker2_Member_List_Next(tm);
118  }
119  }
120 
121  return 0;
122 }
123 
124 
125 
127 {
128  TYPEMAKER2_TYPE *mty;
129  int typeIsWritten=0;
130  const char *s;
131 
133  assert(mty);
134 
135  GWEN_Buffer_AppendString(tbuf, " ");
136 
139  const char *s;
140 
141  if (te==NULL) {
142  DBG_ERROR(0, "No enum name set in definition of member \"%s\"", Typemaker2_Member_GetName(tm));
143  return GWEN_ERROR_BAD_DATA;
144  }
146  if (s && *s) {
147  GWEN_Buffer_AppendArgs(tbuf, "%s ", s);
148  typeIsWritten=1;
149  }
150  }
151 
152  if (!typeIsWritten) {
155  GWEN_Buffer_AppendString(tbuf, "const ");
156 
158  if (!(s && *s)) {
159  const char *xx;
160 
162  DBG_ERROR(0, "Member [%s] has no identifier", xx?xx:"(unnamed)");
163  return GWEN_ERROR_BAD_DATA;
164  }
165  GWEN_Buffer_AppendString(tbuf, s);
166  GWEN_Buffer_AppendString(tbuf, " ");
167 
169  GWEN_Buffer_AppendString(tbuf, "*");
170  }
171 
173  GWEN_Buffer_AppendString(tbuf, s);
176 
177  GWEN_Buffer_AppendString(tbuf, ";");
179  GWEN_Buffer_AppendString(tbuf, " /* volatile */");
180  GWEN_Buffer_AppendString(tbuf, "\n");
181 
182  return 0;
183 }
184 
185 
186 
188 {
189  TYPEMAKER2_VIRTUALFN_LIST *fns;
190 
191  /* add virtual functions */
193  assert(fns);
194  if (Typemaker2_VirtualFn_List_GetCount(fns)) {
195  const char *s;
197 
198  vf=Typemaker2_VirtualFn_List_First(fns);
199  while (vf) {
200  GWEN_Buffer_AppendString(tbuf, " ");
202  GWEN_Buffer_AppendString(tbuf, s);
203  GWEN_Buffer_AppendString(tbuf, "_");
205  if (!(s && *s)) {
206  DBG_ERROR(0, "Virtual function has no name");
207  return GWEN_ERROR_BAD_DATA;
208  }
209  while (*s)
210  GWEN_Buffer_AppendByte(tbuf, toupper(*(s++)));
211  GWEN_Buffer_AppendString(tbuf, "_FN ");
212 
214  if (!(s && *s)) {
215  DBG_ERROR(0, "Virtual function has no name");
216  return GWEN_ERROR_BAD_DATA;
217  }
218  GWEN_Buffer_AppendByte(tbuf, tolower(*s));
219  GWEN_Buffer_AppendString(tbuf, s+1);
220  GWEN_Buffer_AppendString(tbuf, "Fn;\n");
221 
222  vf=Typemaker2_VirtualFn_List_Next(vf);
223  }
224  }
225 
226  return 0;
227 }
228 
229 
230 
231 
232 
#define TYPEMAKER2_TYPEFLAGS_WITH_TREE2
Definition: tm_type.h:71
TYPEMAKER2_VIRTUALFN_LIST * Typemaker2_Type_GetVirtualFns(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:695
TYPEMAKER2_TYPE * Typemaker2_Member_GetTypePtr(const TYPEMAKER2_MEMBER *tm)
Definition: tm_member.c:467
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
uint32_t Typemaker2_Member_GetFlags(const TYPEMAKER2_MEMBER *tm)
Definition: tm_member.c:237
TYPEMAKER2_ENUM * Typemaker2_Member_GetEnumPtr(const TYPEMAKER2_MEMBER *tm)
Definition: tm_member.c:485
#define NULL
Definition: binreloc.c:300
static int _addMembers(TYPEMAKER2_TYPE *ty, GWEN_BUFFER *tbuf)
Definition: tm2c_struct.c:99
#define TYPEMAKER2_TYPEFLAGS_WITH_SLOTS
Definition: tm_type.h:63
struct TYPEMAKER2_ENUM TYPEMAKER2_ENUM
Definition: tm_enum.h:24
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
int Typemaker2_Member_GetMaxLen(const TYPEMAKER2_MEMBER *tm)
Definition: tm_member.c:369
void Typemaker2_Builder_AddPrivateDeclaration(TYPEMAKER2_BUILDER *tb, const char *s)
Definition: tm_builder.c:196
static int _addVirtualFunctions(TYPEMAKER2_TYPE *ty, GWEN_BUFFER *tbuf)
Definition: tm2c_struct.c:187
struct TYPEMAKER2_VIRTUALFN TYPEMAKER2_VIRTUALFN
Definition: tm_virtualfn.h:21
#define GWEN_ERROR_BAD_DATA
Definition: error.h:121
#define TYPEMAKER2_TYPEFLAGS_WITH_SIGNALS
Definition: tm_type.h:62
const char * Typemaker2_Type_GetIdentifier(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:543
int GWEN_Buffer_AppendArgs(GWEN_BUFFER *bf, const char *fmt,...)
Definition: buffer.c:1084
#define TYPEMAKER2_TYPEFLAGS_WITH_TREE
Definition: tm_type.h:60
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:394
#define TYPEMAKER2_FLAGS_CONST
Definition: tm_type.h:33
struct TYPEMAKER2_BUILDER TYPEMAKER2_BUILDER
Definition: tm_builder.h:26
#define TYPEMAKER2_FLAGS_ENUM
Definition: tm_type.h:38
TYPEMAKER2_MEMBER_LIST * Typemaker2_Type_GetMembers(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:641
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
static int _addMember(TYPEMAKER2_MEMBER *tm, GWEN_BUFFER *tbuf)
Definition: tm2c_struct.c:126
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
static void _addFlagspecificElements(TYPEMAKER2_TYPE *ty, uint32_t flags, GWEN_BUFFER *tbuf)
Definition: tm2c_struct.c:78
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
struct TYPEMAKER2_TYPE TYPEMAKER2_TYPE
Definition: tm_type.h:21
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
#define TYPEMAKER2_TYPEFLAGS_WITH_REFCOUNT
Definition: tm_type.h:61
int TM2C_BuildStruct(TYPEMAKER2_BUILDER *tb, TYPEMAKER2_TYPE *ty)
Definition: tm2c_struct.c:30
int Typemaker2_Type_GetType(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:171
struct TYPEMAKER2_MEMBER TYPEMAKER2_MEMBER
Definition: tm_member.h:21
#define TYPEMAKER2_TYPEFLAGS_WITH_INHERIT
Definition: tm_type.h:58
uint32_t Typemaker2_Type_GetFlags(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:341
const char * Typemaker2_Member_GetName(const TYPEMAKER2_MEMBER *tm)
Definition: tm_member.c:83
const char * Typemaker2_Enum_GetType(const TYPEMAKER2_ENUM *p_struct)
Definition: tm_enum.c:192
#define TYPEMAKER2_FLAGS_VOLATILE
Definition: tm_type.h:32
const char * Typemaker2_VirtualFn_GetName(const TYPEMAKER2_VIRTUALFN *vf)
Definition: tm_virtualfn.c:170
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989
#define TYPEMAKER2_TYPEFLAGS_WITH_LIST1
Definition: tm_type.h:56