ecc; output 'code generated from eC source file.' comment at top of generated .c...
[sdk] / compiler / libec / src / output.ec
index d86447e..a7f4c6f 100644 (file)
@@ -75,7 +75,7 @@ static void OutputOperator(int op, File f)
    }
 }
 
-public void OutputTypeName(TypeName type, File f)
+public void OutputTypeName(TypeName type, File f, bool typeName)
 {
    /*if(type.typedObject) 
    {
@@ -87,7 +87,7 @@ public void OutputTypeName(TypeName type, File f)
       Specifier spec;
       for(spec = type.qualifiers->first; spec; spec = spec.next)
       {
-         OutputSpecifier(spec, f);
+         OutputSpecifier(spec, f, typeName);
          if(spec.next) f.Puts(" ");
       }
    }
@@ -113,7 +113,7 @@ public void OutputExpression(Expression exp, File f)
             Specifier spec;
             for(spec = exp._classExp.specifiers->first; spec; spec = spec.next)
             {
-               OutputSpecifier(spec, f);
+               OutputSpecifier(spec, f, false);
                if(spec.next) f.Puts(" ");
             }
             if(exp._classExp.decl) 
@@ -132,7 +132,7 @@ public void OutputExpression(Expression exp, File f)
       case newExp:
          f.Puts("new ");
          f.Puts(" ");
-         OutputTypeName(exp._renew.typeName, f);
+         OutputTypeName(exp._renew.typeName, f, false);
          f.Puts("[");
          OutputExpression(exp._renew.size, f);
          f.Puts("]");
@@ -140,7 +140,7 @@ public void OutputExpression(Expression exp, File f)
       case new0Exp:
          f.Puts("new0 ");
          f.Puts(" ");
-         OutputTypeName(exp._renew.typeName, f);
+         OutputTypeName(exp._renew.typeName, f, false);
          f.Puts("[");
          OutputExpression(exp._renew.size, f);
          f.Puts("]");
@@ -149,7 +149,7 @@ public void OutputExpression(Expression exp, File f)
          f.Puts("renew ");
          OutputExpression(exp._renew.exp, f);
          f.Puts(" ");
-         OutputTypeName(exp._renew.typeName, f);
+         OutputTypeName(exp._renew.typeName, f, false);
          f.Puts("[");
          OutputExpression(exp._renew.size, f);
          f.Puts("]");
@@ -158,7 +158,7 @@ public void OutputExpression(Expression exp, File f)
          f.Puts("renew0 ");
          OutputExpression(exp._renew.exp, f);
          f.Puts(" ");
-         OutputTypeName(exp._renew.typeName, f);
+         OutputTypeName(exp._renew.typeName, f, false);
          f.Puts("[");
          OutputExpression(exp._renew.size, f);
          f.Puts("]");
@@ -259,25 +259,25 @@ public void OutputExpression(Expression exp, File f)
          break;
       case typeSizeExp:
          f.Puts("sizeof(");
-         OutputTypeName(exp.typeName, f);
+         OutputTypeName(exp.typeName, f, false);
          f.Puts(")");
          break;
       case typeAlignExp:
          f.Puts("__alignof__(");
-         OutputTypeName(exp.typeName, f);
+         OutputTypeName(exp.typeName, f, false);
          f.Puts(")");
          break;
       case extensionInitializerExp:
          f.Puts("__extension__ (");
          if(exp.initializer.typeName)
-            OutputTypeName(exp.initializer.typeName, f);
+            OutputTypeName(exp.initializer.typeName, f, false);
          f.Puts(")");
          if(exp.initializer.initializer)
             OutputInitializer(exp.initializer.initializer, f);
          break;
       case castExp:
          f.Puts("(");
-         OutputTypeName(exp.cast.typeName, f);
+         OutputTypeName(exp.cast.typeName, f, false);
          f.Puts(")");
          if(exp.cast.exp)
             OutputExpression(exp.cast.exp, f);
@@ -300,7 +300,7 @@ public void OutputExpression(Expression exp, File f)
          f.Puts("__builtin_va_arg(");
          OutputExpression(exp.vaArg.exp, f);
          f.Puts(", ");
-         OutputTypeName(exp.vaArg.typeName, f);
+         OutputTypeName(exp.vaArg.typeName, f, false);
          f.Puts(")");
          break;
       case arrayExp:
@@ -330,31 +330,29 @@ static void OutputAsmField(AsmField field, File f)
    }
 }
 
+static void GetSourceName(char * name, const char * src)
+{
+   name[0] = 0;
+   if(src)
+   {
+      if(!strchr(src, '/') && !strchr(src, '\\'))
+         StripLastDirectory(sourceFile, name);
+      PathCat(name, src);
+   }
+   else if(sourceFile)
+      PathCat(name, sourceFile);
+   ChangeCh(name, '\\', '/');
+}
+
 static void OutputStatement(Statement stmt, File f)
 {
    char name[MAX_FILENAME] = "";
    char origName[MAX_FILENAME] = "";
 
-   /*
-   if(sourceFile)
-      eString_GetLastDirectory(sourceFile, name);
-   */
-
    if(inCompiler)
    {
-      if(yylloc.start.included)
-      {
-         //GetWorkingDir(name, sizeof(name));
-         PathCat(name, GetIncludeFileFromID(yylloc.start.included));
-      }
-      else if(sourceFile)
-      {
-         //GetWorkingDir(name, sizeof(name));
-         PathCat(name, sourceFile);
-      }
-      ChangeCh(name, '\\', '/');
+      GetSourceName(name, stmt.loc.start.included ? GetIncludeFileFromID(stmt.loc.start.included) : null);
 
-      //GetWorkingDir(origName, sizeof(origName));
       PathCat(origName, outputFile);
       ChangeCh(origName, '\\', '/');
    }
@@ -370,6 +368,9 @@ static void OutputStatement(Statement stmt, File f)
 
    switch(stmt.type)
    {
+      case badDeclarationStmt:
+         OutputDeclaration(stmt.decl, f);
+         break;
       case labeledStmt:
          OutputIdentifier(stmt.labeled.id, f);
          f.Puts(":\n");
@@ -597,13 +598,14 @@ static void OutputStatement(Statement stmt, File f)
                }
                else
                {
-                  PrintType(exp.expType, string, true, true);
+                  // Printing 'name' as a hack when we're dealing with typedefs with extended attributes
+                  PrintTypeNoConst(exp.expType, string, exp.expType.kind == dummyType ? true : false, true);
                   decl = SpecDeclFromString(string, specs, MkDeclaratorIdentifier(MkIdentifier("__ecereReturnVal")));
                }
 
                typeName = MkTypeName(specs, decl);
                InstDeclPassTypeName(typeName, false);
-               OutputTypeName(typeName, f);
+               OutputTypeName(typeName, f, false);
                f.Printf(";");
                FreeTypeName(typeName);
             }
