compiler/grammar: Added support for extended declarator in between struct and identifier
[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 enum Order { ascending, descending };
25
26 public class DBTableDef : struct
27 {
28 public:
29    char * name;
30    Symbol symbol;
31    OldList * definitions;   
32    AccessMode declMode;
33 }
34
35 enum DBTableEntryType { fieldEntry, indexEntry };
36
37 class DBTableEntry : struct
38 {
39    DBTableEntry prev, next;
40    DBTableEntryType type;
41    Identifier id;
42    union
43    {
44       struct
45       {
46          TypeName dataType;
47          char * name;
48       };
49       OldList * items;
50    };
51 };
52
53 class DBIndexItem : struct
54 {
55    DBIndexItem prev, next;
56    Identifier id;
57    Order order;
58 };
59
60 bool inCompiler = false;
61 public void SetInCompiler(bool b) { inCompiler = b; }
62
63 Context curContext;
64 Context globalContext;
65 OldList * excludedSymbols;
66
67 Context topContext;
68 OldList * imports;
69 OldList * defines;
70 Module privateModule;
71 public void SetPrivateModule(Module module) { privateModule = module; }public Module GetPrivateModule() { return privateModule; }
72 ModuleImport mainModule;
73 public void SetMainModule(ModuleImport moduleImport) { mainModule = moduleImport; } public ModuleImport GetMainModule() { return mainModule; }
74 File fileInput;
75 public void SetFileInput(File file) { fileInput = file; }
76 char * symbolsDir = null;
77 public void SetSymbolsDir(char * s) { 
78    delete symbolsDir;
79    symbolsDir = CopyString(s);
80 } public char * GetSymbolsDir() { return symbolsDir ? symbolsDir : ""; }
81 char * outputFile;
82 public void SetOutputFile(char * s) { outputFile = s; } public char * GetOutputFile() { return outputFile; }
83 char * sourceFile;
84 public void SetSourceFile(char * s) { sourceFile = s; } public char * GetSourceFile() { return sourceFile; }
85
86 public void SetGlobalContext(Context context) { globalContext = context; } public Context GetGlobalContext() { return globalContext; }
87 public void SetTopContext(Context context) { topContext = context; } public Context GetTopContext() { return topContext; }
88 public void SetCurrentContext(Context context) { curContext = context; } public Context GetCurrentContext() { return curContext; }
89 public void SetExcludedSymbols(OldList * list) { excludedSymbols = list; }
90 public void SetImports(OldList * list) { imports = list; }
91 public void SetDefines(OldList * list) { defines = list; }
92
93 bool outputLineNumbers = true;
94 public void SetOutputLineNumbers(bool value) { outputLineNumbers = value; }
95
96 /*public Module GetPrivateModule()
97 {
98    return privateModule;
99 }*/
100
101 public class GlobalData : BTNode
102 {
103 public:
104    Module module;
105    char * dataTypeString;
106    Type dataType;
107    void * symbol;
108    char * fullName;
109 };
110
111 public class TemplatedType : BTNode
112 {
113 public:
114    TemplateParameter param;
115 };
116
117 class DataRedefinition : struct
118 {
119    DataRedefinition prev, next;
120    char name[1024];
121    char type1[1024], type2[1024];
122 };
123
124 public struct CodePosition
125 {
126 public:
127    int line, charPos, pos;
128    bool included;
129
130    void AdjustDelete(BufferLocation start, BufferLocation end)
131    {
132       // Location is before, nothing to do
133       if(line - 1 < start.y || (line - 1 == start.y && charPos - 1 < start.x))
134          return;
135       // Location is inside deleted bytes, point to the start
136       if((line - 1 >= start.y && (line - 1 > start.y || charPos - 1 >= start.x)) &&
137          (line - 1 >= end.y && (line - 1 > end.y || charPos - 1 >= end.x)))
138       {
139          // Location is after
140          if(line - 1 >= end.y)
141          {
142             // Location is on another line
143             if(line - 1 > end.y)
144                line -= end.y - start.y;
145             // Location is the last touched line
146             else 
147             {
148                if(charPos - 1 >= end.x)
149                {
150                   line = start.y + 1;
151                   //if(start.line == end.line)
152                      charPos -= end.x - start.x;
153                }
154             }
155          }
156       }
157       else
158       {
159          line = start.y + 1;
160          charPos = start.x + 1;
161       }
162    }
163
164    // Assuming no carriage return before first character ???? fixed?
165    void AdjustAdd(BufferLocation start, BufferLocation end)
166    {
167       int numLines = end.y - start.y;
168       if(line - 1 >= start.y)
169       {
170          if(line - 1 > start.y)
171             line += numLines;
172          else
173          {
174             if(charPos - 1 > start.x || (charPos - 1 == start.x /*&& (numLines ? true : false)*/))
175             {
176                line += numLines;
177                //charPos - 1 += numLines ? end.x : (end.x - start.x);            
178                charPos += end.x - start.x;
179             }
180          }
181       }
182    }
183 };
184
185 public struct Location
186 {
187 public:
188    CodePosition start, end;
189
190    bool Inside(int line, int charPos)
191    {
192       return (start.line < line || (start.line == line && start.charPos <= charPos)) && 
193              (end.line > line || (end.line == line && end.charPos >= charPos));
194    }
195 };
196
197 public enum DefinitionType { moduleDefinition, classDefinition, defineDefinition, functionDefinition, dataDefinition };
198
199 public class Definition : struct
200 {
201 public:
202    Definition prev, next;
203    char * name;
204    DefinitionType type;
205 };
206
207 public class ImportedModule : struct
208 {
209 public:
210    ImportedModule prev, next;
211    char * name;
212    DefinitionType type;
213    ImportType importType;
214    bool globalInstance;
215    bool dllOnly;
216    AccessMode importAccess;
217 };
218
219 public class Identifier : struct
220 {
221 public:
222    Identifier prev, next;
223    Location loc;
224    // TODO: NameSpace * nameSpace;
225    Symbol classSym;
226    Specifier _class;
227    char * string;
228    Identifier badID;
229 };
230
231 public enum ExpressionType
232
233    identifierExp, instanceExp, constantExp, stringExp, opExp,
234    bracketsExp, indexExp, callExp, memberExp, pointerExp, typeSizeExp,
235    castExp, conditionExp, newExp, renewExp, classSizeExp,
236    dummyExp, dereferenceErrorExp, symbolErrorExp, classMemberSymbolErrorExp,
237    structMemberSymbolErrorExp, memoryErrorExp, unknownErrorExp,
238    noDebuggerErrorExp, debugStateErrorExp,
239    extensionCompoundExp, classExp, classDataExp, new0Exp, renew0Exp,
240    dbopenExp, dbfieldExp, dbtableExp, dbindexExp, extensionExpressionExp, extensionInitializerExp,
241    vaArgExp, arrayExp, typeAlignExp
242 };
243
244 public enum MemberType
245 {
246    unresolvedMember, propertyMember, methodMember, dataMember, reverseConversionMember, classPropertyMember
247 };
248
249 public class ExpUsage
250 {
251 public:
252    bool usageGet:1, usageSet:1, usageArg:1, usageCall:1, usageMember:1, usageDeepGet:1, usageRef:1, usageDelete:1;
253 };
254
255 public class TemplateParameter : struct
256 {
257 public:
258    TemplateParameter prev, next;
259    Location loc;
260    
261    TemplateParameterType type;
262    Identifier identifier;
263    union
264    {
265       TemplateDatatype dataType; // For both base datatype (type) and data type (expression)
266       TemplateMemberType memberType;   // For identifier
267    };
268    TemplateArgument defaultArgument;
269     
270    // For type parameters
271    char * dataTypeString;
272    Type baseType;
273 }
274
275 public class TemplateDatatype : struct
276 {
277 public:
278    OldList * specifiers;
279    Declarator decl;
280 }
281
282 public class TemplateArgument : struct
283 {
284 public:
285    TemplateArgument prev, next;
286    Location loc;
287    
288    Identifier name;
289    TemplateParameterType type;
290    union
291    {
292       Expression expression;
293       Identifier identifier;
294       TemplateDatatype templateDatatype;
295    };
296 }
297
298 public enum SpecifierType
299 {
300    baseSpecifier, nameSpecifier, enumSpecifier, structSpecifier, unionSpecifier, /*classSpecifier,*/
301    extendedSpecifier, typeOfSpecifier, subClassSpecifier, templateTypeSpecifier
302 };
303
304 public class Specifier : struct
305 {
306 public:
307    Specifier prev, next;
308    Location loc;
309    SpecifierType type;
310    union
311    {
312       int specifier;
313       struct
314       {
315          ExtDecl extDecl;
316          char * name;
317          Symbol symbol;
318          OldList * templateArgs;
319       };
320       struct
321       {
322          Identifier id;
323          OldList * list;
324          OldList * baseSpecs;
325          OldList * definitions;
326          bool addNameSpace;
327          Context ctx;
328          ExtDecl extDeclStruct;
329       };
330       Expression expression;
331       Specifier _class;
332       TemplateParameter templateParameter;
333    };
334 };
335
336 public class Attribute : struct
337 {
338 public:
339    Attribute prev, next;
340    Location loc;
341    String attr;
342    Expression exp;
343 }
344
345 public class Attrib : struct
346 {
347 public:
348    Location loc;
349    int type;
350    OldList * attribs;
351 }
352
353 public class ExtDecl : struct
354 {
355 public:
356    Location loc;
357    ExtDeclType type;
358    union
359    {
360       String s;
361       Attrib attr;
362    };
363 }
364
365 public enum ExtDeclType
366 {
367    extDeclString, extDeclAttrib
368 };
369
370 public class Expression : struct
371 {
372 public:
373    Expression prev, next;
374    Location loc;
375    ExpressionType type;
376    union
377    {
378       struct
379       {
380          char * constant;
381          Identifier identifier;
382       };
383       Statement compound;
384       Instantiation instance;
385       char * string;
386       OldList * list;
387       struct
388       {
389          OldList * specifiers;
390          Declarator decl;
391       } _classExp;
392       struct
393       {
394          Identifier id;
395       } classData;
396       struct
397       {
398          Expression exp;
399          OldList * arguments;
400          Location argLoc;
401       } call;
402       struct
403       {
404          Expression exp;
405          OldList * index;
406       } index;
407       struct
408       {
409          Expression exp;
410          Identifier member;
411
412          MemberType memberType;
413          bool thisPtr;
414       } member;
415       struct
416       {
417          int op;
418          Expression exp1, exp2;
419       } op;
420       TypeName typeName;
421       Specifier _class;
422       struct
423       {
424          TypeName typeName;
425          Expression exp;
426       } cast;
427       struct
428       {
429          Expression cond;
430          OldList * exp;
431          Expression elseExp;
432       } cond;
433       struct
434       {
435          TypeName typeName;
436          Expression size;
437       } _new;
438       struct
439       {
440          TypeName typeName;
441          Expression size;
442          Expression exp;
443       } _renew;
444       struct
445       {
446          char * table;
447          Identifier id;
448       } db;
449       struct
450       {
451          Expression ds;
452          Expression name;
453       } dbopen;
454       struct
455       {
456          TypeName typeName;
457          Initializer initializer;
458       } initializer;
459       struct
460       {
461          Expression exp;
462          TypeName typeName;
463       } vaArg;
464    };
465
466    bool debugValue;
467
468    DataValue val;
469
470    uint address;
471    bool hasAddress;
472
473    // *** COMPILING DATA ***
474    Type expType;
475    Type destType;
476
477    ExpUsage usage;
478    int tempCount;
479    bool byReference;
480    bool isConstant;
481    bool addedThis;
482    bool needCast;
483    bool thisPtr;
484
485    void Clear()
486    {
487       debugValue = false;
488       val = { 0 };
489       address = 0;
490       hasAddress = false;
491
492       expType = null;
493       destType = null;
494
495       usage = 0;
496       tempCount = 0;
497       byReference = false;
498       isConstant = false;
499       addedThis = false;
500       needCast = false;
501       thisPtr = false;
502    }
503 };
504
505 public class Enumerator : struct
506 {
507 public:
508    Enumerator prev, next;
509    Location loc;
510    Identifier id;
511    Expression exp;
512 };
513
514 class Pointer : struct
515 {
516    Pointer prev, next;
517    Location loc;
518    OldList * qualifiers;
519    Pointer pointer;
520 };
521
522 public enum DeclaratorType
523 {
524    structDeclarator, identifierDeclarator, bracketsDeclarator, arrayDeclarator, 
525    functionDeclarator, pointerDeclarator, extendedDeclarator, extendedDeclaratorEnd
526 };
527
528 public class Declarator : struct
529 {
530 public:
531    Declarator prev, next;
532    Location loc;
533    DeclaratorType type;
534    Symbol symbol;//, propSymbol;
535    Declarator declarator;
536    union
537    {
538       Identifier identifier;
539       struct
540       {
541          Expression exp;
542          Expression posExp;
543          Attrib attrib;
544       } structDecl;
545       struct
546       {
547          Expression exp;
548          Specifier enumClass;
549       } array;
550       struct
551       {
552          OldList * parameters;
553       } function;
554       struct
555       {
556          Pointer pointer;
557       } pointer;
558       struct
559       {
560          ExtDecl extended;
561       } extended;
562    };
563 };
564
565 public enum InitializerType { expInitializer, listInitializer };
566
567 public class Initializer : struct
568 {
569 public:
570    Initializer prev, next;
571    Location loc;
572    InitializerType type;
573    union
574    {
575       Expression exp;
576       OldList * list;
577    };
578    bool isConstant;
579 };
580
581 public class InitDeclarator : struct
582 {
583 public:
584    InitDeclarator prev, next;
585    Location loc;
586    Declarator declarator;
587    Initializer initializer;
588 };
589
590 public enum ClassObjectType
591 {
592    none,
593    classPointer,
594    typedObject,
595    anyObject
596 };
597
598 public class TypeName : struct
599 {
600 public:
601    TypeName prev, next;
602    Location loc;
603    OldList * qualifiers;
604    Declarator declarator;
605    //bool /*typedObject, */byReference;
606    //bool anyObject;
607    ClassObjectType classObjectType;
608    Expression bitCount;
609 };
610
611 class AsmField : struct
612 {
613    AsmField prev, next;
614    Location loc;
615    char * command;
616    Expression expression;
617 };
618
619 public enum StmtType { labeledStmt, caseStmt, compoundStmt,
620                expressionStmt, ifStmt, switchStmt, whileStmt, doWhileStmt, 
621                forStmt, gotoStmt, continueStmt, breakStmt, returnStmt, asmStmt, badDeclarationStmt,
622                fireWatchersStmt, stopWatchingStmt, watchStmt, forEachStmt
623              };
624
625 public class Statement : struct
626 {
627 public:
628    Statement prev, next;
629    Location loc;
630    StmtType type;
631    union
632    {
633       OldList * expressions;
634       struct
635       {
636          Identifier id;
637          Statement stmt;
638       } labeled;
639       struct
640       {
641          Expression exp;
642          Statement stmt;
643       } caseStmt;
644       struct
645       {
646          OldList * declarations;
647          OldList * statements;
648          Context context;
649          bool isSwitch;
650       } compound;
651       struct
652       {
653          OldList * exp;
654          Statement stmt;
655          Statement elseStmt;                  
656       } ifStmt;
657       struct
658       {
659          OldList * exp;
660          Statement stmt;
661       } switchStmt;
662       struct
663       {
664          OldList * exp;
665          Statement stmt;
666       } whileStmt;
667       struct
668       {
669          OldList * exp;
670          Statement stmt;
671       } doWhile;
672       struct
673       {
674          Statement init;
675          Statement check;
676          OldList * increment;
677          Statement stmt;
678       } forStmt;
679       struct
680       {
681          Identifier id;
682       } gotoStmt;
683       struct
684       {
685          Specifier spec;
686          char * statements;
687          OldList * inputFields;
688          OldList * outputFields;
689          OldList * clobberedFields;
690       } asmStmt;
691       struct
692       {
693          Expression watcher, object;
694          OldList * watches;   // OldList of PropertyWatch for a StmtWatch, list of property identifiers for firewatches, stopwatching
695       } _watch;
696       struct
697       {
698          Identifier id;
699          OldList * exp;
700          OldList * filter;
701          Statement stmt;
702       } forEachStmt;
703       Declaration decl;
704    };
705 };
706
707 public enum DeclarationType { structDeclaration, initDeclaration, instDeclaration, defineDeclaration };
708
709 public class Declaration : struct
710 {
711 public:
712    Declaration prev, next;
713    Location loc;
714    DeclarationType type;
715    union
716    {
717       struct
718       {
719          OldList * specifiers;
720          OldList * declarators;
721       };
722       Instantiation inst;
723       struct
724       {
725          Identifier id;
726          Expression exp;
727       };
728    };
729    Specifier extStorage;
730    Symbol symbol;
731    AccessMode declMode;
732 };
733
734 public class Instantiation : struct
735 {
736 public:
737    Instantiation prev, next;
738    Location loc;
739    Specifier _class;
740    Expression exp;
741    OldList * members;
742    Symbol symbol;
743    bool fullSet;
744    bool isConstant;
745    byte * data;
746    Location nameLoc, insideLoc;
747    bool built;
748 };
749
750 public enum MembersInitType { dataMembersInit, methodMembersInit };
751
752 public class FunctionDefinition : struct
753 {
754 public:
755    FunctionDefinition prev, next;
756    Location loc;
757    OldList * specifiers;
758    Declarator declarator;
759    OldList * declarations;
760    Statement body;
761    Class _class;
762    OldList attached;    // For IDE
763    AccessMode declMode;
764
765    Type type;
766    Symbol propSet;
767
768    int tempCount;
769    bool propertyNoThis; // Not used yet; might use to support both this = and return syntax for conversion properties
770 };
771
772 public class ClassFunction : struct
773 {
774 public:
775    ClassFunction prev, next;
776    Location loc;
777    OldList * specifiers;
778    Declarator declarator;
779    OldList * declarations;
780    Statement body;
781    Class _class;
782    OldList attached;    // For IDE
783    AccessMode declMode;
784    
785    // COMPILING DATA
786    Type type;
787    Symbol propSet;
788
789    bool isVirtual;
790    bool isConstructor, isDestructor;
791    bool dontMangle;
792    int id, idCode;
793 };
794
795 public class MembersInit : struct
796 {
797 public:
798    MembersInit prev, next;
799    Location loc;
800    MembersInitType type;
801    union
802    {
803       OldList * dataMembers;
804       ClassFunction function;
805    };
806    //bool coloned;
807 };
808
809 public class MemberInit : struct
810 {
811 public:
812    MemberInit prev, next;
813    Location loc;
814    Location realLoc;
815    OldList * identifiers;
816    // Expression exp;
817    Initializer initializer;
818
819    // COMPILE DATA
820    bool used;
821    bool variable;
822    bool takeOutExp;
823 };
824
825 public class ClassDefinition : struct
826 {
827 public:
828    ClassDefinition prev, next;
829    Location loc;
830    Specifier _class;
831    // Specifier base;
832    OldList * baseSpecs;
833    OldList * definitions;
834    Symbol symbol;
835    Location blockStart;
836    Location nameLoc;
837    int endid;
838    AccessMode declMode;
839    bool deleteWatchable;
840 };
841
842 public class PropertyWatch : struct
843 {
844 public:
845    PropertyWatch prev, next;
846    Location loc;
847    Statement compound;
848    OldList * properties;
849    bool deleteWatch;
850 };
851
852 public enum ClassDefType
853
854    functionClassDef, defaultPropertiesClassDef, declarationClassDef, propertyClassDef,
855    propertyWatchClassDef, classDesignerClassDef, classNoExpansionClassDef, classFixedClassDef,
856    designerDefaultPropertyClassDef, classDataClassDef, classPropertyClassDef, classPropertyValueClassDef,
857    memberAccessClassDef, accessOverrideClassDef
858 };
859
860 public class PropertyDef : struct
861 {
862 public:
863    PropertyDef prev, next;
864    Location loc;
865    OldList * specifiers;
866    Declarator declarator;
867    Identifier id;
868    Statement getStmt;
869    Statement setStmt;
870    Statement issetStmt;
871    Symbol symbol;
872    bool conversion;
873    bool isWatchable;
874    Expression category;
875 };
876
877 public class ClassDef : struct
878 {
879 public:
880    ClassDef prev, next;
881    Location loc;
882    ClassDefType type;
883    union
884    {
885       Declaration decl;
886       ClassFunction function;
887       OldList * defProperties;
888       PropertyDef propertyDef;
889       PropertyWatch propertyWatch;
890       char * designer;
891       Identifier defaultProperty;
892       struct
893       {
894          Identifier id;
895          Initializer initializer;
896       };
897    };
898    AccessMode memberAccess;
899
900    // IDE
901    void * object;
902 };
903
904 public enum ExternalType { functionExternal, declarationExternal, classExternal, importExternal, nameSpaceExternal, dbtableExternal };
905
906 public class External : struct
907 {
908 public:
909    External prev, next;
910    Location loc;
911    ExternalType type;
912    Symbol symbol;
913    union
914    {
915       FunctionDefinition function;
916       ClassDefinition _class;
917       Declaration declaration;
918       char * importString;
919       Identifier id;
920       DBTableDef table;
921    };
922    ImportType importType;
923 };
924
925 public class Context : struct
926 {
927 public:
928    Context parent;
929    BinaryTree types { CompareKey = (void *)BinaryTree::CompareString };
930    BinaryTree classes { CompareKey = (void *)BinaryTree::CompareString };
931    BinaryTree symbols { CompareKey = (void *)BinaryTree::CompareString };
932    BinaryTree structSymbols { CompareKey = (void *)BinaryTree::CompareString };
933    int nextID;
934    int simpleID;
935    BinaryTree templateTypes { CompareKey = (void *)BinaryTree::CompareString };
936    ClassDefinition classDef;
937    bool templateTypesOnly;
938    bool hasNameSpace;
939 };
940
941 /*************** Compiling passes symbols ***************/
942
943 public class Symbol : struct
944 {
945 public:
946    char * string;
947    Symbol parent, left, right;   // Reusing left, right as prev, next when in excludedSymbols
948    int depth;
949
950    Type type;
951    union
952    {
953       Method method;
954       Property _property;
955       Class registered;
956    };
957    int id, idCode;
958    union
959    {
960       struct
961       {
962          External pointerExternal;  // external declaration for the pointer to the Class:    e.g. __ecereClass___ecereNameSpace__ecere__com__Class
963          External structExternal;   // external declaration for the actual class members structure: e.g. __ecereNameSpace__ecere__com__Class
964       };
965       struct
966       {
967          External externalGet;
968          External externalSet;
969          External externalPtr;    // Property pointer for watchers
970          External externalIsSet;
971       };
972       struct
973       {
974          External methodExternal;
975          External methodCodeExternal;
976       };
977    };
978    bool imported, declaredStructSym;
979    Class _class; // for properties only...
980
981    // Only used for classes right now...
982    bool declaredStruct;
983    bool needConstructor, needDestructor;
984    char * constructorName, * structName, * className, * destructorName;
985
986    ModuleImport module;
987    ClassImport _import;  
988    Location nameLoc;
989    bool isParam;
990    bool isRemote;
991    bool isStruct;
992    bool fireWatchersDone;
993    int declaring;
994    bool classData;
995    bool isStatic;
996    char * shortName;
997    OldList * templateParams;     // Review the necessity for this
998    OldList templatedClasses;
999    Context ctx;
1000    int isIterator;
1001    Expression propCategory;
1002 };
1003
1004 // For the .imp file:
1005 public class ClassImport : struct
1006 {
1007 public:
1008    ClassImport prev, next;
1009    char * name;
1010    OldList methods;
1011    OldList properties;
1012    bool itself;
1013    bool isRemote;
1014 };
1015
1016 public class FunctionImport : struct
1017 {
1018 public:
1019    FunctionImport prev, next;
1020    char * name;
1021 };
1022
1023 public class ModuleImport : struct
1024 {
1025 public:
1026    ModuleImport prev, next;
1027    char * name;
1028    OldList classes;
1029    OldList functions;
1030    ImportType importType;
1031    AccessMode importAccess;
1032 };
1033
1034 public class PropertyImport : struct
1035 {
1036 public:
1037    PropertyImport prev, next;
1038    char * name;
1039    bool isVirtual;
1040    bool hasSet, hasGet;
1041 };
1042
1043 public class MethodImport : struct
1044 {
1045 public:
1046    MethodImport prev, next;
1047    char * name;
1048    bool isVirtual;
1049 };
1050
1051 // For the .sym file:
1052 public enum TypeKind
1053
1054    voidType, charType, shortType, intType, int64Type, longType, floatType,
1055    doubleType, classType, structType, unionType, functionType, arrayType, pointerType,
1056    ellipsisType, enumType, methodType, vaListType, /*typedObjectType, anyObjectType, classPointerType, */ dummyType,
1057    subClassType, templateType, thisClassType, intPtrType
1058 };
1059
1060 public class Type : struct
1061 {
1062 public:
1063    Type prev, next;
1064    int refCount;
1065    union
1066    {
1067       Symbol _class;
1068       struct
1069       {
1070          OldList members;
1071          char * enumName;
1072       };
1073       // For a function:
1074       struct
1075       {
1076          Type returnType;
1077          OldList params;
1078          Symbol thisClass;
1079          bool staticMethod;
1080          TemplateParameter thisClassTemplate;
1081       };
1082       // For a method
1083       struct
1084       {
1085          Method method;
1086          Class methodClass;
1087          Class usedClass;
1088       };
1089
1090       // For an array:
1091       struct
1092       {
1093          Type arrayType;
1094          int arraySize;
1095          Expression arraySizeExp;
1096          bool freeExp;
1097          Symbol enumClass;
1098       };
1099       // For a pointer:
1100       Type type;
1101       TemplateParameter templateParameter;
1102    };
1103    bool isSigned;
1104    TypeKind kind;
1105    bool constant;
1106    uint size;
1107    char * name;
1108    char * typeName;
1109    bool count;
1110    bool truth;
1111
1112    ClassObjectType classObjectType;
1113    bool byReference;
1114
1115    bool extraParam;
1116    int alignment;
1117    bool directClassAccess;
1118    bool computing;
1119    bool dllExport;
1120    uint offset;
1121    bool keepCast;
1122    bool passAsTemplate;
1123    int bitFieldCount;
1124
1125    char * OnGetString(char * tempString, void * fieldData, bool * needClass)
1126    {
1127       Type type = (Type)this;
1128       tempString[0] = '\0';
1129       if(type)
1130          PrintType(type, tempString, false, true);
1131       return tempString;
1132    }
1133
1134    void OnFree()
1135    {
1136       
1137    }
1138 };            
1139
1140 public struct Operand
1141 {
1142 public:
1143    TypeKind kind;
1144    Type type;
1145    unsigned int ptrSize;
1146    union    // Promote to using data value
1147    {
1148       char c;
1149       unsigned char uc;
1150       short s;
1151       unsigned short us;
1152       int i;
1153       unsigned int ui;
1154       float f;
1155       double d;
1156       unsigned char * p;
1157       int64 i64;
1158       uint64 ui64;
1159       intptr iptr;
1160       uintptr uiptr;
1161    };
1162    OpTable ops;
1163 };
1164
1165 public struct OpTable
1166 {
1167 public:
1168    // binary arithmetic
1169    bool (* Add)(Expression, Operand, Operand);
1170    bool (* Sub)(Expression, Operand, Operand);
1171    bool (* Mul)(Expression, Operand, Operand);
1172    bool (* Div)(Expression, Operand, Operand);
1173    bool (* Mod)(Expression, Operand, Operand);
1174    
1175    // unary arithmetic
1176    bool (* Neg)(Expression, Operand);
1177    
1178    // unary arithmetic increment and decrement
1179    bool (* Inc)(Expression, Operand);
1180    bool (* Dec)(Expression, Operand);
1181    
1182    // binary arithmetic assignment
1183    bool (* Asign)(Expression, Operand, Operand);
1184    bool (* AddAsign)(Expression, Operand, Operand);
1185    bool (* SubAsign)(Expression, Operand, Operand);
1186    bool (* MulAsign)(Expression, Operand, Operand);
1187    bool (* DivAsign)(Expression, Operand, Operand);
1188    bool (* ModAsign)(Expression, Operand, Operand);
1189    
1190    // binary bitwise
1191    bool (* BitAnd)(Expression, Operand, Operand);
1192    bool (* BitOr)(Expression, Operand, Operand);
1193    bool (* BitXor)(Expression, Operand, Operand);
1194    bool (* LShift)(Expression, Operand, Operand);
1195    bool (* RShift)(Expression, Operand, Operand);
1196    bool (* BitNot)(Expression, Operand);
1197    
1198    // binary bitwise assignment
1199    bool (* AndAsign)(Expression, Operand, Operand);
1200    bool (* OrAsign)(Expression, Operand, Operand);
1201    bool (* XorAsign)(Expression, Operand, Operand);
1202    bool (* LShiftAsign)(Expression, Operand, Operand);
1203    bool (* RShiftAsign)(Expression, Operand, Operand);
1204    
1205    // unary logical negation
1206    bool (* Not)(Expression, Operand);
1207    
1208    // binary logical equality
1209    bool (* Equ)(Expression, Operand, Operand);
1210    bool (* Nqu)(Expression, Operand, Operand);
1211    
1212    // binary logical
1213    bool (* And)(Expression, Operand, Operand);
1214    bool (* Or)(Expression, Operand, Operand);
1215    
1216    // binary logical relational
1217    bool (* Grt)(Expression, Operand, Operand);
1218    bool (* Sma)(Expression, Operand, Operand);
1219    bool (* GrtEqu)(Expression, Operand, Operand);
1220    bool (* SmaEqu)(Expression, Operand, Operand);
1221    
1222    bool (* Cond)(Expression, Operand, Operand, Operand);
1223 };
1224
1225 define MAX_INCLUDE_DEPTH = 10;
1226
1227 #include <stdarg.h>
1228
1229 void Compiler_Error(char * format, ...)
1230 {
1231    if(inCompiler)
1232    {
1233       if(!parsingType)
1234       {
1235          va_list args;
1236          char string[10000];
1237
1238          if(yylloc.start.included)
1239          {
1240             GetWorkingDir(string, sizeof(string));
1241             PathCat(string, GetIncludeFileFromID(yylloc.start.included));
1242          }
1243          else
1244          {
1245             GetWorkingDir(string, sizeof(string));
1246             PathCat(string, sourceFile);
1247          }
1248          printf(string);
1249
1250          /*
1251          yylloc.start.col = yylloc.end.col = 1;
1252          yylloc.start.line = yylloc.end.line = 1;
1253          yylloc.start.pos = yylloc.end.pos = 0;
1254          */
1255 #ifdef _DEBUG
1256          if(!yylloc.start.line)
1257             printf("");
1258 #endif
1259
1260          //printf("(%d, %d) : error: ", yylloc.start.line, yylloc.start.charPos);
1261          printf($":%d:%d: error: ", yylloc.start.line, yylloc.start.charPos);
1262          //printf(":%d: error: ", yylloc.start.line);
1263          va_start(args, format);
1264          vsnprintf(string, sizeof(string), format, args);
1265          string[sizeof(string)-1] = 0;
1266          va_end(args);
1267          fputs(string, stdout);
1268          __thisModule.application.exitCode = 1;
1269       }
1270       else
1271       {
1272          // Error parsing type
1273          parseTypeError = true;
1274       }
1275    }
1276 }
1277
1278 int numWarnings;
1279 public int GetNumWarnings() { return numWarnings; }
1280
1281 void Compiler_Warning(char * format, ...)
1282 {
1283    if(inCompiler)
1284    {
1285       va_list args;
1286       char string[10000];
1287
1288       if(yylloc.start.included)
1289       {
1290          GetWorkingDir(string, sizeof(string));
1291          PathCat(string, GetIncludeFileFromID(yylloc.start.included));
1292       }
1293       else
1294       {
1295          GetWorkingDir(string, sizeof(string));
1296          PathCat(string, sourceFile);
1297       }
1298       
1299       printf(string);
1300
1301       //printf("(%d, %d) : warning: ", yylloc.start.line, yylloc.start.charPos);
1302       printf($":%d:%d: warning: ", yylloc.start.line, yylloc.start.charPos);
1303       //printf(":%d: warning: ", yylloc.start.line);
1304       va_start(args, format);
1305       vsnprintf(string, sizeof(string), format, args);
1306       string[sizeof(string)-1] = 0;
1307       va_end(args);
1308       fputs(string, stdout);
1309       numWarnings++;
1310    }
1311 }
1312 bool parseError;
1313 bool skipErrors;
1314
1315 int yyerror(char * s)
1316 {
1317    if(!skipErrors)
1318    {
1319         //fflush(stdout);
1320         //printf("\n%*s\n%*s\n", column, "^", column, s);
1321       parseError = true;
1322       Compiler_Error($"syntax error\n");
1323    }
1324    return 0;
1325 }
1326
1327 Platform targetPlatform;
1328
1329 public int GetHostBits()
1330 {
1331    // Default to runtime platform in case we fail to determine host
1332    int hostBits = (sizeof(uintptr) == 8) ? 64 : 32;
1333    String hostType = getenv("HOSTTYPE");
1334    char host[256];
1335    if(!hostType)
1336    {
1337       DualPipe f = DualPipeOpen({ output = true }, "uname -m");
1338       if(f)
1339       {
1340          if(f.GetLine(host, sizeof(host)))
1341             hostType = host;
1342          delete f;
1343       }
1344    }
1345    if(hostType)
1346    {
1347       if(!strcmp(hostType, "x86_64"))
1348          hostBits = 64;
1349       else if(!strcmp(hostType, "i386") || !strcmp(hostType, "i686"))
1350          hostBits = 32;
1351    }
1352    return hostBits;
1353 }
1354
1355 public void SetTargetPlatform(Platform platform) { targetPlatform = platform; };
1356
1357 int targetBits;
1358
1359 public void SetTargetBits(int bits) { targetBits = bits; };