wip II
[sdk] / compiler / libec / src / ecdefs.ec
index 4d56a41..380a73f 100644 (file)
@@ -93,6 +93,53 @@ public void SetDefines(OldList * list) { defines = list; }
 bool outputLineNumbers = true;
 public void SetOutputLineNumbers(bool value) { outputLineNumbers = value; }
 
+public void FixModuleName(char *moduleName)
+{
+   ChangeCh(moduleName, '.', '_');
+   ChangeCh(moduleName, ' ', '_');
+   ChangeCh(moduleName, '-', '_');
+   ChangeCh(moduleName, '&', '_');
+}
+
+// todo support %var% variables for windows and $var for linux?
+public char * PassArg(char * output, const char * input)
+{
+#ifdef __WIN32__
+//define windowsFileNameCharsNeedEscaping = " !%&'()+,;=[]^`{}~"; // "#$-.@_" are ok
+   const char * escChars = " !\"%&'()+,;=[]^`{}~"; // windowsFileNameCharsNeedEscaping;
+   const char * escCharsQuoted = "\"";
+#else
+//define linuxFileNameCharsNeedEscaping = " !\"$&'()*:;<=>?[\\`{|"; // "#%+,-.@]^_}~" are ok
+   const char * escChars = " !\"$&'()*:;<=>?[\\`{|"; // linuxFileNameCharsNeedEscaping;
+   const char * escCharsQuoted = "\"()$";
+#endif
+   bool quoting = false;
+   char *o = output, *i = input, *l = input;
+#ifdef __WIN32__
+   while(*l && !strchr(escChars, *l)) l++;
+   if(*l) quoting = true;
+#else
+   if(*i == '-')
+   {
+      l++;
+      while(*l && !strchr(escChars, *l)) l++;
+      if(*l) quoting = true;
+      *o++ = *i++;
+   }
+#endif
+   if(quoting)
+      *o++ = '\"';
+   while(*i)
+   {
+      if(strchr(quoting ? escCharsQuoted : escChars, *i))
+         *o++ = '\\';
+      *o++ = *i++;
+   }
+   if(quoting)
+      *o++ = '\"';
+   *o = '\0';
+   return o;
+}
 /*public Module GetPrivateModule()
 {
    return privateModule;
@@ -125,7 +172,7 @@ public struct CodePosition
 {
 public:
    int line, charPos, pos;
-   bool included;
+   int included;
 
    void AdjustDelete(BufferLocation start, BufferLocation end)
    {
@@ -238,7 +285,7 @@ public enum ExpressionType
    noDebuggerErrorExp, debugStateErrorExp,
    extensionCompoundExp, classExp, classDataExp, new0Exp, renew0Exp,
    dbopenExp, dbfieldExp, dbtableExp, dbindexExp, extensionExpressionExp, extensionInitializerExp,
-   vaArgExp, arrayExp
+   vaArgExp, arrayExp, typeAlignExp
 };
 
 public enum MemberType
@@ -312,6 +359,7 @@ public:
       int specifier;
       struct
       {
+         ExtDecl extDecl;
          char * name;
          Symbol symbol;
          OldList * templateArgs;
@@ -324,6 +372,7 @@ public:
          OldList * definitions;
          bool addNameSpace;
          Context ctx;
+         ExtDecl extDeclStruct;
       };
       Expression expression;
       Specifier _class;
@@ -331,6 +380,40 @@ public:
    };
 };
 
+public class Attribute : struct
+{
+public:
+   Attribute prev, next;
+   Location loc;
+   String attr;
+   Expression exp;
+}
+
+public class Attrib : struct
+{
+public:
+   Location loc;
+   int type;
+   OldList * attribs;
+}
+
+public class ExtDecl : struct
+{
+public:
+   Location loc;
+   ExtDeclType type;
+   union
+   {
+      String s;
+      Attrib attr;
+   };
+}
+
+public enum ExtDeclType
+{
+   extDeclString, extDeclAttrib
+};
+
 public class Expression : struct
 {
 public:
@@ -431,7 +514,7 @@ public:
 
    DataValue val;
 
-   uint address;
+   uint64 address;
    bool hasAddress;
 
    // *** COMPILING DATA ***
@@ -504,7 +587,7 @@ public:
       {
          Expression exp;
          Expression posExp;
-         char * attrib;
+         Attrib attrib;
       } structDecl;
       struct
       {
@@ -521,7 +604,7 @@ public:
       } pointer;
       struct
       {
-         char * extended;
+         ExtDecl extended;
       } extended;
    };
 };
@@ -835,7 +918,7 @@ public:
    Symbol symbol;
    bool conversion;
    bool isWatchable;
-   char * category;
+   Expression category;
 };
 
 public class ClassDef : struct
@@ -899,6 +982,7 @@ public:
    BinaryTree templateTypes { CompareKey = (void *)BinaryTree::CompareString };
    ClassDefinition classDef;
    bool templateTypesOnly;
+   bool hasNameSpace;
 };
 
 /*************** Compiling passes symbols ***************/
@@ -961,6 +1045,7 @@ public:
    OldList templatedClasses;
    Context ctx;
    int isIterator;
+   Expression propCategory;
 };
 
 // For the .imp file:
