Initial git commit -- Transition from CodeGuard repository
[sdk] / compiler / libec / src / shortcuts.ec
1 #ifdef ECERE_STATIC
2 public import static "ecere"
3 #else
4 public import "ecere"
5 #endif
6
7 import "ecdefs"
8
9 bool parsingType;
10 public void SetParsingType(bool b) { parsingType = b; }
11
12 extern TypeName parsedType;
13 extern bool echoOn;
14 extern bool type_yydebug;
15 extern Location yylloc;
16
17 int type_yyparse();
18 void resetScannerPos(CodePosition pos);
19
20 Declarator PlugDeclarator(Declarator decl, Declarator baseDecl)
21 {
22    if(decl && decl.type != identifierDeclarator)
23    {
24       Declarator base;
25       decl = CopyDeclarator(decl);
26
27       base = decl;
28       if(base.type != identifierDeclarator)
29       {
30          for(; base.declarator && base.declarator.type != identifierDeclarator; base = base.declarator)
31          {
32             printf("");
33          }
34       }
35       if(baseDecl)
36       {
37          if(base.declarator) 
38             FreeDeclarator(base.declarator);
39          base.declarator = baseDecl;
40       }
41       else if(base.type == identifierDeclarator)
42       {
43          FreeDeclarator(decl);
44          decl = null;
45       }
46       return decl;
47    }
48    else
49       return baseDecl;
50 }
51
52
53 // *** Shortcut Functions ***
54 Declarator QMkPtrDecl(char * id)
55 {
56    Declarator declarator = id ? MkDeclaratorIdentifier(MkIdentifier(id)) : null;
57    return MkDeclaratorPointer(MkPointer(null,null), declarator);
58 }
59
60 TypeName QMkType(char * spec, Declarator decl)
61 {
62    OldList * specs = MkList();
63    ListAdd(specs, MkSpecifierName(spec));
64    return MkTypeName(specs, decl);
65 }
66
67 TypeName QMkClass(char * spec, Declarator decl)
68 {
69    OldList * specs = MkList();
70    ListAdd(specs, MkSpecifierName/*MkClassName*/(spec));
71    return MkTypeName(specs, decl);
72 }
73
74 Expression QBrackets(Expression exp)
75 {
76    OldList * expList = MkList();
77    ListAdd(expList, exp);
78    return MkExpBrackets(expList);
79 }
80
81 Expression QMkExpId(char * id)
82 {
83    return MkExpIdentifier(MkIdentifier(id));
84 }
85
86 Expression QMkExpCond(Expression cond, Expression exp, Expression elseExp)
87 {
88    OldList * expList = MkList();
89    ListAdd(expList, exp);
90    return MkExpCondition(cond, expList, elseExp);
91 }
92
93 Declaration QMkDeclaration(char * name, InitDeclarator initDecl)
94 {
95    OldList * specs = MkList(), * initDecls = null;
96    ListAdd(specs, MkSpecifierName(name));
97    if(initDecl)
98    {
99       initDecls = MkList();
100       ListAdd(initDecls, initDecl);
101    }
102    return MkDeclaration(specs, initDecls);
103 }
104
105 Declaration QMkDeclarationBase(int base, InitDeclarator initDecl)
106 {
107    OldList * specs = MkList(), * initDecls = null;
108    ListAdd(specs, MkSpecifier(base));
109    if(initDecl)
110    {
111       initDecls = MkList();
112       ListAdd(initDecls, initDecl);
113    }
114    return MkDeclaration(specs, initDecls);
115 }
116
117 char * QMkString(char * source)
118 {
119    char * string;
120    if(source)
121    {
122       int len = 0;
123       int i,j = 0;
124       char ch;
125
126       for(i = 0; (ch = source[i]); i++)
127       {
128          len++;
129          if(ch == '\"' || ch == '\\') 
130             len++;
131       }
132
133       string = new char[len+3];
134
135       string[j++] = '\"';
136       for(i = 0; (ch = source[i]); i++)
137       {
138          if(ch == '\"' || ch == '\\')
139             string[j++] = '\\';
140          string[j++] = ch;
141       }
142       string[j++] = '\"';
143       string[j] = '\0';
144    }
145    else
146       string = CopyString("0");
147    return string;
148 }
149
150 public Declarator GetFuncDecl(Declarator decl)
151 {
152    while(decl && decl.type != functionDeclarator && decl.type != identifierDeclarator)
153       decl = decl.declarator;
154    return (decl && decl.type == functionDeclarator) ? decl : null;
155 }
156
157 bool parseTypeError;
158
159 public Declarator SpecDeclFromString(char * string, OldList * specs, Declarator baseDecl)
160 {
161    Location oldLocation = yylloc;
162    Declarator decl = null;
163    File backFileInput = fileInput;
164
165    //char * classOp, * type;
166    if(!string)
167       string = "void()";
168
169    /*
170    classOp = strstr(string, "::");
171    type = classOp ? (classOp + 2) : string;
172    */
173
174    fileInput = TempFile { };
175    fileInput.Write(string, 1, strlen(string));
176    fileInput.Seek(0, start);
177
178    //type_yydebug = true;
179    echoOn = false;
180    parseTypeError = false;
181    parsedType = null;
182    declMode = 0;
183    resetScanner();
184    {
185       bool oldParsingType = parsingType;
186       parsingType = true;
187       type_yyparse();
188       parsingType = oldParsingType;
189    }
190    declMode = privateAccess;
191
192    type_yydebug = false;
193
194    delete fileInput;
195
196    if(parsedType)
197    {
198       if(parsedType.qualifiers)
199       {
200          Specifier spec;
201          for(;(spec = parsedType.qualifiers->first);)
202          {
203             parsedType.qualifiers->Remove(spec);
204             specs->Add(spec);
205          }
206       }
207       if(parsedType.bitCount)
208       {
209          parsedType.declarator = MkStructDeclarator(parsedType.declarator, parsedType.bitCount);
210          parsedType.bitCount = null;
211       }
212       decl = PlugDeclarator(parsedType.declarator, baseDecl);
213       FreeTypeName(parsedType);
214       parsedType = null;
215
216       if(parseTypeError)
217       {
218          Compiler_Warning("parsing type %s\n", string);
219          // Compiler_Error("parsing type %s\n", string);
220       }
221    }
222    else
223    {
224       Compiler_Warning("parsing type %s\n", string);
225       // Compiler_Error("parsing type %s\n", string);
226       //eSystem_Logf("parsing type %s\n", string);
227       //exit(0);
228       decl = baseDecl;
229    }
230
231    yylloc = oldLocation;
232
233    fileInput = backFileInput;
234    return decl;
235 }