compiler/libec Improvements to conversion from AST->Type class, and to outputting...
[sdk] / compiler / libec / src / pass15.ec
index d7071df..f140449 100644 (file)
@@ -269,7 +269,7 @@ public char * PrintUShort(unsigned short result)
    if(result > 32767)
       sprintf(temp, "0x%X", (int)result);
    else
-      sprintf(temp, "%d", result);
+      sprintf(temp, "%d", (int)result);
    return CopyString(temp);
 }
 
@@ -279,7 +279,7 @@ public char * PrintChar(char result)
    if(result > 0 && isprint(result))
       sprintf(temp, "'%c'", result);
    else if(result < 0)
-      sprintf(temp, "%d", result);
+      sprintf(temp, "%d", (int)result);
    else
       //sprintf(temp, "%#X", result);
       sprintf(temp, "0x%X", (unsigned char)result);
@@ -757,7 +757,8 @@ public int ComputeTypeSize(Type type)
             }
 
             size = ComputeTypeSize(type.type) * type.arraySize;
-            type.alignment = type.type.alignment;
+            if(type.type)
+               type.alignment = type.type.alignment;
             
             break;
          case structType:
@@ -3373,9 +3374,9 @@ bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type de
                         char constant[256];
                         sourceExp.type = constantExp;
                         if(!strcmp(baseClass.dataTypeString, "int"))
-                           sprintf(constant, "%d",value.data);
+                           sprintf(constant, "%d",(int)value.data);
                         else
-                           sprintf(constant, "0x%X",value.data);
+                           sprintf(constant, "0x%X",(int)value.data);
                         sourceExp.constant = CopyString(constant);
                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
                      }
@@ -3918,9 +3919,9 @@ bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, b
                            char constant[256];
                            sourceExp.type = constantExp;
                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
-                              sprintf(constant, "%d",value.data);
+                              sprintf(constant, "%d", (int) value.data);
                            else
-                              sprintf(constant, "0x%X",value.data);
+                              sprintf(constant, "0x%X", (int) value.data);
                            sourceExp.constant = CopyString(constant);
                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
                         }
@@ -6385,63 +6386,39 @@ static void GetTypeSpecs(Type type, OldList * specs)
    }
 }
 
+static void PrintArraySize(Type arrayType, char * string)
+{
+   char size[256];
+   size[0] = '\0';
+   strcat(size, "[");
+   if(arrayType.enumClass)
+      strcat(size, arrayType.enumClass.string);
+   else if(arrayType.arraySizeExp)
+      PrintExpression(arrayType.arraySizeExp, size);
+   strcat(size, "]");
+   strcat(string, size);
+}
+
 // WARNING : This function expects a null terminated string since it recursively concatenate...