@@ -646,7 +648,7 @@ static void OutputStatement(Statement stmt, File f)
          AsmField field;
          f.Puts("__asm__ ");
          if(stmt.asmStmt.spec)
-            OutputSpecifier(stmt.asmStmt.spec, f);
+            OutputSpecifier(stmt.asmStmt.spec, f, false);
          f.Puts("(");
          f.Puts(stmt.asmStmt.statements);
 
@@ -684,6 +686,7 @@ static void OutputStatement(Statement stmt, File f)
             }
          }
          f.Puts(");\n");
+         outputLine++;
          break;
       }
    }
@@ -704,7 +707,7 @@ static void OutputPointer(Pointer ptr, File f)
       f.Puts(" ");
       for(spec = ptr.qualifiers->first; spec; spec = spec.next)
       {
-         OutputSpecifier(spec, f);
+         OutputSpecifier(spec, f, false);
          if(spec.next) f.Puts(" ");
       }
    }
@@ -777,7 +780,7 @@ static void OutputDeclarator(Declarator decl, File f)
             Symbol _class = decl.array.enumClass.symbol; // FindClass(decl.array.enumClass.name);
             if(_class && _class.registered)
             {
-               f.Printf("%d", eClass_GetProperty(_class.registered, "enumSize"));
+               f.Printf("%d", (int)eClass_GetProperty(_class.registered, "enumSize"));
             }            
          }
          f.Puts("]");
@@ -793,7 +796,7 @@ static void OutputDeclarator(Declarator decl, File f)
          {
             for(param = decl.function.parameters->first; param; param = param.next)
             {
-               OutputTypeName(param, f);
+               OutputTypeName(param, f, false);
                if(param.next)
                   f.Puts(", ");
             }
@@ -862,7 +865,7 @@ static void OutputExtDecl(ExtDecl extDecl, File f)
       OutputAttrib(extDecl.attr, f);
 }
 