@@ -1016,7 +1101,7 @@ public enum TypeKind
    voidType, charType, shortType, intType, int64Type, longType, floatType,
    doubleType, classType, structType, unionType, functionType, arrayType, pointerType,
    ellipsisType, enumType, methodType, vaListType, /*typedObjectType, anyObjectType, classPointerType, */ dummyType,
-   subClassType, templateType, thisClassType
+   subClassType, templateType, thisClassType, intPtrType, intSizeType
 };
 
 public class Type : struct
@@ -1045,7 +1130,7 @@ public:
       struct
       {
          Method method;
-         Class methodClass;
+         Class methodClass;      // Clarify what this is!
          Class usedClass;
       };
 
@@ -1062,27 +1147,30 @@ public:
       Type type;
       TemplateParameter templateParameter;
    };
-   bool isSigned;
    TypeKind kind;
-   bool constant;
    uint size;
    char * name;
    char * typeName;
-   bool count;
-   bool truth;
 
    ClassObjectType classObjectType;
-   bool byReference;
-
-   bool extraParam;
    int alignment;
-   bool directClassAccess;
-   bool computing;
-   bool dllExport;
    uint offset;
-   bool keepCast;
-   bool passAsTemplate;
    int bitFieldCount;
+   int count;
+
+   bool isSigned:1;
+   bool constant:1;
+   bool truth:1;
+   bool byReference:1;
+   bool extraParam:1;      // Clarify this... One thing it is used for is adaptive method with their own type explicitly specified
+   bool directClassAccess:1;     // Need to clarify this if this had the same intended purpose as declaredWithStruct
+   bool computing:1;
+   bool keepCast:1;
+   bool passAsTemplate:1;
+   bool dllExport:1;
+   bool attrStdcall:1;
+   bool declaredWithStruct:1;
+   bool typedByReference:1;      // Originally typed by reference, regardless of class type
 
    char * OnGetString(char * tempString, void * fieldData, bool * needClass)
    {
@@ -1115,9 +1203,11 @@ public:
       unsigned int ui;
       float f;
       double d;
-      unsigned char * p;
+      // unsigned char * p; // Now always storing addresses in ui64
       int64 i64;
       uint64 ui64;
+      // intptr iptr;
+      // uintptr uiptr;
    };
    OpTable ops;
 };
@@ -1182,7 +1272,7 @@ public:
    bool (* Cond)(Expression, Operand, Operand, Operand);
 };
 
-define MAX_INCLUDE_DEPTH = 10;
+define MAX_INCLUDE_DEPTH = 30;
 
 #include <stdarg.h>
 
@@ -1218,10 +1308,11 @@ void Compiler_Error(char * format, ...)
 #endif
 
          //printf("(%d, %d) : error: ", yylloc.start.line, yylloc.start.charPos);
-         printf(":%d:%d: error: ", yylloc.start.line, yylloc.start.charPos);
+         printf($":%d:%d: error: ", yylloc.start.line, yylloc.start.charPos);
          //printf(":%d: error: ", yylloc.start.line);
          va_start(args, format);
-         vsprintf(string,format,args);
+         vsnprintf(string, sizeof(string), format, args);
+         string[sizeof(string)-1] = 0;
          va_end(args);
          fputs(string, stdout);
          __thisModule.application.exitCode = 1;
@@ -1258,10 +1349,11 @@ void Compiler_Warning(char * format, ...)
       printf(string);
 
       //printf("(%d, %d) : warning: ", yylloc.start.line, yylloc.start.charPos);
-      printf(":%d:%d: warning: ", yylloc.start.line, yylloc.start.charPos);
+      printf($":%d:%d: warning: ", yylloc.start.line, yylloc.start.charPos);
       //printf(":%d: warning: ", yylloc.start.line);
       va_start(args, format);
-      vsprintf(string,format,args);
+      vsnprintf(string, sizeof(string), format, args);
+      string[sizeof(string)-1] = 0;
       va_end(args);
       fputs(string, stdout);
       numWarnings++;
@@ -1277,11 +1369,42 @@ int yyerror(char * s)
        //fflush(stdout);
        //printf("\n%*s\n%*s\n", column, "^", column, s);
       parseError = true;
-      Compiler_Error("syntax error\n");
+      Compiler_Error($"syntax error\n");
    }
    return 0;
 }
 
 Platform targetPlatform;
 
+public int GetHostBits()
+{
+   // Default to runtime platform in case we fail to determine host
+   int hostBits = (sizeof(uintptr) == 8) ? 64 : 32;
+   String hostType = getenv("HOSTTYPE");
+   char host[256];
+   if(!hostType)
+   {
+      DualPipe f = DualPipeOpen({ output = true }, "uname -m");
+      if(f)
+      {
+         if(f.GetLine(host, sizeof(host)))
+            hostType = host;
+         delete f;
+      }
+   }
+   if(hostType)
+   {
+      if(!strcmp(hostType, "x86_64"))
+         hostBits = 64;
+      else if(!strcmp(hostType, "i386") || !strcmp(hostType, "i686"))
+         hostBits = 32;
+   }
+   return hostBits;
+}
+
 public void SetTargetPlatform(Platform platform) { targetPlatform = platform; };
+
+int targetBits;
+
+public void SetTargetBits(int bits) { targetBits = bits; };
+public int GetTargetBits() { return targetBits; };