buildsystem,ecc,ecp,ecs,ide; (#1055) allow cross-compile from 64 bit sdk to Android...
[sdk] / compiler / ecp / ecp.ec
index 918d571..1163916 100644 (file)
@@ -13,9 +13,9 @@ import "ec"
 #define ACCESS_CLASSDATA(_class, baseClass) \
    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
 
-#include <stdarg.h>
+// #include <stdarg.h>
 
-// WARNING: PropertyDefine, ClassDefine and DataMemberDefine must remain compatible 
+// WARNING: PropertyDefine, ClassDefine and DataMemberDefine must remain compatible
 struct ClassDefine : Definition
 {
    char * base;
@@ -53,7 +53,7 @@ class MethodDefine : struct
    bool isVirtual;
 };
 
-// WARNING: PropertyDefine, ClassDefine and DataMemberDefine must remain compatible 
+// WARNING: PropertyDefine, ClassDefine and DataMemberDefine must remain compatible
 class PropertyDefine : struct
 {
    PropertyDefine prev, next;
@@ -67,7 +67,7 @@ class PropertyDefine : struct
    bool hasSet, hasGet;
 };
 
-// WARNING: PropertyDefine, ClassDefine and DataMemberDefine must remain compatible 
+// WARNING: PropertyDefine, ClassDefine and DataMemberDefine must remain compatible
 class DataMemberDefine : struct
 {
    DataMemberDefine prev, next;
@@ -155,16 +155,16 @@ static void AddDefinitions(ClassDefine classDefine, DataMemberDefine parentMembe
             if(decl.type == structDeclaration)
             {
                Declarator d;
-               
+
                if(decl.declarators)
                {
                   for(d = decl.declarators->first; d; d = d.next)
                   {
+                     Identifier declId = GetDeclId(d);
                      //if(d.type != DeclaratorFunction)
+                     if(declId)
                      {
-                        Identifier declId = GetDeclId(d);
-
-                        dataMemberDefine = DataMemberDefine 
+                        dataMemberDefine = DataMemberDefine
                         {
                            isProperty = MemberType::dataMember;
                            memberType = normalMember;
@@ -194,7 +194,7 @@ static void AddDefinitions(ClassDefine classDefine, DataMemberDefine parentMembe
                               if(posExp.isConstant)
                                  bitPos = strtol(posExp.constant, null, 0);
                               FreeExpression(posExp);
-                     
+
                            }
 
                            d.structDecl.exp = null;
@@ -228,10 +228,10 @@ static void AddDefinitions(ClassDefine classDefine, DataMemberDefine parentMembe
                               PrintType(dataType, typeString, false, true);
 
                               if(member)
-                                 dataMember = eMember_AddDataMember(member, declId.string, 
+                                 dataMember = eMember_AddDataMember(member, declId.string,
                                     typeString, /*ComputeTypeSize(dataType)*/ 0, 0, def.memberAccess);
                               else
-                                 dataMember = eClass_AddDataMember(regClass, declId.string, 
+                                 dataMember = eClass_AddDataMember(regClass, declId.string,
                                     typeString, /*ComputeTypeSize(dataType)*/ 0, 0, def.memberAccess);
                               if(dataMember)
                                  dataMember.dataType = dataType;
@@ -261,7 +261,7 @@ static void AddDefinitions(ClassDefine classDefine, DataMemberDefine parentMembe
                               memberType = (spec.type == SpecifierType::unionSpecifier) ? unionMember : structMember;
                            };
                            parentMemberDefine.dataMembers.Add(dataMemberDefine);
-                           
+
                            AddDefinitions(null, dataMemberDefine, null, dataMember, spec.definitions);
 
                            if(member)
@@ -284,10 +284,10 @@ static void AddDefinitions(ClassDefine classDefine, DataMemberDefine parentMembe
                               PrintType(dataType, typeString, false, true);
 
                               if(member)
-                                 dataMember = eMember_AddDataMember(member, id.string, 
+                                 dataMember = eMember_AddDataMember(member, id.string,
                                     typeString, /*ComputeTypeSize(dataType)*/ 0, 0, def.memberAccess);
                               else
-                                 dataMember = eClass_AddDataMember(regClass, id.string, 
+                                 dataMember = eClass_AddDataMember(regClass, id.string,
                                     typeString, /*ComputeTypeSize(dataType)*/ 0, 0, def.memberAccess);
 
                               //delete dataTypeString;
@@ -364,7 +364,7 @@ static void AddDefinitions(ClassDefine classDefine, DataMemberDefine parentMembe
                if(prop)
                {
                   prop.symbol = Symbol
-                  { 
+                  {
                      string = CopyString(propertyDef.symbol.string);
                      id = propertyDef.symbol.id;
                      type = propertyDef.symbol.type;
@@ -452,7 +452,7 @@ static void AddDefinitions(ClassDefine classDefine, DataMemberDefine parentMembe
 
                member = eClass_AddDataMember(regClass, def.id.string, null, 0, 0, def.memberAccess);
 
-               dataMemberDefine = DataMemberDefine 
+               dataMemberDefine = DataMemberDefine
                {
                   isProperty = dataMember;
                   memberType = normalMember;
@@ -498,12 +498,12 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
             }
          }
       }
-      else if(baseType.kind == structType || baseType.kind == unionType) 
+      else if(baseType.kind == structType || baseType.kind == unionType)
       {
          classType = noHeadClass;
          baseName[0] = '\0';
       }
-      else 
+      else
          unitType = true;
       FreeType(baseType);
 
@@ -575,7 +575,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
       unionDefine = DataMemberDefine
       {
          isProperty = dataMember;
-         memberType = DataMemberType::unionMember;      
+         memberType = DataMemberType::unionMember;
       };
       classDefine.propertiesAndMembers.Add(unionDefine);
 
@@ -673,7 +673,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
             switch(param.type)
             {
                case type:
-                  defaultArg.dataTypeString = 
+                  defaultArg.dataTypeString =
                      StringFromSpecDecl(param.defaultArgument.templateDatatype.specifiers, param.defaultArgument.templateDatatype.decl);
                   break;
                case identifier:
@@ -688,7 +688,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                      else
                         strcpy(memberString, param.defaultArgument.identifier._class.name);
                   }
-                  
+
                   if(memberString[0])
                   {
                      strcat(memberString, "::");
@@ -730,7 +730,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
             char * typeString = param.dataType ? StringFromSpecDecl(param.dataType.specifiers, param.dataType.decl) : null;
             eClass_AddTemplateParameter(regClass, param.identifier.string, param.type, typeString, defaultArg);
 
-            /*eClass_AddTemplateParameter(regClass, param.identifier.string, param.type, 
+            /*eClass_AddTemplateParameter(regClass, param.identifier.string, param.type,
                (param.type == type) ? eSystem_FindClass(regClass.module, typeString) : CopyString(typeString), defaultArg);*/
             // delete typeString;
          }
@@ -755,7 +755,7 @@ static void ProcessClassEnumValues(ClassType classType, OldList definitions, Sym
                refCount = 1;
             };
             e.exp.destType = destType;
-            
+
             // Set parsingType to avoid producing errors
             SetParsingType(true);
             ProcessExpressionType(e.exp);
@@ -788,7 +788,7 @@ static void ProcessClassEnumValues(ClassType classType, OldList definitions, Sym
             }
             else
                // Sort of an error
-               eEnum_AddValue(regClass, e.id.string);                        
+               eEnum_AddValue(regClass, e.id.string);
          }
          else
             eEnum_AddValue(regClass, e.id.string);
@@ -827,7 +827,7 @@ void PreCompPreProcessClassDefinitions()
                   Specifier specifier;
                   for(specifier = declaration.specifiers->first; specifier; specifier = specifier.next)
                   {
-                     if((specifier.type == enumSpecifier || specifier.type == structSpecifier || specifier.type == unionSpecifier) && specifier.id && specifier.id.string && 
+                     if((specifier.type == enumSpecifier || specifier.type == structSpecifier || specifier.type == unionSpecifier) && specifier.id && specifier.id.string &&
                         (declaration.declMode || specifier.baseSpecs || (specifier.type == enumSpecifier && specifier.definitions)))
                      {
                         Symbol symbol = FindClass(specifier.id.string);
@@ -920,7 +920,7 @@ void PreCompPreProcessClassDefinitions()
          typeString[0] = '\0';
          PrintType(function.declarator.symbol.type, typeString, true, true);
          functionDefine = FunctionDefine
-         { 
+         {
             type = functionDefinition;
             name = CopyString(function.declarator.symbol.string);
             dataType = CopyString(typeString);
@@ -951,7 +951,7 @@ void PreCompPreProcessClassDefinitions()
                Specifier specifier;
                for(specifier = declaration.specifiers->first; specifier; specifier = specifier.next)
                {
-                  if((specifier.type == enumSpecifier) && specifier.id && specifier.id.string && 
+                  if((specifier.type == enumSpecifier) && specifier.id && specifier.id.string &&
                      (declaration.declMode || specifier.baseSpecs || (specifier.type == enumSpecifier && specifier.definitions)))
                   {
                      Symbol symbol = FindClass(specifier.id.string);
@@ -1208,7 +1208,7 @@ static void OutputSymbols(char * fileName)
                }
                f.Printf("         .\n");
             }
-            
+
             if(!classDefine.isStatic)
             {
                if(classDefine.methods.first)
@@ -1275,7 +1275,7 @@ static void OutputSymbols(char * fileName)
       }
       f.Printf("   .\n");
       delete f;
-   }   
+   }
 }
 
 class PrecompApp : Application
@@ -1319,6 +1319,17 @@ class PrecompApp : Application
          argc++;
       }*/
 
+#ifdef _DEBUG
+      printf("\nArguments given:\n");
+      for(c=1; c<argc; c++)
+         printf(" %s", argv[c]);
+      printf("\n\n");
+      for(c=1; c<argc; c++)
+         PrintLn("Arg", c, ": ", argv[c]);
+      printf("\n");
+      //getch();
+#endif
+
       for(c = 1; c<argc; c++)
       {
          char * arg = argv[c];
@@ -1326,36 +1337,26 @@ class PrecompApp : Application
          {
             if(!strcmp(arg + 1, "m32") || !strcmp(arg + 1, "m64"))
             {
-               int argLen = strlen(arg);
-               int newLen = cppOptionsLen + 1 + argLen;
+               int newLen = cppOptionsLen + 1 + strlen(arg);
                cppOptions = renew cppOptions char[newLen + 1];
                cppOptions[cppOptionsLen] = ' ';
                strcpy(cppOptions + cppOptionsLen + 1, arg);
                cppOptionsLen = newLen;
                targetBits = !strcmp(arg + 1, "m32") ? 32 : 64;
             }
-            else if(arg[1] == 'D')
+            else if(!strcmp(arg + 1, "t32") || !strcmp(arg + 1, "t64"))
             {
-               int argLen = strlen(arg);
-               int newLen = cppOptionsLen + 1 + argLen;
-               cppOptions = renew cppOptions char[newLen + 1];
-               cppOptions[cppOptionsLen] = ' ';
-               strcpy(cppOptions + cppOptionsLen + 1, arg); 
-               cppOptionsLen = newLen;
+               targetBits = !strcmp(arg + 1, "t32") ? 32 : 64;
             }
-            else if(arg[1] == 'I')
+            else if(arg[1] == 'D' || arg[1] == 'I')
             {
-               int argLen = strlen(arg);
-               int newLen = cppOptionsLen + argLen + 3;
-               cppOptions = renew cppOptions char[newLen + 1];
-               cppOptions[cppOptionsLen] = ' ';
-               cppOptions[cppOptionsLen+1] = '-';
-               cppOptions[cppOptionsLen+2] = 'I';
-               cppOptions[cppOptionsLen+3] = '"';
-               strcpy(cppOptions + cppOptionsLen + 4, arg+2); 
-               cppOptions[newLen-1] = '\"';
-               cppOptions[newLen] = '\0';
-               cppOptionsLen = newLen;
+               char * buf;
+               int size = cppOptionsLen + 1 + strlen(arg) * 2 + 1;
+               cppOptions = renew cppOptions char[size];
+               buf = cppOptions + cppOptionsLen;
+               *buf++ = ' ';
+               PassArg(buf, arg);
+               cppOptionsLen = cppOptionsLen + 1 + strlen(buf);
             }
             else if(!strcmp(arg+1, "t"))
             {
@@ -1395,23 +1396,30 @@ class PrecompApp : Application
             {
                if(c + 1 < argc)
                {
-                  int argLen = strlen(arg);
-                  int arg1Len = strlen(argv[c+1]);
-                  int newLen = cppOptionsLen + argLen + arg1Len + 4;
-                  cppOptions = renew cppOptions char[newLen + 1];
-                  cppOptions[cppOptionsLen] = ' ';
-                  strcpy(cppOptions + cppOptionsLen + 1, arg); 
-                  cppOptions[cppOptionsLen+argLen+1] = ' ';
-                  cppOptions[cppOptionsLen+argLen+2] = '"';
-                  arg = argv[++c];
-                  strcpy(cppOptions + cppOptionsLen + argLen + 3, arg); 
-                  cppOptions[newLen-1] = '\"';
-                  cppOptions[newLen] = '\0';
-                  cppOptionsLen = newLen;
+                  char * buf;
+                  char * arg1 = argv[++c];
+                  int size = cppOptionsLen + 1 + strlen(arg) * 2 + strlen(arg1) * 2 + 1;
+                  cppOptions = renew cppOptions char[size];
+                  buf = cppOptions + cppOptionsLen;
+                  *buf++ = ' ';
+                  buf = PassArg(buf, arg);
+                  *buf++ = ' ';
+                  buf = PassArg(buf, arg1);
+                  cppOptionsLen = buf - cppOptions;
                }
                else
                   valid = false;
             }
+            else if(!strcmp(arg+1, "fno-diagnostics-show-caret"))
+            {
+               char * buf;
+               int size = cppOptionsLen + 1 + strlen(arg) * 2 + 1;
+               cppOptions = renew cppOptions char[size];
+               buf = cppOptions + cppOptionsLen;
+               *buf++ = ' ';
+               PassArg(buf, arg);
+               cppOptionsLen = cppOptionsLen + 1 + strlen(buf);
+            }
             else if(!strcmp(arg+1, "symbols"))
             {
                if(c + 1 < argc)
@@ -1426,7 +1434,7 @@ class PrecompApp : Application
             {
                if(c + 1 < argc)
                {
-                  SetDefaultNameSpace(argv[c+1]); //defaultNameSpace = argv[c+1];
+                  SetDefaultNameSpace(argv[c+1]);
                   //defaultNameSpaceLen = strlen(argv[c+1]);
                   c++;
                }
@@ -1455,29 +1463,30 @@ class PrecompApp : Application
             SetOutputFile(defaultSymFile);
          }
       }
-      
+
       if(!valid)
+      {
          printf($"Syntax:\n   ecp [-t <target platform>] [-cpp <c preprocessor>] [-o <output>] [-symbols <outputdir>] [-I<includedir>]* [-isystem <sysincludedir>]* [-D<definition>]* -c <input>\n");
+      }
       else
       {
+         DualPipe cppOutput;
          // TODO: Improve this
          char command[MAX_F_STRING*3];
-         DualPipe cppOutput;
-         
+         SetGlobalData(&globalData);
+         SetExcludedSymbols(&_excludedSymbols);
          SetGlobalContext(globalContext);
-         SetTopContext(globalContext);
          SetCurrentContext(globalContext);
-         SetExcludedSymbols(&_excludedSymbols);
-         SetGlobalData(&globalData);
+         SetTopContext(globalContext);
          SetDefines(&::defines);
          SetImports(&imports);
-         SetPrecompDefines(&precompDefines);
          SetInPreCompiler(true);
+         SetPrecompDefines(&precompDefines);
          SetTargetPlatform(targetPlatform);
          SetTargetBits(targetBits);
          SetEchoOn(false);
 
-         privateModule = (Module)__ecere_COM_Initialize(true | ((targetBits == 64)?2:0), 1, null);
+         privateModule = (Module)__ecere_COM_Initialize(true | (targetBits == sizeof(uintptr)*8 ? 0 : targetBits == 64 ? 2 : targetBits==32 ? 4 : 0) | 8, 1, null);
          SetPrivateModule(privateModule);
 
          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint"), type = ProcessTypeString("unsigned int", false) });
@@ -1487,16 +1496,21 @@ class PrecompApp : Application
          globalContext.types.Add((BTNode)Symbol { string = CopyString("byte"), type = ProcessTypeString("unsigned char", false) });
          globalContext.types.Add((BTNode)Symbol { string = CopyString("intptr_t"), type = ProcessTypeString("intptr", false) });
          globalContext.types.Add((BTNode)Symbol { string = CopyString("uintptr_t"), type = ProcessTypeString("uintptr", false) });
-         
+         globalContext.types.Add((BTNode)Symbol { string = CopyString("ssize_t"), type = ProcessTypeString("intsize", false) });
+         globalContext.types.Add((BTNode)Symbol { string = CopyString("size_t"), type = ProcessTypeString("uintsize", false) });
+
          {
             char * outputFilePath = GetOutputFile();
             if(FileExists(outputFilePath))
                DeleteFile(outputFilePath);
          }
-         
+
          snprintf(command, sizeof(command), "%s%s -x c -E \"%s\"", cppCommand, cppOptions ? cppOptions : "", GetSourceFile());
          command[sizeof(command)-1] = 0;
-
+#ifdef _DEBUG
+         PrintLn("ECP Executing:");
+         PrintLn(command);
+#endif
          if((cppOutput = DualPipeOpen({ output = true }, command)))
          {
             int exitCode;
@@ -1530,7 +1544,7 @@ class PrecompApp : Application
             SetYydebug(false);
             delete fileInput;
             SetFileInput(null);
-            
+
             ast = GetAST();
             if(!exitCode)
             {
@@ -1557,7 +1571,7 @@ class PrecompApp : Application
          ::defines.Free(FreeModuleDefine);
          imports.Free(FreeModuleImport);
 
-         precompDefines.Free(FreeDefinition);   
+         precompDefines.Free(FreeDefinition);
 
          FreeTypeData(privateModule);
          FreeIncludeFiles();
@@ -1576,8 +1590,9 @@ class PrecompApp : Application
       */
       SetSymbolsDir(null); // Free symbols dir
 
-#ifdef _DEBUG
+#if defined(_DEBUG) && defined(__WIN32__)
       // CheckMemory();
+      PrintLn("Done.");
       getch();
 #endif
    }