-static void OutputSpecifier(Specifier spec, File f)
+static void OutputSpecifier(Specifier spec, File f, bool typeName)
 {
    switch(spec.type)
    {
@@ -908,12 +911,12 @@ static void OutputSpecifier(Specifier spec, File f)
                f.Puts("int");
                break;
             case UINT:
-               f.Puts("unsigned int");
+               f.Puts(typeName ? "uint" : "unsigned int");
                break;
             case INT64:
                //f.Puts("__int64");
                //f.Puts("int64");
-               f.Puts("long long");
+               f.Puts(typeName ? "int64" : "long long");
                break;
             case VALIST:
                f.Puts("__builtin_va_list");
@@ -998,6 +1001,11 @@ static void OutputSpecifier(Specifier spec, File f)
       case unionSpecifier:
       {
          f.Puts((spec.type == structSpecifier) ? "struct" : "union");
+         if(spec.extDeclStruct)
+         {
+            f.Puts(" ");
+            OutputExtDecl(spec.extDeclStruct, f);
+         }
          if(spec.id)
          {
             f.Puts(" ");
@@ -1013,7 +1021,7 @@ static void OutputSpecifier(Specifier spec, File f)
                //OutputDeclaration(decl, f);
                OutputClassDef(def, f);
             }
-            f.Puts("}");
+            f.Puts("} __attribute__ ((gcc_struct))");
          }
          break;
       }
@@ -1024,7 +1032,7 @@ static void OutputSpecifier(Specifier spec, File f)
          break;
       case subClassSpecifier:
          f.Puts("subclass(");
-         OutputSpecifier(spec._class, f);
+         OutputSpecifier(spec._class, f, false);
          f.Puts(")");                  
          break;
       case templateTypeSpecifier:
@@ -1038,26 +1046,10 @@ static void OutputInitializer(Initializer initializer, File f)
    char name[MAX_FILENAME] = "";
    char origName[MAX_FILENAME] = "";
 
-   /*
-   if(sourceFile)
-      eString_GetLastDirectory(sourceFile, name);
-   */
-
    if(inCompiler)
    {
-      if(yylloc.start.included)
-      {
-         //GetWorkingDir(name, sizeof(name));
-         PathCat(name, GetIncludeFileFromID(yylloc.start.included));
-      }
-      else if(sourceFile)
-      {
-         //GetWorkingDir(name, sizeof(name));
-         PathCat(name, sourceFile);
-      }
-      ChangeCh(name, '\\', '/');
+      GetSourceName(name, initializer.loc.start.included ? GetIncludeFileFromID(initializer.loc.start.included) : null);
 
-      //GetWorkingDir(origName, sizeof(origName));
       PathCat(origName, outputFile);
       ChangeCh(origName, '\\', '/');
    }
@@ -1139,22 +1131,8 @@ static void OutputDeclaration(Declaration decl, File f)
                if(d.initializer)
                {
                   char name[MAX_FILENAME] = "";
-                  /*
-                  if(sourceFile)
-                     GetLastDirectory(sourceFile, name);
-                  */
 
-                  if(yylloc.start.included)
-                  {
-                     //GetWorkingDir(name, sizeof(name));
-                     PathCat(name, GetIncludeFileFromID(yylloc.start.included));
-                  }
-                  else if(sourceFile)
-                  {
-                     //GetWorkingDir(name, sizeof(name));
-                     PathCat(name, sourceFile);
-                  }
-                  ChangeCh(name, '\\', '/');
+                  GetSourceName(name, decl.loc.start.included ? GetIncludeFileFromID(decl.loc.start.included) : null);
 
                   if(inCompiler && outputLineNumbers && decl.loc.start.line) 
                   {
@@ -1170,9 +1148,12 @@ static void OutputDeclaration(Declaration decl, File f)
 
          if(decl.specifiers)
          {
+            bool inTypeDef = false;
             for(spec = decl.specifiers->first; spec; spec = spec.next)
             {
-               OutputSpecifier(spec, f);
+               if(spec.type == baseSpecifier && spec.specifier == TYPEDEF)
+                  inTypeDef = true;
+               OutputSpecifier(spec, f, inTypeDef && !spec.next);
                if(spec.next) f.Puts(" ");
             }
          }
@@ -1194,7 +1175,7 @@ static void OutputDeclaration(Declaration decl, File f)
          {
             for(spec = decl.specifiers->first; spec; spec = spec.next)
             {
-               OutputSpecifier(spec, f);
+               OutputSpecifier(spec, f, false);
                if(spec.next) f.Puts(" ");
             }
          }
@@ -1212,7 +1193,7 @@ static void OutputDeclaration(Declaration decl, File f)
          if(decl.extStorage)
          {
             f.Puts(" ");
-            OutputSpecifier(decl.extStorage, f);
+            OutputSpecifier(decl.extStorage, f, false);
          }
          break;
       }
@@ -1245,7 +1226,7 @@ static void OutputFunction(FunctionDefinition func, File f)
       Specifier spec;
       for(spec = func.specifiers->first; spec; spec = spec.next)
       {
-         OutputSpecifier(spec, f);
+         OutputSpecifier(spec, f, false);
          if(spec.next) f.Puts(" ");
       }
       f.Puts(" ");
@@ -1270,17 +1251,7 @@ static void OutputFunction(FunctionDefinition func, File f)
          char name[1024] = "";
          Identifier id = GetDeclId(func.declarator);
 
-         if(yylloc.start.included)
-         {
-            //GetWorkingDir(name, sizeof(name));
-            PathCat(name, GetIncludeFileFromID(yylloc.start.included));
-         }
-         else if(sourceFile)
-         {
-            //GetWorkingDir(name, sizeof(name));
-            PathCat(name, sourceFile);
-         }
-         ChangeCh(name, '\\', '/');
+         GetSourceName(name, func.loc.start.included ? GetIncludeFileFromID(func.loc.start.included) : null);
 
          f.Printf("{\n");
          f.Printf("   __ecereNameSpace__ecere__com__MemoryGuard_PushLoc(\"%s:%s\");\n", name, id.string);
@@ -1349,7 +1320,7 @@ static void OutputMembersInit(MembersInit init, File f)
 static void OutputInstance(Instantiation inst, File f)
 {
    if(inst._class)
-      OutputSpecifier(inst._class, f);
+      OutputSpecifier(inst._class, f, false);
    if(inst.exp)
    {
       f.Puts(" ");
@@ -1390,7 +1361,7 @@ static void OutputClassFunction(ClassFunction func, File f)
       Specifier spec;
       for(spec = func.specifiers->first; spec; spec = spec.next)
       {
-         OutputSpecifier(spec, f);
+         OutputSpecifier(spec, f, false);
          if(spec.next) f.Puts(" ");
       }
       f.Puts(" ");
@@ -1399,7 +1370,7 @@ static void OutputClassFunction(ClassFunction func, File f)
    {
       //if(func.class != (void *)-1)
       if(func.class)
-         OutputSpecifier(func.class, f);
+         OutputSpecifier(func.class, f, false);
       f.Puts("::");
    }*/
    if(func.declarator) OutputDeclarator(func.declarator, f);
@@ -1463,7 +1434,7 @@ static void OutputClassDef(ClassDef def, File f)
 static void OutputClass(ClassDefinition _class, File f)
 {
    f.Puts("class ");
-   OutputSpecifier(_class._class, f);
+   OutputSpecifier(_class._class, f, false);
    if(_class.baseSpecs)
    {
       Specifier spec;
@@ -1471,7 +1442,7 @@ static void OutputClass(ClassDefinition _class, File f)
       f.Puts(" : ");
       for(spec = _class.baseSpecs->first; spec; spec = spec.next)
       {
-         OutputSpecifier(spec, f);
+         OutputSpecifier(spec, f, false);
       }
    }
    if(_class.definitions)
@@ -1494,7 +1465,7 @@ public void OutputTree(OldList ast, File f)
 {
    External external;
 
-   outputLine = 25;
+   outputLine = 31;
 
    for(external = ast.first; external; external = external.next)
    {
@@ -1529,7 +1500,7 @@ public char * StringFromSpecDecl(OldList specs, Declarator decl)
    typeName.qualifiers = specs;
    typeName.declarator = decl;
 
-   OutputTypeName(typeName, f);
+   OutputTypeName(typeName, f, true);
 
    delete typeName;