-static void _PrintType(Type type, char * string, bool printName, bool printFunction, bool fullName)
+static void PrintTypeSpecs(Type type, char * string, bool fullName)
 {
    if(type)
    {
+      if(type.constant)
+         strcat(string, "const ");
       switch(type.kind)
       {
          case classType:
-            if(type._class && type._class.string)
+         {
+            Symbol c = type._class;
+            if(c && c.string)
             {
                // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
                //       look into merging with thisclass ?
                if(type.classObjectType == typedObject)
                   strcat(string, "typed_object");
-               else if(fullName)
-                  strcat(string, type._class.string);
                else
-               {
-                  if(type._class.registered)
-                     strcat(string, type._class.registered.name);
-                  else
-                     strcat(string, type._class.string);                     
-               }
-            }
-            break;
-         case pointerType:
-         {
-            /*Type funcType;
-            for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
-            if(funcType && funcType.kind == functionType)
-            {
-               Type param;
-               PrintType(funcType.returnType, string, false, fullName);
-               strcat(string, "(*");
-               if(printName || funcType.thisClass)
-               {
-                  strcat(string, " ");
-                  if(funcType.thisClass)
-                  {
-                     strcat(string, funcType.thisClass.string);
-                     strcat(string, "::");
-                  }
-                  if(type.name)
-                     strcat(string, type.name);
-               }
-               strcat(string, ")(");
-               for(param = funcType.params.first; param; param = param.next)
-               {
-                  PrintType(param, string, false, fullName);
-                  if(param.next) strcat(string, ", ");
-               }
-               strcat(string, ")");               
-            }
-            else*/
-            {
-               _PrintType(type.type, string, false /*printName*/, printFunction, fullName);
-               strcat(string, " *");
+                  strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
             }
             break;
          }
@@ -6461,17 +6438,11 @@ static void _PrintType(Type type, char * string, bool printName, bool printFunct
                strcat(string, type.enumName);
             }
             else if(type.typeName)
-            {
                strcat(string, type.typeName);
-            }
             else
             {
-               /*
-               strcat(string, "struct ");
-               strcat(string,"(unnamed)");
-               */
                Type member;
-               strcat(string, "struct {");
+               strcat(string, "struct { ");
                for(member = type.members.first; member; member = member.next)
                {
                   PrintType(member, string, true, fullName);
@@ -6487,9 +6458,7 @@ static void _PrintType(Type type, char * string, bool printName, bool printFunct
                strcat(string, type.enumName);
             }
             else if(type.typeName)
-            {
                strcat(string, type.typeName);
-            }
             else
             {
                strcat(string, "union ");
@@ -6503,125 +6472,13 @@ static void _PrintType(Type type, char * string, bool printName, bool printFunct
                strcat(string, type.enumName);
             }
             else if(type.typeName)
-            {
                strcat(string, type.typeName);
-            }
             else
                strcat(string, "enum");
             break;
-         case functionType:
-         {
-            if(printFunction)
-            {
-               if(type.dllExport)
-                  strcat(string, "dllexport ");
-               PrintType(type.returnType, string, false, fullName);
-               strcat(string, " ");
-            }
-            
-            // DANGER: Testing This
-            if(printName)
-            {
-               if(type.name)
-               {
-                  if(fullName)
-                     strcat(string, type.name);
-                  else
-                  {
-                     char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
-                     if(name) name += 2; else name = type.name;
-                     strcat(string, name);
-                  }
-               }
-            }
-
-            if(printFunction)
-            {
-               Type param;
-               strcat(string, "(");
-               for(param = type.params.first; param; param = param.next)
-               {
-                  PrintType(param, string, true, fullName);
-                  if(param.next) strcat(string, ", ");
-               }
-               strcat(string, ")");
-            }
-            break;
-         }
-         case arrayType:
-         {
-            /*Type funcType;
-            for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
-            if(funcType && funcType.kind == functionType)
-            {
-               Type param;
-               PrintType(funcType.returnType, string, false, fullName);
-               strcat(string, "(*");
-               if(printName || funcType.thisClass)
-               {
-                  strcat(string, " ");
-                  if(funcType.thisClass)
-                  {
-                     strcat(string, funcType.thisClass.string);
-                     strcat(string, "::");
-                  }
-                  if(type.name)
-                     strcat(string, type.name);
-               }
-               strcat(string, ")(");
-               for(param = funcType.params.first; param; param = param.next)
-               {
-                  PrintType(param, string, false, fullName);
-                  if(param.next) strcat(string, ", ");
-               }
-               strcat(string, ")");               
-            }
-            else*/
-            {
-               char baseType[1024], size[256];
-               Type arrayType = type;
-               baseType[0] = '\0';
-               size[0] = '\0';
-
-               while(arrayType.kind == TypeKind::arrayType)
-               {
-                  strcat(size, "[");
-                  if(arrayType.enumClass)
-                     strcat(size, arrayType.enumClass.string);
-                  else if(arrayType.arraySizeExp)
-                     PrintExpression(arrayType.arraySizeExp, size);
-                  //sprintf(string, "%s[%s]", baseType, size); 
-                  strcat(size, "]");
-
-                  arrayType = arrayType.arrayType;
-               }
-               _PrintType(arrayType, baseType, printName, printFunction, fullName);
-               strcat(string, baseType);
-               strcat(string, size);
-            }
-
-            /*
-               PrintType(type.arrayType, baseType, printName, fullName);
-               if(type.enumClass)
-                  strcpy(size, type.enumClass.string);
-               else if(type.arraySizeExp)
-                  PrintExpression(type.arraySizeExp, size);
-               //sprintf(string, "%s[%s]", baseType, size); 
-               strcat(string, baseType);
-               strcat(string, "[");
-               strcat(string, size); 
-               strcat(string, "]");
-               */
-
-            printName = false;
-            break;
-         }
          case ellipsisType:
             strcat(string, "...");
             break;
-         case methodType:
-            _PrintType(type.method.dataType, string, false, printFunction, fullName);
-            break;
          case subClassType:
             strcat(string, "subclass(");
             strcat(string, type._class ? type._class.string : "int");
@@ -6634,44 +6491,78 @@ static void _PrintType(Type type, char * string, bool printName, bool printFunct
             strcat(string, "thisclass");
             break;
          case vaListType:
-         strcat(string, "__builtin_va_list");
+            strcat(string, "__builtin_va_list");
             break;
       }
-      if(type.name && printName && type.kind != functionType && (type.kind != pointerType || type.type.kind != functionType))
-      {
-         strcat(string, " ");
+   }
+}
+
+static void PrintName(Type type, char * string, bool fullName)
+{
+   if(type.name && type.name[0])
+   {
+      strcat(string, " ");
+      if(fullName)
          strcat(string, type.name);
+      else
+      {
+         char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
+         if(name) name += 2; else name = type.name;
+         strcat(string, name);
       }
    }
 }
 
-// *****
-// TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
-// *****
-void PrintType(Type type, char * string, bool printName, bool fullName)
+static void PrePrintType(Type type, char * string, bool printName, bool fullName)
 {
-   Type funcType;
-   for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
-   if(funcType && funcType.kind == functionType && type != funcType)
+   if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
    {
-      char typeString[1024];
-      Type param;
+      if((type.kind == functionType || type.kind == methodType) && type.dllExport)
+         strcat(string, "dllexport ");
+      if(type.constant && (type.kind == functionType || type.kind == methodType))
+         strcat(string, " const");
+      PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, printName, fullName);
+      if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
+         strcat(string, " (");
+      if(type.kind == pointerType)
+         strcat(string, (type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType) ? "*" : " *");
+      if(type.constant && type.kind == pointerType)
+         strcat(string, " const");
+   }
+   else
+      PrintTypeSpecs(type, string, fullName);
+   if(printName && type.name)
+      PrintName(type, string, fullName);
+}
 
-      PrintType(funcType.returnType, string, false, fullName);
-      strcat(string, "(");
-      _PrintType(type, string, printName, false, fullName);
+static void PostPrintType(Type type, char * string, bool fullName)
+{
+   if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
       strcat(string, ")");
-
+   if(type.kind == arrayType)
+      PrintArraySize(type, string);
+   else if(type.kind == functionType)
+   {
+      Type param;
       strcat(string, "(");
-      for(param = funcType.params.first; param; param = param.next)
+      for(param = type.params.first; param; param = param.next)
       {
          PrintType(param, string, true, fullName);
          if(param.next) strcat(string, ", ");
       }
       strcat(string, ")");
    }
-   else
-      _PrintType(type, string, printName, true, fullName);
+   if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
+      PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
+}
+
+// *****
+// TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
+// *****
+void PrintType(Type type, char * string, bool printName, bool fullName)
+{
+   PrePrintType(type, string, printName, fullName);
+   PostPrintType(type, string, fullName);
    if(type.bitFieldCount)
    {
       char count[100];
@@ -6767,9 +6658,9 @@ static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassChe
                exp.type = constantExp;
                exp.isConstant = true;
                if(!strcmp(baseClass.dataTypeString, "int"))
-                  sprintf(constant, "%d",value.data);
+                  sprintf(constant, "%d",(int)value.data);
                else
-                  sprintf(constant, "0x%X",value.data);
+                  sprintf(constant, "0x%X",(int)value.data);
                exp.constant = CopyString(constant);
                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
                exp.expType = MkClassType(baseClass.fullName);