2 public import static "ecere"
24 #define yylloc _yylloc
30 identifier = IDENTIFIER,
32 stringLiteral = STRING_LITERAL,
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,
93 thisClass = THISCLASS,
94 className = CLASS_NAME,
102 _extStorage = EXT_STORAGE,
109 typedObject = TYPED_OBJECT,
110 anyObject = ANY_OBJECT,
112 extension = EXTENSION,
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,
128 nameSpace = NAMESPACE,
135 databaseOpen = DATABASE_OPEN,
137 attribDep = ATTRIB_DEP,
142 imaginary = _IMAGINARY,
143 _restrict = RESTRICT,
147 enum Order { ascending, descending };
149 public class DBTableDef : struct
154 OldList * definitions;
158 enum DBTableEntryType { fieldEntry, indexEntry };
160 class DBTableEntry : struct
162 DBTableEntry prev, next;
163 DBTableEntryType type;
176 class DBIndexItem : struct
178 DBIndexItem prev, next;
183 bool inCompiler = false;
184 public void SetInCompiler(bool b) { inCompiler = b; }
187 Context globalContext;
188 OldList * excludedSymbols;
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; }
198 public void SetFileInput(File file) { fileInput = file; }
199 char * symbolsDir = null;
200 public void SetSymbolsDir(const char * s) {
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; }
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; }
218 bool outputLineNumbers = true;
219 public void SetOutputLineNumbers(bool value) { outputLineNumbers = value; }
221 public void FixModuleName(char *moduleName)
223 ChangeCh(moduleName, '.', '_');
224 ChangeCh(moduleName, ' ', '_');
225 ChangeCh(moduleName, '-', '_');
226 ChangeCh(moduleName, '&', '_');
229 // todo support %var% variables for windows and $var for linux?
230 public char * PassArg(char * output, const char * input)
233 //define windowsFileNameCharsNeedEscaping = " !%&'()+,;=[]^`{}~"; // "#$-.@_" are ok
234 const char * escChars = " !\"%&'()+,;=[]^`{}~"; // windowsFileNameCharsNeedEscaping;
235 const char * escCharsQuoted = "\"";
237 //define linuxFileNameCharsNeedEscaping = " !\"$&'()*:;<=>?[\\`{|"; // "#%+,-.@]^_}~" are ok
238 const char * escChars = " !\"$&'()*:;<=>?[\\`{|"; // linuxFileNameCharsNeedEscaping;
239 const char * escCharsQuoted = "\"()$";
241 bool quoting = false;
243 const char *i = input, *l = input;
245 while(*l && !strchr(escChars, *l)) l++;
246 if(*l) quoting = true;
251 while(*l && !strchr(escChars, *l)) l++;
252 if(*l) quoting = true;
260 if(strchr(quoting ? escCharsQuoted : escChars, *i))
269 /*public Module GetPrivateModule()
271 return privateModule;
274 public class GlobalData : BTNode
278 char * dataTypeString;
284 public class TemplatedType : BTNode
287 TemplateParameter param;
290 class DataRedefinition : struct
292 DataRedefinition prev, next;
294 char type1[1024], type2[1024];
297 public struct CodePosition
300 int line, charPos, pos;
303 void AdjustDelete(BufferLocation start, BufferLocation end)
305 // Location is before, nothing to do
306 if(line - 1 < start.y || (line - 1 == start.y && charPos - 1 < start.x))
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)))
313 if(line - 1 >= end.y)
315 // Location is on another line
317 line -= end.y - start.y;
318 // Location is the last touched line
321 if(charPos - 1 >= end.x)
324 //if(start.line == end.line)
325 charPos -= end.x - start.x;
333 charPos = start.x + 1;
337 // Assuming no carriage return before first character ???? fixed?
338 void AdjustAdd(BufferLocation start, BufferLocation end)
340 int numLines = end.y - start.y;
341 if(line - 1 >= start.y)
343 if(line - 1 > start.y)
347 if(charPos - 1 > start.x || (charPos - 1 == start.x /*&& (numLines ? true : false)*/))
350 //charPos - 1 += numLines ? end.x : (end.x - start.x);
351 charPos += end.x - start.x;
358 public struct Location
361 CodePosition start, end;
363 bool Inside(int line, int charPos)
365 return (start.line < line || (start.line == line && start.charPos <= charPos)) &&
366 (end.line > line || (end.line == line && end.charPos >= charPos));
370 public enum DefinitionType { moduleDefinition, classDefinition, defineDefinition, functionDefinition, dataDefinition };
372 public class Definition : struct
375 Definition prev, next;
380 public class ImportedModule : struct
383 ImportedModule prev, next;
386 ImportType importType;
389 AccessMode importAccess;
392 public class Identifier : struct
395 Identifier prev, next;
397 // TODO: NameSpace * nameSpace;
404 public enum ExpressionType
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,
412 extensionCompoundExp, classExp, classDataExp, new0Exp, renew0Exp,
413 dbopenExp, dbfieldExp, dbtableExp, dbindexExp, extensionExpressionExp, extensionInitializerExp,
414 vaArgExp, arrayExp, typeAlignExp,
415 memberPropertyErrorExp, functionCallErrorExp, divideBy0ErrorExp
418 public enum MemberType
420 unresolvedMember, propertyMember, methodMember, dataMember, reverseConversionMember, classPropertyMember
423 public class ExpUsage
426 bool usageGet:1, usageSet:1, usageArg:1, usageCall:1, usageMember:1, usageDeepGet:1, usageRef:1, usageDelete:1;
429 public class TemplateParameter : struct
432 TemplateParameter prev, next;
435 TemplateParameterType type;
436 Identifier identifier;
439 TemplateDatatype dataType; // For both base datatype (type) and data type (expression)
440 TemplateMemberType memberType; // For identifier
442 TemplateArgument defaultArgument;
444 // For type parameters
445 const char * dataTypeString;
449 public class TemplateDatatype : struct
452 OldList * specifiers;
456 public class TemplateArgument : struct
459 TemplateArgument prev, next;
463 TemplateParameterType type;
466 Expression expression;
467 Identifier identifier;
468 TemplateDatatype templateDatatype;
472 public enum SpecifierType
474 baseSpecifier, nameSpecifier, enumSpecifier, structSpecifier, unionSpecifier, /*classSpecifier,*/
475 extendedSpecifier, typeOfSpecifier, subClassSpecifier, templateTypeSpecifier
478 public class Specifier : struct
481 Specifier prev, next;
492 OldList * templateArgs;
499 OldList/*<ClassDef>*/ * definitions;
502 ExtDecl extDeclStruct;
504 Expression expression;
506 TemplateParameter templateParameter;
510 public class Attribute : struct
513 Attribute prev, next;
519 public class Attrib : struct
527 public class ExtDecl : struct
539 public enum ExtDeclType
541 extDeclString, extDeclAttrib
544 public class Expression : struct
547 Expression prev, next;
555 Identifier identifier;
558 Instantiation instance;
567 OldList * specifiers;
590 MemberType memberType;
596 Expression exp1, exp2;
635 Initializer initializer;
651 // *** COMPILING DATA ***
685 public class Enumerator : struct
688 Enumerator prev, next;
694 class Pointer : struct
698 OldList * qualifiers;
702 public enum DeclaratorType
704 structDeclarator, identifierDeclarator, bracketsDeclarator, arrayDeclarator,
705 functionDeclarator, pointerDeclarator, extendedDeclarator, extendedDeclaratorEnd
708 public class Declarator : struct
711 Declarator prev, next;
714 Symbol symbol;//, propSymbol;
715 Declarator declarator;
718 Identifier identifier;
732 OldList * parameters;
745 public enum InitializerType { expInitializer, listInitializer };
747 public class Initializer : struct
750 Initializer prev, next;
752 InitializerType type;
762 public class InitDeclarator : struct
765 InitDeclarator prev, next;
767 Declarator declarator;
768 Initializer initializer;
771 public enum ClassObjectType
779 public class TypeName : struct
784 OldList * qualifiers;
785 Declarator declarator;
786 //bool /*typedObject, */byReference;
788 ClassObjectType classObjectType;
792 class AsmField : struct
797 Expression expression;
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
807 public class Statement : struct
810 Statement prev, next;
815 OldList * expressions;
828 OldList * declarations;
829 OldList * statements;
869 OldList * inputFields;
870 OldList * outputFields;
871 OldList * clobberedFields;
875 Expression watcher, object;
876 OldList * watches; // OldList of PropertyWatch for a StmtWatch, list of property identifiers for firewatches, stopwatching
889 public enum DeclarationType { structDeclaration, initDeclaration, instDeclaration, defineDeclaration };
891 public class Declaration : struct
894 Declaration prev, next;
896 DeclarationType type;
901 OldList * specifiers;
902 OldList * declarators;
911 Specifier extStorage;
916 public class Instantiation : struct
919 Instantiation prev, next;
928 Location nameLoc, insideLoc;
932 public enum MembersInitType { dataMembersInit, methodMembersInit };
934 public class FunctionDefinition : struct
937 FunctionDefinition prev, next;
939 OldList * specifiers;
940 Declarator declarator;
941 OldList * declarations;
944 OldList attached; // For IDE
951 bool propertyNoThis; // Not used yet; might use to support both this = and return syntax for conversion properties
954 public class ClassFunction : struct
957 ClassFunction prev, next;
959 OldList * specifiers;
960 Declarator declarator;
961 OldList * declarations;
964 OldList attached; // For IDE
972 bool isConstructor, isDestructor;
977 public class MembersInit : struct
980 MembersInit prev, next;
982 MembersInitType type;
985 OldList * dataMembers;
986 ClassFunction function;
991 public class MemberInit : struct
994 MemberInit prev, next;
997 OldList * identifiers;
999 Initializer initializer;
1007 public class ClassDefinition : struct
1010 ClassDefinition prev, next;
1014 OldList * baseSpecs;
1015 OldList * definitions;
1017 Location blockStart;
1020 AccessMode declMode;
1021 bool deleteWatchable;
1024 public class PropertyWatch : struct
1027 PropertyWatch prev, next;
1030 OldList * properties;
1034 public enum ClassDefType
1036 functionClassDef, defaultPropertiesClassDef, declarationClassDef, propertyClassDef,
1037 propertyWatchClassDef, classDesignerClassDef, classNoExpansionClassDef, classFixedClassDef,
1038 designerDefaultPropertyClassDef, classDataClassDef, classPropertyClassDef, classPropertyValueClassDef,
1039 memberAccessClassDef, accessOverrideClassDef
1042 public class PropertyDef : struct
1045 PropertyDef prev, next;
1047 OldList * specifiers;
1048 Declarator declarator;
1052 Statement issetStmt;
1054 Expression category;
1063 public class ClassDef : struct
1066 ClassDef prev, next;
1072 ClassFunction function;
1073 OldList * defProperties;
1074 PropertyDef propertyDef;
1075 PropertyWatch propertyWatch;
1077 Identifier defaultProperty;
1081 Initializer initializer;
1084 AccessMode memberAccess;
1090 public enum ExternalType { functionExternal, declarationExternal, classExternal, importExternal, nameSpaceExternal, dbtableExternal };
1092 public class External : struct
1095 External prev, next;
1101 FunctionDefinition function;
1102 ClassDefinition _class;
1103 Declaration declaration;
1104 char * importString;
1108 ImportType importType;
1111 public class Context : struct
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 };
1121 BinaryTree templateTypes { CompareKey = (void *)BinaryTree::CompareString };
1122 ClassDefinition classDef;
1123 bool templateTypesOnly;
1127 /*************** Compiling passes symbols ***************/
1129 public class Symbol : struct
1133 Symbol parent, left, right; // Reusing left, right as prev, next when in excludedSymbols
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
1153 External externalGet;
1154 External externalSet;
1155 External externalPtr; // Property pointer for watchers
1156 External externalIsSet;
1160 External methodExternal;
1161 External methodCodeExternal;
1164 bool imported, declaredStructSym;
1165 Class _class; // for properties only...
1167 // Only used for classes right now...
1168 bool declaredStruct;
1169 bool needConstructor, needDestructor;
1170 char * constructorName, * structName, * className, * destructorName;
1172 ModuleImport module;
1173 ClassImport _import;
1178 bool fireWatchersDone;
1183 OldList * templateParams; // Review the necessity for this
1184 OldList templatedClasses;
1187 Expression propCategory;
1190 // For the .imp file:
1191 public class ClassImport : struct
1194 ClassImport prev, next;
1202 public class FunctionImport : struct
1205 FunctionImport prev, next;
1209 public class ModuleImport : struct
1212 ModuleImport prev, next;
1216 ImportType importType;
1217 AccessMode importAccess;
1220 public class PropertyImport : struct
1223 PropertyImport prev, next;
1226 bool hasSet, hasGet;
1229 public class MethodImport : struct
1232 MethodImport prev, next;
1237 // For the .sym file:
1238 public enum TypeKind
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
1246 public class Type : struct
1266 TemplateParameter thisClassTemplate;
1272 Class methodClass; // Clarify what this is!
1281 Expression arraySizeExp;
1287 TemplateParameter templateParameter;
1294 ClassObjectType classObjectType;
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
1308 bool passAsTemplate:1;
1311 bool declaredWithStruct:1;
1312 bool typedByReference:1; // Originally typed by reference, regardless of class type
1314 // TODO: Add _Complex & _Imaginary support
1315 // bool complex:1, imaginary:1;
1317 const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
1319 Type type = (Type)this;
1320 tempString[0] = '\0';
1322 PrintType(type, tempString, false, true);
1332 public struct Operand
1337 unsigned int ptrSize;
1338 union // Promote to using data value
1348 // unsigned char * p; // Now always storing addresses in ui64
1357 public struct OpTable
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);
1368 bool (* Neg)(Expression, Operand);
1370 // unary arithmetic increment and decrement
1371 bool (* Inc)(Expression, Operand);
1372 bool (* Dec)(Expression, Operand);
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);
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);
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);
1397 // unary logical negation
1398 bool (* Not)(Expression, Operand);
1400 // binary logical equality
1401 bool (* Equ)(Expression, Operand, Operand);
1402 bool (* Nqu)(Expression, Operand, Operand);
1405 bool (* And)(Expression, Operand, Operand);
1406 bool (* Or)(Expression, Operand, Operand);
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);
1414 bool (* Cond)(Expression, Operand, Operand, Operand);
1417 define MAX_INCLUDE_DEPTH = 30;
1421 void Compiler_Error(const char * format, ...)
1430 if(yylloc.start.included)
1432 GetWorkingDir(string, sizeof(string));
1433 PathCat(string, GetIncludeFileFromID(yylloc.start.included));
1437 GetWorkingDir(string, sizeof(string));
1438 PathCat(string, sourceFile);
1443 yylloc.start.col = yylloc.end.col = 1;
1444 yylloc.start.line = yylloc.end.line = 1;
1445 yylloc.start.pos = yylloc.end.pos = 0;
1448 if(!yylloc.start.line)
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;
1459 fputs(string, stdout);
1460 __thisModule.application.exitCode = 1;
1464 // Error parsing type
1465 parseTypeError = true;
1471 public int GetNumWarnings() { return numWarnings; }
1473 void Compiler_Warning(const char * format, ...)
1479 char fileName[MAX_FILENAME];
1481 if(yylloc.start.included)
1483 String include = GetIncludeFileFromID(yylloc.start.included);
1484 GetWorkingDir(string, sizeof(string));
1485 PathCat(string, include);
1489 GetWorkingDir(string, sizeof(string));
1490 PathCat(string, sourceFile);
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;
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;
1506 fputs(string, stdout);
1518 //printf("\n%*s\n%*s\n", column, "^", column, s);
1520 Compiler_Error($"syntax error\n");
1525 Platform targetPlatform;
1527 public int GetHostBits()
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");
1535 DualPipe f = DualPipeOpen({ output = true }, "uname -m");
1538 if(f.GetLine(host, sizeof(host)))
1545 if(!strcmp(hostType, "x86_64"))
1547 else if(!strcmp(hostType, "i386") || !strcmp(hostType, "i686"))
1553 public void SetTargetPlatform(Platform platform) { targetPlatform = platform; };
1557 public void SetTargetBits(int bits) { targetBits = bits; };
1558 public int GetTargetBits() { return targetBits; };