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