:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / rpcrt4 / midl / types.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "midl.h"
6
7 struct _type;
8
9 typedef struct _struct_member
10 {
11    char* name;
12    unsigned int value;
13    struct _struct_member* next;
14 } struct_member;
15
16 typedef struct
17 {
18    struct_member* member_list;
19    char* tag;
20    struct _type* type_value;
21 } struct_type;
22
23 typedef struct _type
24 {
25    char* name;
26    unsigned int value;
27    unsigned int default_sign;
28    struct_type* struct_desc;         
29 } type;
30
31 static struct_type struct_types[255];
32
33 static int next_struct_slot_free = 0;
34 static struct_type* current_struct;
35
36 static type types[255] = {
37      {NULL, 0, 0},
38      {"boolean", BOOLEAN_TYPE, UNSIGNED_TYPE_OPTION},
39      {"byte", BYTE_TYPE, 0},
40      {"char", CHAR_TYPE, UNSIGNED_TYPE_OPTION},
41      {"double", DOUBLE_TYPE, 0},
42      {"enum", ENUM_TYPE, 0},
43      {"error_status_t", ERROR_STATUS_TYPE, UNSIGNED_TYPE_OPTION},
44      {"float", FLOAT_TYPE, 0},
45      {"handle_t", HANDLE_TYPE, 0},
46      {"hyper", HYPER_TYPE, SIGNED_TYPE_OPTION},
47      {"int", INT_TYPE, SIGNED_TYPE_OPTION},
48      {"__int32", INT32_TYPE, SIGNED_TYPE_OPTION},
49      {"__int3264", INT32OR64_TYPE, SIGNED_TYPE_OPTION},
50      {"__int64", INT64_TYPE, SIGNED_TYPE_OPTION},
51      {"long", LONG_TYPE, SIGNED_TYPE_OPTION},
52      {"short", SHORT_TYPE, SIGNED_TYPE_OPTION},
53      {"small", SMALL_TYPE, SIGNED_TYPE_OPTION},
54      {"void", VOID_TYPE, 0},
55      {"wchar_t", WCHAR_TYPE, UNSIGNED_TYPE_OPTION},
56      {NULL, 0, 0}
57      };
58
59 static int next_free_slot = 18;
60
61 unsigned int struct_to_type(char* tag)
62 {
63    int i;
64    
65    for (i = 0; i < next_struct_slot_free; i++)
66      {
67         if (strcmp(tag, struct_types[i].tag) == 0)
68           {
69              return(struct_types[i].type_value->value);
70           }
71      }
72    return(0);
73 }
74
75 void start_struct(char* tag)
76 {
77    char* name;
78    
79    if (tag == NULL)
80      {
81         tag = malloc(255);
82         sprintf(tag, "__unnamed_struct%d", next_struct_slot_free);
83      }
84    
85    name = malloc(strlen("struct ") + strlen(tag) + 1);
86    strcpy(name, "struct ");
87    strcat(name, tag);
88       
89    struct_types[next_struct_slot_free].tag = strdup(tag);
90    struct_types[next_struct_slot_free].member_list = NULL;
91    current_struct = &struct_types[next_struct_slot_free];
92    
93    types[next_free_slot].name = name;
94    types[next_free_slot].value = next_free_slot << 8;
95    types[next_free_slot].default_sign = 0;
96    types[next_free_slot].struct_desc = 
97      &struct_types[next_struct_slot_free];
98    
99    struct_types[next_struct_slot_free].type_value = &types[next_free_slot];
100    
101    next_struct_slot_free++;
102    next_free_slot++;
103 }
104
105 void add_struct_member(char* name, unsigned int type)
106 {
107    struct_member* member;
108    
109    member = malloc(sizeof(struct_member));
110    
111    member->name = strdup(name);
112    member->value = type;
113    member->next = current_struct->member_list;
114    current_struct->member_list = member;
115 }
116
117 unsigned int end_struct(void)
118 {
119    int n;
120    struct_member* cur;
121    
122    printf("Defining struct %s {\n", current_struct->tag);
123    
124    cur = current_struct->member_list;
125    while (cur != NULL)
126      {
127         print_type(cur->value);
128         printf(" %s\n", cur->name);
129         
130         cur = cur->next;
131      }
132    printf("}\n");
133    
134    n = current_struct->type_value->value;
135    current_struct = NULL;
136    return(n);
137 }
138
139 void add_typedef(char* name, int type)
140 {
141    printf("Adding typedef %s to ", name);
142    print_type(type);
143    printf("\n");
144    
145    types[next_free_slot].name = strdup(name);
146    types[next_free_slot].value = type;
147    types[next_free_slot].default_sign = 0;
148    next_free_slot++;
149 }
150
151 void print_type(int tval)
152 {
153    int i;
154    
155    for (i = 1; i < next_free_slot; i++)
156      {
157         if ((tval & BASE_TYPE_MASK) == types[i].value)
158           {
159              if (tval & UNSIGNED_TYPE_OPTION)
160                {
161                   printf("unsigned ");
162                }
163              if (tval & SIGNED_TYPE_OPTION)
164                {
165                   printf("signed ");
166                }
167              printf("%s", types[i].name);
168              if (tval & POINTER_TYPE_OPTION)
169                {
170                   printf("*");
171                }
172              return;
173           }
174      }
175    printf("unknown type");
176 }
177
178 int token_to_type(char* token)
179 {
180    int i;
181    
182    for (i = 1; i < next_free_slot; i++)
183      {
184         if (strcmp(types[i].name, token) == 0)
185           {
186              return(types[i].value);
187           }
188      }
189    return(0);
190 }