gwenhywfar  5.14.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  int pack;
37 
38  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
39 
40  flags=Typemaker2_Type_GetFlags(ty);
41 
43 
44  pack=Typemaker2_Type_GetPack(ty);
45 
46  if (pack)
47  GWEN_Buffer_AppendArgs(tbuf, "#pragma pack(push, %d)\n", pack);
48 
49  GWEN_Buffer_AppendArgs(tbuf, "struct %s {\n", sTypeId);
50 
51  _addFlagspecificElements(ty, flags, tbuf);
52 
54  GWEN_Buffer_AppendString(tbuf, " GWEN_SIGNALOBJECT *_signalObject;\n");
55 
57  GWEN_Buffer_AppendString(tbuf, " int _refCount;\n");
58 
59  rv=_addMembers(ty, tbuf);
60  if (rv<0) {
61  DBG_INFO(NULL, "here (%d)", rv);
62  GWEN_Buffer_free(tbuf);
63  return rv;
64  }
65 
66  rv=_addVirtualFunctions(ty, tbuf);
67  if (rv<0) {
68  DBG_INFO(NULL, "here (%d)", rv);
69  GWEN_Buffer_free(tbuf);
70  return rv;
71  }
72 
73 
74  GWEN_Buffer_AppendString(tbuf, "};\n");
75 
76  if (pack)
77  GWEN_Buffer_AppendArgs(tbuf, "#pragma pack(pop)\n");
78 
79  /* add to declaration according to structAccess (defaults to "private") */
83  break;
86  break;
89  break;
92  break;
93  }
94  GWEN_Buffer_free(tbuf);
95 
96  return 0;
97 }
98 
99 
100 
101 void _addFlagspecificElements(TYPEMAKER2_TYPE *ty, uint32_t flags, GWEN_BUFFER *tbuf)
102 {
103  const char *sTypeId;
104 
105  sTypeId=Typemaker2_Type_GetIdentifier(ty);
106 
108  GWEN_Buffer_AppendArgs(tbuf, " GWEN_INHERIT_ELEMENT(%s)\n", sTypeId);
109 
111  GWEN_Buffer_AppendArgs(tbuf, " GWEN_LIST_ELEMENT(%s)\n", sTypeId);
112 
113  if (flags & TYPEMAKER2_TYPEFLAGS_WITH_TREE)
114  GWEN_Buffer_AppendArgs(tbuf, " GWEN_TREE_ELEMENT(%s)\n", sTypeId);
115 
117  GWEN_Buffer_AppendArgs(tbuf, " GWEN_TREE2_ELEMENT(%s)\n", sTypeId);
118 }
119 
120 
121 
123 {
124  TYPEMAKER2_MEMBER_LIST *tml;
125 
127  if (tml) {
128  TYPEMAKER2_MEMBER *tm;
129 
130  tm=Typemaker2_Member_List_First(tml);
131  while (tm) {
132  int rv;
133 
134  rv=_addMember(tm, tbuf);
135  if (rv<0) {
136  DBG_INFO(NULL, "here (%d)", rv);
137  return rv;
138  }
139 
140  tm=Typemaker2_Member_List_Next(tm);
141  }
142  }
143 
144  return 0;
145 }
146 
147 
148 
150 {
151  TYPEMAKER2_TYPE *mty;
152  int typeIsWritten=0;
153  const char *s;
154 
156  assert(mty);
157 
158  GWEN_Buffer_AppendString(tbuf, " ");
159 
162  const char *s;
163 
164  if (te==NULL) {
165  DBG_ERROR(0, "No enum name set in definition of member \"%s\"", Typemaker2_Member_GetName(tm));
166  return GWEN_ERROR_BAD_DATA;
167  }
169  if (s && *s) {
170  GWEN_Buffer_AppendArgs(tbuf, "%s ", s);
171  typeIsWritten=1;
172  }
173  }
174 
175  if (!typeIsWritten) {
178  GWEN_Buffer_AppendString(tbuf, "const ");
179 
181  if (!(s && *s)) {
182  const char *xx;
183 
185  DBG_ERROR(0, "Member [%s] has no identifier", xx?xx:"(unnamed)");
186  return GWEN_ERROR_BAD_DATA;
187  }
188  GWEN_Buffer_AppendString(tbuf, s);
189  GWEN_Buffer_AppendString(tbuf, " ");
190 
192  GWEN_Buffer_AppendString(tbuf, "*");
193  }
194 
196  GWEN_Buffer_AppendString(tbuf, s);
199 
200  GWEN_Buffer_AppendString(tbuf, ";");
202  GWEN_Buffer_AppendString(tbuf, " /* volatile */");
203  GWEN_Buffer_AppendString(tbuf, "\n");
204 
205  return 0;
206 }
207 
208 
209 
211 {
212  TYPEMAKER2_VIRTUALFN_LIST *fns;
213 
214  /* add virtual functions */
216  assert(fns);
217  if (Typemaker2_VirtualFn_List_GetCount(fns)) {
218  const char *s;
220 
221  vf=Typemaker2_VirtualFn_List_First(fns);
222  while (vf) {
223  GWEN_Buffer_AppendString(tbuf, " ");
225  GWEN_Buffer_AppendString(tbuf, s);
226  GWEN_Buffer_AppendString(tbuf, "_");
228  if (!(s && *s)) {
229  DBG_ERROR(0, "Virtual function has no name");
230  return GWEN_ERROR_BAD_DATA;
231  }
232  while (*s)
233  GWEN_Buffer_AppendByte(tbuf, toupper(*(s++)));
234  GWEN_Buffer_AppendString(tbuf, "_FN ");
235 
237  if (!(s && *s)) {
238  DBG_ERROR(0, "Virtual function has no name");
239  return GWEN_ERROR_BAD_DATA;
240  }
241  GWEN_Buffer_AppendByte(tbuf, tolower(*s));
242  GWEN_Buffer_AppendString(tbuf, s+1);
243  GWEN_Buffer_AppendString(tbuf, "Fn;\n");
244 
245  vf=Typemaker2_VirtualFn_List_Next(vf);
246  }
247  }
248 
249  return 0;
250 }
251 
252 
253 
254 
255 
int Typemaker2_Type_GetPack(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:289
#define TYPEMAKER2_TYPEFLAGS_WITH_TREE2
Definition: tm_type.h:71
#define DBG_ERROR(dbg_logger, format,...)
Definition: debug.h:97
TYPEMAKER2_VIRTUALFN_LIST * Typemaker2_Type_GetVirtualFns(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:737
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
int Typemaker2_Type_GetStructAccess(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:493
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:122
#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:210
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:585
void Typemaker2_Builder_AddProtectedDeclaration(TYPEMAKER2_BUILDER *tb, const char *s)
Definition: tm_builder.c:188
int GWEN_Buffer_AppendArgs(GWEN_BUFFER *bf, const char *fmt,...)
Definition: buffer.c:1087
#define TYPEMAKER2_TYPEFLAGS_WITH_TREE
Definition: tm_type.h:60
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:393
#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:683
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
static int _addMember(TYPEMAKER2_MEMBER *tm, GWEN_BUFFER *tbuf)
Definition: tm2c_struct.c:149
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:101
struct TYPEMAKER2_TYPE TYPEMAKER2_TYPE
Definition: tm_type.h:21
void Typemaker2_Builder_AddPublicDeclaration(TYPEMAKER2_BUILDER *tb, const char *s)
Definition: tm_builder.c:172
void Typemaker2_Builder_AddLibraryDeclaration(TYPEMAKER2_BUILDER *tb, const char *s)
Definition: tm_builder.c:180
#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 DBG_INFO(dbg_logger, format,...)
Definition: debug.h:181
#define TYPEMAKER2_TYPEFLAGS_WITH_INHERIT
Definition: tm_type.h:58
uint32_t Typemaker2_Type_GetFlags(const TYPEMAKER2_TYPE *ty)
Definition: tm_type.c:361
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:992
#define TYPEMAKER2_TYPEFLAGS_WITH_LIST1
Definition: tm_type.h:56