d183a5c46d8c49d540d85c43c7fb38c3e0a63de1
[sdk] / compiler / libec / src / ecdefs.ec
1 #ifdef ECERE_STATIC
2 public import static "ecere"
3 #else
4 public import "ecere"
5 #endif
6
7 import "ast"
8 import "freeAst"
9 import "firstPass"
10 import "pass0"
11 import "pass1"
12 import "pass15"
13 import "pass16"
14 import "pass2"
15 import "pass3"
16 import "loadSymbols"
17 import "copy"
18 import "shortcuts"
19 import "output"
20 import "dbpass"
21
22 #include <stdio.h>
23
24 #define yylloc _yylloc
25 #include "grammar.h"
26 #undef yylloc
27
28 public enum TokenType
29 {
30   identifier = IDENTIFIER,
31   constant = CONSTANT,
32   stringLiteral = STRING_LITERAL,
33   sizeOf = SIZEOF,
34   ptrOp = PTR_OP,
35   incOp = INC_OP,
36   decOp = DEC_OP,
37   leftOp = LEFT_OP,
38   rightOp = RIGHT_OP,
39   leOp = LE_OP,
40   geOp = GE_OP,
41   eqOp = EQ_OP,
42   neOp = NE_OP,
43   andOp = AND_OP,
44   orOp = OR_OP,
45   mulAssign = MUL_ASSIGN,
46   divAssign = DIV_ASSIGN,
47   modAssign = MOD_ASSIGN,
48   addAssign = ADD_ASSIGN,
49   subAssign = SUB_ASSIGN,
50   leftAssign = LEFT_ASSIGN,
51   rightAssign = RIGHT_ASSIGN,
52   andAssign = AND_ASSIGN,
53   xorAssign = XOR_ASSIGN,
54   orAssign = OR_ASSIGN,
55   typeName = TYPE_NAME,
56   _typedef = TYPEDEF,
57   _extern = EXTERN,
58   _static = STATIC,
59   _auto = AUTO,
60   _register = REGISTER,
61   _char = CHAR,
62   _short = SHORT,
63   _int = INT,
64   _uint = UINT,
65   _int64 = INT64,
66   _long = LONG,
67   _signed = SIGNED,
68   _unsigned = UNSIGNED,
69   _float = FLOAT,
70   _double = DOUBLE,
71   _const = CONST,
72   _volatile = VOLATILE,
73   _void = VOID,
74   _valist = VALIST,
75   _struct = STRUCT,
76   _union = UNION,
77   _enum = ENUM,
78   ellipsis = ELLIPSIS,
79   _case = CASE,
80   _default = DEFAULT,
81   _if = IF,
82   _switch = SWITCH,
83   _whilte = WHILE,
84   _do = DO,
85   _for = FOR,
86   _goto = GOTO,
87   _continue = CONTINUE,
88   _break = BREAK,
89   _return = RETURN,
90   ifx = IFX,
91   _else = ELSE,
92   _class = CLASS,
93   thisClass = THISCLASS,
94   className = CLASS_NAME,
95   _property = PROPERTY,
96   setProp = SETPROP,
97   getProp = GETPROP,
98   newOp = NEWOP,
99   _renew = RENEW,
100   _delete = DELETE,
101   _extDecl = EXT_DECL,
102   _extStorage = EXT_STORAGE,
103   _import = IMPORT,
104   _define = DEFINE,
105   _virtual = VIRTUAL,
106   attrib = ATTRIB,
107   _public = PUBLIC,
108   _priate = PRIVATE,
109   typedObject = TYPED_OBJECT,
110   anyObject = ANY_OBJECT,
111   _incref = _INCREF,
112   extension = EXTENSION,
113   ___asm = ASM,
114   _typeof = TYPEOF,
115   _watch = WATCH,
116   stopWatching = STOPWATCHING,
117   fireWatchers = FIREWATCHERS,
118   _watchable = WATCHABLE,
119   classDesigner = CLASS_DESIGNER,
120   classNoExpansion = CLASS_NO_EXPANSION,
121   classFixed = CLASS_FIXED,
122   isPropSet = ISPROPSET,
123   classDefaultProperty = CLASS_DEFAULT_PROPERTY,
124   propertyCategory = PROPERTY_CATEGORY,
125   classData = CLASS_DATA,
126   classProperty = CLASS_PROPERTY,
127   subClass = SUBCLASS,
128   nameSpace = NAMESPACE,
129   new0Op = NEW0OP,
130   renew0Op = RENEW0,
131   vaArg = VAARG,
132   dbTable = DBTABLE,
133   dbField = DBFIELD,
134   dbIndex = DBINDEX,
135   databaseOpen = DATABASE_OPEN,
136   alignOf = ALIGNOF,
137   attribDep = ATTRIB_DEP,
138   _attrib = __ATTRIB,
139   BOOL = BOOL,
140   _BOOL = _BOOL,
141   complex = _COMPLEX,
142   imaginary = _IMAGINARY,
143   _restrict = RESTRICT,
144   _thread = THREAD
145 };
146
147 enum Order { ascending, descending };
148
149 public class DBTableDef : struct
150 {
151 public:
152    char * name;
153    Symbol symbol;
154    OldList * definitions;
155    AccessMode declMode;
156 }
157
158 enum DBTableEntryType { fieldEntry, indexEntry };
159
160 class DBTableEntry : struct
161 {
162    DBTableEntry prev, next;
163    DBTableEntryType type;
164    Identifier id;
165    union
166    {
167       struct
168       {
169          TypeName dataType;
170          char * name;
171       };
172       OldList * items;
173    };
174 };
175
176 class DBIndexItem : struct
177 {
178    DBIndexItem prev, next;
179    Identifier id;
180    Order order;
181 };
182
183 bool inCompiler = false;
184 public void SetInCompiler(bool b) { inCompiler = b; }
185
186 Context curContext;
187 Context globalContext;
188 OldList * excludedSymbols;
189
190 Context topContext;
191 OldList * imports;
192 OldList * defines;
193 Module privateModule;
194 public void SetPrivateModule(Module module) { privateModule = module; }public Module GetPrivateModule() { return privateModule; }
195 ModuleImport mainModule;
196 public void SetMainModule(ModuleImport moduleImport) { mainModule = moduleImport; } public ModuleImport GetMainModule() { return mainModule; }
197 File fileInput;
198 public void SetFileInput(File file) { fileInput = file; }
199 char * symbolsDir = null;
200 public void SetSymbolsDir(char * s) {
201    delete symbolsDir;
202    symbolsDir = CopyString(s);
203 } public char * GetSymbolsDir() { return symbolsDir ? symbolsDir : ""; }
204 char * outputFile;
205 public void SetOutputFile(char * s) { outputFile = s; } public char * GetOutputFile() { return outputFile; }
206 char * sourceFile;
207 public void SetSourceFile(char * s) { sourceFile = s; } public char * GetSourceFile() { return sourceFile; }
208 char * i18nModuleName;
209 public void SetI18nModuleName(char * s) { i18nModuleName = s; } public char * GetI18nModuleName() { return i18nModuleName; }
210
211 public void SetGlobalContext(Context context) { globalContext = context; } public Context GetGlobalContext() { return globalContext; }
212 public void SetTopContext(Context context) { topContext = context; } public Context GetTopContext() { return topContext; }
213 public void SetCurrentContext(Context context) { curContext = context; } public Context GetCurrentContext() { return curContext; }
214 public void SetExcludedSymbols(OldList * list) { excludedSymbols = list; }
215 public void SetImports(OldList * list) { imports = list; }
216 public void SetDefines(OldList * list) { defines = list; }
217
218 bool outputLineNumbers = true;
219 public void SetOutputLineNumbers(bool value) { outputLineNumbers = value; }
220
221 public void FixModuleName(char *moduleName)
222 {
223    ChangeCh(moduleName, '.', '_');
224    ChangeCh(moduleName, ' ', '_');
225    ChangeCh(moduleName, '-', '_');
226    ChangeCh(moduleName, '&', '_');
227 }
228
229 // todo support %var% variables for windows and $var for linux?
230 public char * PassArg(char * output, const char * input)
231 {
232 #ifdef __WIN32__
233 //define windowsFileNameCharsNeedEscaping = " !%&'()+,;=[]^`{}~"; // "#$-.@_" are ok
234    const char * escChars = " !\"%&'()+,;=[]^`{}~"; // windowsFileNameCharsNeedEscaping;
235    const char * escCharsQuoted = "\"";
236 #else
237 //define linuxFileNameCharsNeedEscaping = " !\"$&'()*:;<=>?[\\`{|"; // "#%+,-.@]^_}~" are ok
238    const char * escChars = " !\"$&'()*:;<=>?[\\`{|"; // linuxFileNameCharsNeedEscaping;
239    const char * escCharsQuoted = "\"()$";
240 #endif
241    bool quoting = false;
242    char *o = output, *i = input, *l = input;
243 #ifdef __WIN32__
244    while(*l && !strchr(escChars, *l)) l++;
245    if(*l) quoting = true;
246 #else
247    if(*i == '-')
248    {
249       l++;
250       while(*l && !strchr(escChars, *l)) l++;
251       if(*l) quoting = true;
252       *o++ = *i++;
253    }
254 #endif
255    if(quoting)
256       *o++ = '\"';
257    while(*i)
258    {
259       if(strchr(quoting ? escCharsQuoted : escChars, *i))
260          *o++ = '\\';
261       *o++ = *i++;
262    }
263    if(quoting)
264       *o++ = '\"';
265    *o = '\0';
266    return o;
267 }
268 /*public Module GetPrivateModule()
269 {
270    return privateModule;
271 }*/
272
273 public class GlobalData : BTNode
274 {
275 public:
276    Module module;
277    char * dataTypeString;
278    Type dataType;
279    void * symbol;
280    char * fullName;
281 };
282
283 public class TemplatedType : BTNode
284 {
285 public:
286    TemplateParameter param;
287 };
288
289 class DataRedefinition : struct
290 {
291    DataRedefinition prev, next;
292    char name[1024];
293    char type1[1024], type2[1024];
294 };
295
296 public struct CodePosition
297 {
298 public:
299    int line, charPos, pos;
300    int included;
301
302    void AdjustDelete(BufferLocation start, BufferLocation end)
303    {
304       // Location is before, nothing to do
305       if(line - 1 < start.y || (line - 1 == start.y && charPos - 1 < start.x))
306          return;
307       // Location is inside deleted bytes, point to the start
308       if((line - 1 >= start.y && (line - 1 > start.y || charPos - 1 >= start.x)) &&
309          (line - 1 >= end.y && (line - 1 > end.y || charPos - 1 >= end.x)))
310       {
311          // Location is after
312          if(line - 1 >= end.y)
313          {
314             // Location is on another line
315             if(line - 1 > end.y)
316                line -= end.y - start.y;
317             // Location is the last touched line
318             else
319             {
320                if(charPos - 1 >= end.x)
321                {
322                   line = start.y + 1;
323                   //if(start.line == end.line)
324                      charPos -= end.x - start.x;
325                }
326             }
327          }
328       }
329       else
330       {
331          line = start.y + 1;
332          charPos = start.x + 1;
333       }
334    }
335
336    // Assuming no carriage return before first character ???? fixed?
337    void AdjustAdd(BufferLocation start, BufferLocation end)
338    {
339       int numLines = end.y - start.y;
340       if(line - 1 >= start.y)
341       {
342          if(line - 1 > start.y)
343             line += numLines;
344          else
345          {
346             if(charPos - 1 > start.x || (charPos - 1 == start.x /*&& (numLines ? true : false)*/))
347             {
348                line += numLines;
349                //charPos - 1 += numLines ? end.x : (end.x - start.x);
350                charPos += end.x - start.x;
351             }
352          }
353       }
354    }
355 };
356
357 public struct Location
358 {
359 public:
360    CodePosition start, end;
361
362    bool Inside(int line, int charPos)
363    {
364       return (start.line < line || (start.line == line && start.charPos <= charPos)) &&
365              (end.line > line || (end.line == line && end.charPos >= charPos));
366    }
367 };
368
369 public enum DefinitionType { moduleDefinition, classDefinition, defineDefinition, functionDefinition, dataDefinition };
370
371 public class Definition : struct
372 {
373 public:
374    Definition prev, next;
375    char * name;
376    DefinitionType type;
377 };
378
379 public class ImportedModule : struct
380 {
381 public:
382    ImportedModule prev, next;
383    char * name;
384    DefinitionType type;
385    ImportType importType;
386    bool globalInstance;
387    bool dllOnly;
388    AccessMode importAccess;
389 };
390
391 public class Identifier : struct
392 {
393 public:
394    Identifier prev, next;
395    Location loc;
396    // TODO: NameSpace * nameSpace;
397    Symbol classSym;
398    Specifier _class;
399    char * string;
400    Identifier badID;
401 };
402
403 public enum ExpressionType
404 {
405    identifierExp, instanceExp, constantExp, stringExp, opExp,
406    bracketsExp, indexExp, callExp, memberExp, pointerExp, typeSizeExp,
407    castExp, conditionExp, newExp, renewExp, classSizeExp,
408    dummyExp, dereferenceErrorExp, symbolErrorExp,
409    memberSymbolErrorExp, memoryErrorExp, unknownErrorExp,
410    noDebuggerErrorExp,
411    extensionCompoundExp, classExp, classDataExp, new0Exp, renew0Exp,
412    dbopenExp, dbfieldExp, dbtableExp, dbindexExp, extensionExpressionExp, extensionInitializerExp,
413    vaArgExp, arrayExp, typeAlignExp,
414    memberPropertyErrorExp, functionCallErrorExp, divideBy0ErrorExp
415 };
416
417 public enum MemberType
418 {
419    unresolvedMember, propertyMember, methodMember, dataMember, reverseConversionMember, classPropertyMember
420 };
421
422 public class ExpUsage
423 {
424 public:
425    bool usageGet:1, usageSet:1, usageArg:1, usageCall:1, usageMember:1, usageDeepGet:1, usageRef:1, usageDelete:1;
426 };
427
428 public class TemplateParameter : struct
429 {
430 public:
431    TemplateParameter prev, next;
432    Location loc;
433
434    TemplateParameterType type;
435    Identifier identifier;
436    union
437    {
438       TemplateDatatype dataType; // For both base datatype (type) and data type (expression)
439       TemplateMemberType memberType;   // For identifier
440    };
441    TemplateArgument defaultArgument;
442
443    // For type parameters
444    char * dataTypeString;
445    Type baseType;
446 }
447
448 public class TemplateDatatype : struct
449 {
450 public:
451    OldList * specifiers;
452    Declarator decl;
453 }
454
455 public class TemplateArgument : struct
456 {
457 public:
458    TemplateArgument prev, next;
459    Location loc;
460
461    Identifier name;
462    TemplateParameterType type;
463    union
464    {
465       Expression expression;
466       Identifier identifier;
467       TemplateDatatype templateDatatype;
468    };
469 }
470
471 public enum SpecifierType
472 {
473    baseSpecifier, nameSpecifier, enumSpecifier, structSpecifier, unionSpecifier, /*classSpecifier,*/
474    extendedSpecifier, typeOfSpecifier, subClassSpecifier, templateTypeSpecifier
475 };
476
477 public class Specifier : struct
478 {
479 public:
480    Specifier prev, next;
481    Location loc;
482    SpecifierType type;
483    union
484    {
485       int specifier;
486       struct
487       {
488          ExtDecl extDecl;
489          char * name;
490          Symbol symbol;
491          OldList * templateArgs;
492       };
493       struct
494       {
495          Identifier id;
496          OldList * list;
497          OldList * baseSpecs;
498          OldList/*<ClassDef>*/ * definitions;
499          bool addNameSpace;
500          Context ctx;
501          ExtDecl extDeclStruct;
502       };
503       Expression expression;
504       Specifier _class;
505       TemplateParameter templateParameter;
506    };
507 };
508
509 public class Attribute : struct
510 {
511 public:
512    Attribute prev, next;
513    Location loc;
514    String attr;
515    Expression exp;
516 }
517
518 public class Attrib : struct
519 {
520 public:
521    Location loc;
522    int type;
523    OldList * attribs;
524 }
525
526 public class ExtDecl : struct
527 {
528 public:
529    Location loc;
530    ExtDeclType type;
531    union
532    {
533       String s;
534       Attrib attr;
535    };
536 }
537
538 public enum ExtDeclType
539 {
540    extDeclString, extDeclAttrib
541 };
542
543 public class Expression : struct
544 {
545 public:
546    Expression prev, next;
547    Location loc;
548    ExpressionType type;
549    union
550    {
551       struct
552       {
553          char * constant;
554          Identifier identifier;
555       };
556       Statement compound;
557       Instantiation instance;
558       struct
559       {
560          char * string;
561          bool intlString;
562       };
563       OldList * list;
564       struct
565       {
566          OldList * specifiers;
567          Declarator decl;
568       } _classExp;
569       struct
570       {
571          Identifier id;
572       } classData;
573       struct
574       {
575          Expression exp;
576          OldList * arguments;
577          Location argLoc;
578       } call;
579       struct
580       {
581          Expression exp;
582          OldList * index;
583       } index;
584       struct
585       {
586          Expression exp;
587          Identifier member;
588
589          MemberType memberType;
590          bool thisPtr;
591       } member;
592       struct
593       {
594          int op;
595          Expression exp1, exp2;
596       } op;
597       TypeName typeName;
598       Specifier _class;
599       struct
600       {
601          TypeName typeName;
602          Expression exp;
603       } cast;
604       struct
605       {
606          Expression cond;
607          OldList * exp;
608          Expression elseExp;
609       } cond;
610       struct
611       {
612          TypeName typeName;
613          Expression size;
614       } _new;
615       struct
616       {
617          TypeName typeName;
618          Expression size;
619          Expression exp;
620       } _renew;
621       struct
622       {
623          char * table;
624          Identifier id;
625       } db;
626       struct
627       {
628          Expression ds;
629          Expression name;
630       } dbopen;
631       struct
632       {
633          TypeName typeName;
634          Initializer initializer;
635       } initializer;
636       struct
637       {
638          Expression exp;
639          TypeName typeName;
640       } vaArg;
641    };
642
643    bool debugValue;
644
645    DataValue val;
646
647    uint64 address;
648    bool hasAddress;
649
650    // *** COMPILING DATA ***
651    Type expType;
652    Type destType;
653
654    ExpUsage usage;
655    int tempCount;
656    bool byReference;
657    bool isConstant;
658    bool addedThis;
659    bool needCast;
660    bool thisPtr;
661    bool opDestType;
662
663    void Clear()
664    {
665       debugValue = false;
666       val = { 0 };
667       address = 0;
668       hasAddress = false;
669
670       expType = null;
671       destType = null;
672
673       usage = 0;
674       tempCount = 0;
675       byReference = false;
676       isConstant = false;
677       addedThis = false;
678       needCast = false;
679       thisPtr = false;
680       opDestType = false;
681    }
682 };
683
684 public class Enumerator : struct
685 {
686 public:
687    Enumerator prev, next;
688    Location loc;
689    Identifier id;
690    Expression exp;
691 };
692
693 class Pointer : struct
694 {
695    Pointer prev, next;
696    Location loc;
697    OldList * qualifiers;
698    Pointer pointer;
699 };
700
701 public enum DeclaratorType
702 {
703    structDeclarator, identifierDeclarator, bracketsDeclarator, arrayDeclarator,
704    functionDeclarator, pointerDeclarator, extendedDeclarator, extendedDeclaratorEnd
705 };
706
707 public class Declarator : struct
708 {
709 public:
710    Declarator prev, next;
711    Location loc;
712    DeclaratorType type;
713    Symbol symbol;//, propSymbol;
714    Declarator declarator;
715    union
716    {
717       Identifier identifier;
718       struct
719       {
720          Expression exp;
721          Expression posExp;
722          Attrib attrib;
723       } structDecl;
724       struct
725       {
726          Expression exp;
727          Specifier enumClass;
728       } array;
729       struct
730       {
731          OldList * parameters;
732       } function;
733       struct
734       {
735          Pointer pointer;
736       } pointer;
737       struct
738       {
739          ExtDecl extended;
740       } extended;
741    };
742 };
743
744 public enum InitializerType { expInitializer, listInitializer };
745
746 public class Initializer : struct
747 {
748 public:
749    Initializer prev, next;
750    Location loc;
751    InitializerType type;
752    union
753    {
754       Expression exp;
755       OldList * list;
756    };
757    bool isConstant;
758    Identifier id;
759 };
760
761 public class InitDeclarator : struct
762 {
763 public:
764    InitDeclarator prev, next;
765    Location loc;
766    Declarator declarator;
767    Initializer initializer;
768 };
769
770 public enum ClassObjectType
771 {
772    none,
773    classPointer,
774    typedObject,
775    anyObject
776 };
777
778 public class TypeName : struct
779 {
780 public:
781    TypeName prev, next;
782    Location loc;
783    OldList * qualifiers;
784    Declarator declarator;
785    //bool /*typedObject, */byReference;
786    //bool anyObject;
787    ClassObjectType classObjectType;
788    Expression bitCount;
789 };
790
791 class AsmField : struct
792 {
793    AsmField prev, next;
794    Location loc;
795    char * command;
796    Expression expression;
797    Identifier symbolic;
798 };
799
800 public enum StmtType { labeledStmt, caseStmt, compoundStmt,
801                expressionStmt, ifStmt, switchStmt, whileStmt, doWhileStmt,
802                forStmt, gotoStmt, continueStmt, breakStmt, returnStmt, asmStmt, badDeclarationStmt,
803                fireWatchersStmt, stopWatchingStmt, watchStmt, forEachStmt
804              };
805
806 public class Statement : struct
807 {
808 public:
809    Statement prev, next;
810    Location loc;
811    StmtType type;
812    union
813    {
814       OldList * expressions;
815       struct
816       {
817          Identifier id;
818          Statement stmt;
819       } labeled;
820       struct
821       {
822          Expression exp;
823          Statement stmt;
824       } caseStmt;
825       struct
826       {
827          OldList * declarations;
828          OldList * statements;
829          Context context;
830          bool isSwitch;
831       } compound;
832       struct
833       {
834          OldList * exp;
835          Statement stmt;
836          Statement elseStmt;
837       } ifStmt;
838       struct
839       {
840          OldList * exp;
841          Statement stmt;
842       } switchStmt;
843       struct
844       {
845          OldList * exp;
846          Statement stmt;
847       } whileStmt;
848       struct
849       {
850          OldList * exp;
851          Statement stmt;
852       } doWhile;
853       struct
854       {
855          Statement init;
856          Statement check;
857          OldList * increment;
858          Statement stmt;
859       } forStmt;
860       struct
861       {
862          Identifier id;
863       } gotoStmt;
864       struct
865       {
866          Specifier spec;
867          char * statements;
868          OldList * inputFields;
869          OldList * outputFields;
870          OldList * clobberedFields;
871       } asmStmt;
872       struct
873       {
874          Expression watcher, object;
875          OldList * watches;   // OldList of PropertyWatch for a StmtWatch, list of property identifiers for firewatches, stopwatching
876       } _watch;
877       struct
878       {
879          Identifier id;
880          OldList * exp;
881          OldList * filter;
882          Statement stmt;
883       } forEachStmt;
884       Declaration decl;
885    };
886 };
887
888 public enum DeclarationType { structDeclaration, initDeclaration, instDeclaration, defineDeclaration };
889
890 public class Declaration : struct
891 {
892 public:
893    Declaration prev, next;
894    Location loc;
895    DeclarationType type;
896    union
897    {
898       struct
899       {
900          OldList * specifiers;
901          OldList * declarators;
902       };
903       Instantiation inst;
904       struct
905       {
906          Identifier id;
907          Expression exp;
908       };
909    };
910    Specifier extStorage;
911    Symbol symbol;
912    AccessMode declMode;
913 };
914
915 public class Instantiation : struct
916 {
917 public:
918    Instantiation prev, next;
919    Location loc;
920    Specifier _class;
921    Expression exp;
922    OldList * members;
923    Symbol symbol;
924    bool fullSet;
925    bool isConstant;
926    byte * data;
927    Location nameLoc, insideLoc;
928    bool built;
929 };
930
931 public enum MembersInitType { dataMembersInit, methodMembersInit };
932
933 public class FunctionDefinition : struct
934 {
935 public:
936    FunctionDefinition prev, next;
937    Location loc;
938    OldList * specifiers;
939    Declarator declarator;
940    OldList * declarations;
941    Statement body;
942    Class _class;
943    OldList attached;    // For IDE
944    AccessMode declMode;
945
946    Type type;
947    Symbol propSet;
948
949    int tempCount;
950    bool propertyNoThis; // Not used yet; might use to support both this = and return syntax for conversion properties
951 };
952
953 public class ClassFunction : struct
954 {
955 public:
956    ClassFunction prev, next;
957    Location loc;
958    OldList * specifiers;
959    Declarator declarator;
960    OldList * declarations;
961    Statement body;
962    Class _class;
963    OldList attached;    // For IDE
964    AccessMode declMode;
965
966    // COMPILING DATA
967    Type type;
968    Symbol propSet;
969
970    bool isVirtual;
971    bool isConstructor, isDestructor;
972    bool dontMangle;
973    int id, idCode;
974 };
975
976 public class MembersInit : struct
977 {
978 public:
979    MembersInit prev, next;
980    Location loc;
981    MembersInitType type;
982    union
983    {
984       OldList * dataMembers;
985       ClassFunction function;
986    };
987    //bool coloned;
988 };
989
990 public class MemberInit : struct
991 {
992 public:
993    MemberInit prev, next;
994    Location loc;
995    Location realLoc;
996    OldList * identifiers;
997    // Expression exp;
998    Initializer initializer;
999
1000    // COMPILE DATA
1001    bool used;
1002    bool variable;
1003    bool takeOutExp;
1004 };
1005
1006 public class ClassDefinition : struct
1007 {
1008 public:
1009    ClassDefinition prev, next;
1010    Location loc;
1011    Specifier _class;
1012    // Specifier base;
1013    OldList * baseSpecs;
1014    OldList * definitions;
1015    Symbol symbol;
1016    Location blockStart;
1017    Location nameLoc;
1018    int endid;
1019    AccessMode declMode;
1020    bool deleteWatchable;
1021 };
1022
1023 public class PropertyWatch : struct
1024 {
1025 public:
1026    PropertyWatch prev, next;
1027    Location loc;
1028    Statement compound;
1029    OldList * properties;
1030    bool deleteWatch;
1031 };
1032
1033 public enum ClassDefType
1034 {
1035    functionClassDef, defaultPropertiesClassDef, declarationClassDef, propertyClassDef,
1036    propertyWatchClassDef, classDesignerClassDef, classNoExpansionClassDef, classFixedClassDef,
1037    designerDefaultPropertyClassDef, classDataClassDef, classPropertyClassDef, classPropertyValueClassDef,
1038    memberAccessClassDef, accessOverrideClassDef
1039 };
1040
1041 public class PropertyDef : struct
1042 {
1043 public:
1044    PropertyDef prev, next;
1045    Location loc;
1046    OldList * specifiers;
1047    Declarator declarator;
1048    Identifier id;
1049    Statement getStmt;
1050    Statement setStmt;
1051    Statement issetStmt;
1052    Symbol symbol;
1053    Expression category;
1054    struct
1055    {
1056       bool conversion:1;
1057       bool isWatchable:1;
1058       bool isDBProp:1;
1059    };
1060 };
1061
1062 public class ClassDef : struct
1063 {
1064 public:
1065    ClassDef prev, next;
1066    Location loc;
1067    ClassDefType type;
1068    union
1069    {
1070       Declaration decl;
1071       ClassFunction function;
1072       OldList * defProperties;
1073       PropertyDef propertyDef;
1074       PropertyWatch propertyWatch;
1075       char * designer;
1076       Identifier defaultProperty;
1077       struct
1078       {
1079          Identifier id;
1080          Initializer initializer;
1081       };
1082    };
1083    AccessMode memberAccess;
1084
1085    // IDE
1086    void * object;
1087 };
1088
1089 public enum ExternalType { functionExternal, declarationExternal, classExternal, importExternal, nameSpaceExternal, dbtableExternal };
1090
1091 public class External : struct
1092 {
1093 public:
1094    External prev, next;
1095    Location loc;
1096    ExternalType type;
1097    Symbol symbol;
1098    union
1099    {
1100       FunctionDefinition function;
1101       ClassDefinition _class;
1102       Declaration declaration;
1103       char * importString;
1104       Identifier id;
1105       DBTableDef table;
1106    };
1107    ImportType importType;
1108 };
1109
1110 public class Context : struct
1111 {
1112 public:
1113    Context parent;
1114    BinaryTree types { CompareKey = (void *)BinaryTree::CompareString };
1115    BinaryTree classes { CompareKey = (void *)BinaryTree::CompareString };
1116    BinaryTree symbols { CompareKey = (void *)BinaryTree::CompareString };
1117    BinaryTree structSymbols { CompareKey = (void *)BinaryTree::CompareString };
1118    int nextID;
1119    int simpleID;
1120    BinaryTree templateTypes { CompareKey = (void *)BinaryTree::CompareString };
1121    ClassDefinition classDef;
1122    bool templateTypesOnly;
1123    bool hasNameSpace;
1124 };
1125
1126 /*************** Compiling passes symbols ***************/
1127
1128 public class Symbol : struct
1129 {
1130 public:
1131    char * string;
1132    Symbol parent, left, right;   // Reusing left, right as prev, next when in excludedSymbols
1133    int depth;
1134
1135    Type type;
1136    union
1137    {
1138       Method method;
1139       Property _property;
1140       Class registered;
1141    };
1142    int id, idCode;
1143    union
1144    {
1145       struct
1146       {
1147          External pointerExternal;  // external declaration for the pointer to the Class:    e.g. __ecereClass___ecereNameSpace__ecere__com__Class
1148          External structExternal;   // external declaration for the actual class members structure: e.g. __ecereNameSpace__ecere__com__Class
1149       };
1150       struct
1151       {
1152          External externalGet;
1153          External externalSet;
1154          External externalPtr;    // Property pointer for watchers
1155          External externalIsSet;
1156       };
1157       struct
1158       {
1159          External methodExternal;
1160          External methodCodeExternal;
1161       };
1162    };
1163    bool imported, declaredStructSym;
1164    Class _class; // for properties only...
1165
1166    // Only used for classes right now...
1167    bool declaredStruct;
1168    bool needConstructor, needDestructor;
1169    char * constructorName, * structName, * className, * destructorName;
1170
1171    ModuleImport module;
1172    ClassImport _import;
1173    Location nameLoc;
1174    bool isParam;
1175    bool isRemote;
1176    bool isStruct;
1177    bool fireWatchersDone;
1178    int declaring;
1179    bool classData;
1180    bool isStatic;
1181    char * shortName;
1182    OldList * templateParams;     // Review the necessity for this
1183    OldList templatedClasses;
1184    Context ctx;
1185    int isIterator;
1186    Expression propCategory;
1187 };
1188
1189 // For the .imp file:
1190 public class ClassImport : struct
1191 {
1192 public:
1193    ClassImport prev, next;
1194    char * name;
1195    OldList methods;
1196    OldList properties;
1197    bool itself;
1198    int isRemote;
1199 };
1200
1201 public class FunctionImport : struct
1202 {
1203 public:
1204    FunctionImport prev, next;
1205    char * name;
1206 };
1207
1208 public class ModuleImport : struct
1209 {
1210 public:
1211    ModuleImport prev, next;
1212    char * name;
1213    OldList classes;
1214    OldList functions;
1215    ImportType importType;
1216    AccessMode importAccess;
1217 };
1218
1219 public class PropertyImport : struct
1220 {
1221 public:
1222    PropertyImport prev, next;
1223    char * name;
1224    bool isVirtual;
1225    bool hasSet, hasGet;
1226 };
1227
1228 public class MethodImport : struct
1229 {
1230 public:
1231    MethodImport prev, next;
1232    char * name;
1233    bool isVirtual;
1234 };
1235
1236 // For the .sym file:
1237 public enum TypeKind
1238 {
1239    voidType, charType, shortType, intType, int64Type, longType, floatType,
1240    doubleType, classType, structType, unionType, functionType, arrayType, pointerType,
1241    ellipsisType, enumType, methodType, vaListType, /*typedObjectType, anyObjectType, classPointerType, */ dummyType,
1242    subClassType, templateType, thisClassType, intPtrType, intSizeType, _BoolType
1243 };
1244
1245 public class Type : struct
1246 {
1247 public:
1248    Type prev, next;
1249    int refCount;
1250    union
1251    {
1252       Symbol _class;
1253       struct
1254       {
1255          OldList members;
1256          char * enumName;
1257       };
1258       // For a function:
1259       struct
1260       {
1261          Type returnType;
1262          OldList params;
1263          Symbol thisClass;
1264          bool staticMethod;
1265          TemplateParameter thisClassTemplate;
1266       };
1267       // For a method
1268       struct
1269       {
1270          Method method;
1271          Class methodClass;      // Clarify what this is!
1272          Class usedClass;
1273       };
1274
1275       // For an array:
1276       struct
1277       {
1278          Type arrayType;
1279          int arraySize;
1280          Expression arraySizeExp;
1281          bool freeExp;
1282          Symbol enumClass;
1283       };
1284       // For a pointer:
1285       Type type;
1286       TemplateParameter templateParameter;
1287    };
1288    TypeKind kind;
1289    uint size;
1290    char * name;
1291    char * typeName;
1292
1293    ClassObjectType classObjectType;
1294    int alignment;
1295    uint offset;
1296    int bitFieldCount;
1297    int count;
1298
1299    bool isSigned:1;
1300    bool constant:1;
1301    bool truth:1;
1302    bool byReference:1;
1303    bool extraParam:1;      // Clarify this... One thing it is used for is adaptive method with their own type explicitly specified
1304    bool directClassAccess:1;     // Need to clarify this if this had the same intended purpose as declaredWithStruct
1305    bool computing:1;
1306    bool keepCast:1;
1307    bool passAsTemplate:1;
1308    bool dllExport:1;
1309    bool attrStdcall:1;
1310    bool declaredWithStruct:1;
1311    bool typedByReference:1;      // Originally typed by reference, regardless of class type
1312    // TODO: Add _Complex & _Imaginary support
1313    // bool complex:1, imaginary:1;
1314
1315    char * OnGetString(char * tempString, void * fieldData, bool * needClass)
1316    {
1317       Type type = (Type)this;
1318       tempString[0] = '\0';
1319       if(type)
1320          PrintType(type, tempString, false, true);
1321       return tempString;
1322    }
1323
1324    void OnFree()
1325    {
1326
1327    }
1328 };
1329
1330 public struct Operand
1331 {
1332 public:
1333    TypeKind kind;
1334    Type type;
1335    unsigned int ptrSize;
1336    union    // Promote to using data value
1337    {
1338       char c;
1339       unsigned char uc;
1340       short s;
1341       unsigned short us;
1342       int i;
1343       unsigned int ui;
1344       float f;
1345       double d;
1346       // unsigned char * p; // Now always storing addresses in ui64
1347       int64 i64;
1348       uint64 ui64;
1349       // intptr iptr;
1350       // uintptr uiptr;
1351    };
1352    OpTable ops;
1353 };
1354
1355 public struct OpTable
1356 {
1357 public:
1358    // binary arithmetic
1359    bool (* Add)(Expression, Operand, Operand);
1360    bool (* Sub)(Expression, Operand, Operand);
1361    bool (* Mul)(Expression, Operand, Operand);
1362    bool (* Div)(Expression, Operand, Operand);
1363    bool (* Mod)(Expression, Operand, Operand);
1364
1365    // unary arithmetic
1366    bool (* Neg)(Expression, Operand);
1367
1368    // unary arithmetic increment and decrement
1369    bool (* Inc)(Expression, Operand);
1370    bool (* Dec)(Expression, Operand);
1371
1372    // binary arithmetic assignment
1373    bool (* Asign)(Expression, Operand, Operand);
1374    bool (* AddAsign)(Expression, Operand, Operand);
1375    bool (* SubAsign)(Expression, Operand, Operand);
1376    bool (* MulAsign)(Expression, Operand, Operand);
1377    bool (* DivAsign)(Expression, Operand, Operand);
1378    bool (* ModAsign)(Expression, Operand, Operand);
1379
1380    // binary bitwise
1381    bool (* BitAnd)(Expression, Operand, Operand);
1382    bool (* BitOr)(Expression, Operand, Operand);
1383    bool (* BitXor)(Expression, Operand, Operand);
1384    bool (* LShift)(Expression, Operand, Operand);
1385    bool (* RShift)(Expression, Operand, Operand);
1386    bool (* BitNot)(Expression, Operand);
1387
1388    // binary bitwise assignment
1389    bool (* AndAsign)(Expression, Operand, Operand);
1390    bool (* OrAsign)(Expression, Operand, Operand);
1391    bool (* XorAsign)(Expression, Operand, Operand);
1392    bool (* LShiftAsign)(Expression, Operand, Operand);
1393    bool (* RShiftAsign)(Expression, Operand, Operand);
1394
1395    // unary logical negation
1396    bool (* Not)(Expression, Operand);
1397
1398    // binary logical equality
1399    bool (* Equ)(Expression, Operand, Operand);
1400    bool (* Nqu)(Expression, Operand, Operand);
1401
1402    // binary logical
1403    bool (* And)(Expression, Operand, Operand);
1404    bool (* Or)(Expression, Operand, Operand);
1405
1406    // binary logical relational
1407    bool (* Grt)(Expression, Operand, Operand);
1408    bool (* Sma)(Expression, Operand, Operand);
1409    bool (* GrtEqu)(Expression, Operand, Operand);
1410    bool (* SmaEqu)(Expression, Operand, Operand);
1411
1412    bool (* Cond)(Expression, Operand, Operand, Operand);
1413 };
1414
1415 define MAX_INCLUDE_DEPTH = 30;
1416
1417 #include <stdarg.h>
1418
1419 void Compiler_Error(char * format, ...)
1420 {
1421    if(inCompiler)
1422    {
1423       if(!parsingType)
1424       {
1425          va_list args;
1426          char string[10000];
1427
1428          if(yylloc.start.included)
1429          {
1430             GetWorkingDir(string, sizeof(string));
1431             PathCat(string, GetIncludeFileFromID(yylloc.start.included));
1432          }
1433          else
1434          {
1435             GetWorkingDir(string, sizeof(string));
1436             PathCat(string, sourceFile);
1437          }
1438          printf(string);
1439
1440          /*
1441          yylloc.start.col = yylloc.end.col = 1;
1442          yylloc.start.line = yylloc.end.line = 1;
1443          yylloc.start.pos = yylloc.end.pos = 0;
1444          */
1445 #ifdef _DEBUG
1446          if(!yylloc.start.line)
1447             printf("no line");
1448 #endif
1449
1450          //printf("(%d, %d) : error: ", yylloc.start.line, yylloc.start.charPos);
1451          printf($":%d:%d: error: ", yylloc.start.line, yylloc.start.charPos);
1452          //printf(":%d: error: ", yylloc.start.line);
1453          va_start(args, format);
1454          vsnprintf(string, sizeof(string), format, args);
1455          string[sizeof(string)-1] = 0;
1456          va_end(args);
1457          fputs(string, stdout);
1458          __thisModule.application.exitCode = 1;
1459       }
1460       else
1461       {
1462          // Error parsing type
1463          parseTypeError = true;
1464       }
1465    }
1466 }
1467
1468 int numWarnings;
1469 public int GetNumWarnings() { return numWarnings; }
1470
1471 void Compiler_Warning(char * format, ...)
1472 {
1473    if(inCompiler)
1474    {
1475       va_list args;
1476       char string[10000];
1477       char fileName[MAX_FILENAME];
1478
1479       if(yylloc.start.included)
1480       {
1481          String include = GetIncludeFileFromID(yylloc.start.included);
1482          GetWorkingDir(string, sizeof(string));
1483          PathCat(string, include);
1484       }
1485       else
1486       {
1487          GetWorkingDir(string, sizeof(string));
1488          PathCat(string, sourceFile);
1489       }
1490
1491       // Skip these warnings from MinGW-w64 GCC 4.8 in intrin-impl.h
1492       GetLastDirectory(string, fileName);
1493       if(!strcmp(fileName, "intrin-impl.h")) return;
1494
1495       printf(string);
1496
1497       //printf("(%d, %d) : warning: ", yylloc.start.line, yylloc.start.charPos);
1498       printf($":%d:%d: warning: ", yylloc.start.line, yylloc.start.charPos);
1499       //printf(":%d: warning: ", yylloc.start.line);
1500       va_start(args, format);
1501       vsnprintf(string, sizeof(string), format, args);
1502       string[sizeof(string)-1] = 0;
1503       va_end(args);
1504       fputs(string, stdout);
1505       numWarnings++;
1506    }
1507 }
1508 bool parseError;
1509 bool skipErrors;
1510
1511 int yyerror()
1512 {
1513    if(!skipErrors)
1514    {
1515         //fflush(stdout);
1516         //printf("\n%*s\n%*s\n", column, "^", column, s);
1517       parseError = true;
1518       Compiler_Error($"syntax error\n");
1519    }
1520    return 0;
1521 }
1522
1523 Platform targetPlatform;
1524
1525 public int GetHostBits()
1526 {
1527    // Default to runtime platform in case we fail to determine host
1528    int hostBits = (sizeof(uintptr) == 8) ? 64 : 32;
1529    String hostType = getenv("HOSTTYPE");
1530    char host[256];
1531    if(!hostType)
1532    {
1533       DualPipe f = DualPipeOpen({ output = true }, "uname -m");
1534       if(f)
1535       {
1536          if(f.GetLine(host, sizeof(host)))
1537             hostType = host;
1538          delete f;
1539       }
1540    }
1541    if(hostType)
1542    {
1543       if(!strcmp(hostType, "x86_64"))
1544          hostBits = 64;
1545       else if(!strcmp(hostType, "i386") || !strcmp(hostType, "i686"))
1546          hostBits = 32;
1547    }
1548    return hostBits;
1549 }
1550
1551 public void SetTargetPlatform(Platform platform) { targetPlatform = platform; };
1552
1553 int targetBits;
1554
1555 public void SetTargetBits(int bits) { targetBits = bits; };
1556 public int GetTargetBits() { return targetBits; };