compiler/libec: Parser improvements
[sdk] / compiler / libec / src / ast.ec
index 50cbdeb..a758930 100644 (file)
@@ -139,7 +139,7 @@ public TemplateParameter MkTypeTemplateParameter(Identifier identifier, Template
    if(identifier.string)
    {
       TemplateParameter param { type = TemplateParameterType::type, identifier = identifier, dataType = baseTplDatatype, defaultArgument = defaultArgument };
-      TemplatedType type { key = (uint)identifier.string, param = param };
+      TemplatedType type { key = (uintptr)identifier.string, param = param };
       if(!curContext.templateTypes.Add((BTNode)type))
          delete type;
       return param;
@@ -247,6 +247,14 @@ Expression MkExpString(char * string)
 public struct ContextStringPair
 {
    String string, context;
+
+   // TODO: Should this be automated somehow?
+   void OnFree()
+   {
+      delete string;
+      delete context;
+   }
+
    int OnCompare(ContextStringPair b)
    {
       int result;
@@ -256,10 +264,10 @@ public struct ContextStringPair
 
       result = (context && b.context) ? strcmp(context, b.context) :
          (!context && b.context) ? 1 : (context && !b.context) ? -1 : 0;
-      if(result) return result;
       // TODO: Support these
       // result = CaseSensitiveString::OnCompare(string, b.string);
       // result = ((CaseSensitiveString)string).OnCompare(b.string);
+      return result;
    }
 };
 
@@ -302,6 +310,7 @@ Expression MkExpIntlString(char * string, char * context)
       memcpy(msgid+1+lenContext-2+1, string+1, lenString-2);
       memcpy(msgid+1+lenContext-2+1+lenString-2, "\"", 2);
       ListAdd(list, MkExpString(msgid));
+      delete msgid;
    }
    else
       ListAdd(list, QMkExpId("null"));
@@ -365,6 +374,11 @@ Expression MkExpTypeSize(TypeName typeName)
    return { type = typeSizeExp, typeName = typeName };
 }
 
+Expression MkExpTypeAlign(TypeName typeName)
+{
+   return { type = typeAlignExp, typeName = typeName };
+}
+
 Expression MkExpClassSize(Specifier _class)
 {
    return { type = classSizeExp, _class = _class };
@@ -420,9 +434,9 @@ Specifier MkSpecifierSubClass(Specifier _class)
    return { type = subClassSpecifier, _class = _class };
 }
 
-Specifier MkSpecifierExtended(char * name)
+Specifier MkSpecifierExtended(ExtDecl extDecl)
 {
-   return { type = extendedSpecifier, name = CopyString(name) };
+   return { type = extendedSpecifier, extDecl = extDecl /*name = CopyString(name)*/ };
 }
 
 Specifier MkEnum(Identifier id, OldList list)
@@ -496,6 +510,28 @@ void AddStructDefinitions(Specifier spec, OldList definitions)
    }
 }
 
+Attribute MkAttribute(String attr, Expression exp)
+{
+   return { attr = attr, exp = exp };
+}
+
+Attrib MkAttrib(int type, OldList * attribs)
+{
+   return { type = type, attribs = attribs };
+}
+
+ExtDecl MkExtDeclString(String s)
+{
+   return { type = extDeclString, s = s };
+
+}
+
+ExtDecl MkExtDeclAttrib(Attrib attr)
+{
+   return { type = extDeclAttrib, attr = attr };
+}
+
+
 public Declarator MkDeclaratorIdentifier(Identifier id)
 {
    return { type = identifierDeclarator, identifier = id };
@@ -506,12 +542,12 @@ Declarator MkDeclaratorFunction(Declarator declarator, OldList parameters)
    return { type = functionDeclarator, declarator = declarator, function.parameters = parameters };
 }
 
-Declarator MkDeclaratorExtended(char * extended, Declarator declarator)
+Declarator MkDeclaratorExtended(ExtDecl extended, Declarator declarator)
 {
    return { type = extendedDeclarator, declarator = declarator, extended.extended = extended };
 }
 
-Declarator MkDeclaratorExtendedEnd(char * extended, Declarator declarator)
+Declarator MkDeclaratorExtendedEnd(ExtDecl extended, Declarator declarator)
 {
    return { type = extendedDeclaratorEnd, declarator = declarator, extended.extended = extended };
 }
@@ -575,6 +611,46 @@ public TypeName MkTypeName(OldList qualifiers, Declarator declarator)
    return { qualifiers = qualifiers, declarator = declarator };
 }
 
+public TypeName MkTypeNameGuessDecl(OldList qualifiers, Declarator declarator)
+{
+   TypeName typeName { qualifiers = qualifiers, declarator = declarator };
+   if(qualifiers != null)
+   {
+      bool gotType = false;
+      bool gotFullType = false;
+      Specifier spec;
+      for(spec = qualifiers.first; spec; spec = spec.next)
+      {
+         if(gotType && !declarator && ((spec.type == nameSpecifier && spec.name) || (spec.type == baseSpecifier && gotFullType)))
+         {
+            String s = null;
+            if(spec.type == nameSpecifier)
+            {
+               char * colon = RSearchString(spec.name, "::", strlen(spec.name), true, false);
+               s = colon ? colon + 2 : spec.name;
+            }
+            else if(spec.type == baseSpecifier)
+            {
+               if(spec.specifier == INT64) s = "int64";
+            }
+            if(s)
+            {
+               typeName.declarator = declarator = MkDeclaratorIdentifier(MkIdentifier(CopyString(s)));
+               qualifiers.Remove(spec);
+               FreeSpecifier(spec);
+            }
+         }
+         if(spec.type != extendedSpecifier)
+         {
+            if(spec.type != baseSpecifier || (spec.specifier != UNSIGNED && spec.specifier != SIGNED && spec.specifier != LONG))
+               gotFullType = true;
+            gotType = true;
+         }
+      }
+   }
+   return typeName;
+}
+
 public Identifier GetDeclId(Declarator decl)
 {
    while(decl && decl.type != identifierDeclarator)
@@ -669,7 +745,7 @@ Declaration MkDeclarationDefine(Identifier id, Expression exp)
    if(!eSystem_FindDefine(privateModule, id.string))
       eSystem_RegisterDefine(id.string, expString, privateModule, buildingECERECOMModule ? baseSystemAccess : publicAccess);
    else
-      Compiler_Warning("Redefinition of %s ignored\n", id.string);
+      Compiler_Warning($"Redefinition of %s ignored\n", id.string);
    return decl;
 }
 
@@ -680,6 +756,7 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
    
    if(specifiers != null)
    {
+      bool gotType = false;
       Specifier spec;
       for(spec = specifiers.first; spec; spec = spec.next)
       {
@@ -708,20 +785,32 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
             }
             else if(spec.next)
             {
-               for(; spec; spec = spec.next)
+               //for(spec = spec.next; spec; spec = spec.next)
+               spec = specifiers.last;
                {
-                  if(spec.type == nameSpecifier && spec.name)
+                  if((spec.type == nameSpecifier && spec.name) || spec.type == baseSpecifier)
                   {
-                     Symbol type
+                     String s = null;
+                     if(spec.type == nameSpecifier)
                      {
-                        string = CopyString(spec.name);
-                        type = ProcessType(specifiers, null);
-                     };
-                     type.id = type.idCode = curContext.nextID++;
-                     if(!(curContext.templateTypesOnly ? curContext.parent : curContext).types.Add((BTNode)type))
-                        excludedSymbols->Add(type);
-
-                     decl.symbol = type;
+                        char * colon = RSearchString(spec.name, "::", strlen(spec.name), true, false);
+                        s = colon ? colon + 2 : spec.name;
+                     }
+                     else if(spec.type == baseSpecifier)
+                     {
+                        if(spec.specifier == INT64) s = "int64";
+                     }
+                     if(s)
+                     {
+                        Symbol type { string = CopyString(s), type = ProcessType(specifiers, null) };
+                        type.id = type.idCode = curContext.nextID++;
+                        decl.symbol = type;
+                        decl.declarators = initDeclarators = MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(CopyString(s))), null));
+                        specifiers.Remove(spec);
+                        FreeSpecifier(spec);
+                        if(!(curContext.templateTypesOnly ? curContext.parent : curContext).types.Add((BTNode)type))
+                           excludedSymbols->Add(type);
+                     }
                   }
                }
             }
@@ -731,6 +820,30 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
          else if(spec.type == baseSpecifier && 
             (spec.specifier == STRUCT || spec.specifier == UNION))
             variable = false;
+         else
+         {
+            if(gotType && initDeclarators == null && !spec.next && ((spec.type == nameSpecifier && spec.name) || spec.type == baseSpecifier))
+            {
+               String s = null;
+               if(spec.type == nameSpecifier)
+               {
+                  char * colon = RSearchString(spec.name, "::", strlen(spec.name), true, false);
+                  s = colon ? colon + 2 : spec.name;
+               }
+               else if(spec.type == baseSpecifier)
+               {
+                  if(spec.specifier == INT64) s = "int64";
+               }
+               if(s)
+               {
+                  decl.declarators = initDeclarators = MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(CopyString(s))), null));
+                  specifiers.Remove(spec);
+                  FreeSpecifier(spec);
+               }
+            }
+         }
+         if(spec.type != extendedSpecifier)
+            gotType = true;
       }
    }
    if(variable && initDeclarators)
@@ -833,7 +946,37 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
 
 Declaration MkStructDeclaration(OldList specifiers, OldList declarators, Specifier extStorage)
 {
-   return { type = structDeclaration, declarators = declarators, specifiers = specifiers, extStorage = extStorage, loc = yylloc };
+   Declaration decl { type = structDeclaration, declarators = declarators, specifiers = specifiers, extStorage = extStorage, loc = yylloc };
+   if(specifiers != null)
+   {
+      bool gotType = false;
+      Specifier spec;
+      for(spec = specifiers.first; spec; spec = spec.next)
+      {
+         if(gotType && declarators == null && ((spec.type == nameSpecifier && spec.name) || spec.type == baseSpecifier))
+         {
+            String s = null;
+            if(spec.type == nameSpecifier)
+            {
+               char * colon = RSearchString(spec.name, "::", strlen(spec.name), true, false);
+               s = colon ? colon + 2 : spec.name;
+            }
+            else if(spec.type == baseSpecifier)
+            {
+               if(spec.specifier == INT64) s = "int64";
+            }
+            if(s)
+            {
+               decl.declarators = declarators = MkListOne(MkDeclaratorIdentifier(MkIdentifier(CopyString(s))));
+               specifiers.Remove(spec);
+               FreeSpecifier(spec);
+            }
+         }
+         if(spec.type != extendedSpecifier)
+            gotType = true;
+      }
+   }
+   return decl;
 }
 
 Statement MkLabeledStmt(Identifier id, Statement statement)
@@ -1424,7 +1567,7 @@ Instantiation MkInstantiationNamed(OldList specs, Expression exp, OldList member
          
       if(!spec)
       {
-         Compiler_Error("Expecting class specifier\n");
+         Compiler_Error($"Expecting class specifier\n");
          inst._class = MkSpecifierName /*MkClassName*/("");
          //exit(1);
          //return null;
@@ -1624,7 +1767,7 @@ void SetupBaseSpecs(Symbol symbol, OldList baseSpecs)
                         dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
                      };
                   }
-                  type = TemplatedType { key = (uint)p.name, param = param };
+                  type = TemplatedType { key = (uintptr)p.name, param = param };
                   if(!curContext.templateTypes.Add((BTNode)type))
                      delete type;
                }
@@ -1640,6 +1783,22 @@ ClassDefinition MkClass(Symbol symbol, OldList baseSpecs, OldList definitions)
    SetupBaseSpecs(symbol, baseSpecs);
    if(symbol.ctx)
    {
+      ClassDefinition classDef = symbol.ctx.classDef;
+      if(classDef)
+      {
+         // This can occur if two instances of a class are defined...
+         // To avoid dangling 'parent' Contexts, we free the previous class definition
+         External external;
+         for(external = ast->first; external; external = external.next)
+         {
+            if(external.type == classExternal && external._class == classDef)
+            {
+               ast->Remove(external);
+               FreeExternal(external);
+               break;
+            }
+         }
+      }
       FreeContext(symbol.ctx);
       delete symbol.ctx;
    }
@@ -1762,7 +1921,13 @@ Symbol FindType(Context ctx, char * name)
    Symbol type = null;
    if(curContext)
    {
+      //char output[8192];
       type = (Symbol)ctx.types.FindString(name);
+      /*if(!strcmp(name, "intptr_t") && !type)
+      {
+         ctx.types.Print(output, depthOrder);
+         puts(output);
+      }*/
       if(!type && ctx.parent)
          type = FindType(ctx.parent, name);
    }
@@ -2027,13 +2192,12 @@ public Type ProcessType(OldList specs, Declarator decl)
             if(decl.structDecl.exp.type == constantExp)
                specType.bitFieldCount = strtoul(decl.structDecl.exp.constant, null, 0);
          }
-         if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended && 
-            (!strcmp(decl.extended.extended, "__declspec(dllexport)") || !strcmp(decl.extended.extended, "dllexport")))
+         if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended && decl.extended.extended.type == extDeclString &&
+            decl.extended.extended.s && (!strcmp(decl.extended.extended.s, "__declspec(dllexport)") || !strcmp(decl.extended.extended.s, "dllexport")))
          {
             dllExport = true;
          }
-         if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended && 
-            (strstr(decl.extended.extended, "__attribute__")))
+         if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended && decl.extended.extended.type == extDeclAttrib)
          {
             specType.keepCast = true;
          }
@@ -2049,11 +2213,11 @@ public Type ProcessType(OldList specs, Declarator decl)
             bool isLong = false;
             for(spec = specs.first; spec; spec = spec.next)
             {
-               if(spec.type == extendedSpecifier && (!strcmp(spec.name, "__declspec(dllexport)") || !strcmp(spec.name, "dllexport")))
+               if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && spec.extDecl.s && (!strcmp(spec.extDecl.s, "__declspec(dllexport)") || !strcmp(spec.extDecl.s, "dllexport")))
                {
                   dllExport = true;
                }
-               if(spec.type == extendedSpecifier && strstr(spec.name, "__attribute__"))
+               if(spec.type == extendedSpecifier && spec.extDecl.type == extDeclAttrib)
                {
                   specType.keepCast = true;
                }
@@ -2106,25 +2270,40 @@ public Type ProcessType(OldList specs, Declarator decl)
                }
                else if(spec.type == nameSpecifier)
                {
-                  Symbol symbol = spec.name ? FindType(curContext, spec.name) : null;
-                  if(symbol && symbol.type)
+                  if(spec.name && (!strcmp(spec.name, "intptr") || !strcmp(spec.name, "uintptr")))
                   {
-                     // Free Type Contents:
-                     Type dummy { };
-                     *dummy = *specType;
-                     FreeType(dummy);
-
-                     CopyTypeInto(specType, symbol.type);
-                     specType.typeName = CopyString(symbol.type.name);
+                     specType.kind = intPtrType;
+                     if(!strcmp(spec.name, "uintptr"))
+                        specType.isSigned = false;
                   }
-                  else if(!isTypedef) // !specType.kind)    // TESTING THIS FOR enum / typedef problem
+                  else if(spec.name && (!strcmp(spec.name, "uintsize") || !strcmp(spec.name, "intsize")))
                   {
-                     // key.sym enum values need FindClass:
-                     specType._class = spec.name ? FindClass(spec.name) : null;
-                     // specType._class = spec.symbol; 
-                     specType.kind = classType;
-                     if(!specType._class)
-                        specType.kind = intType;
+                     specType.kind = intSizeType;
+                     if(!strcmp(spec.name, "uintsize"))
+                        specType.isSigned = false;
+                  }
+                  else
+                  {
+                     Symbol symbol = spec.name ? FindType(curContext, spec.name) : null;
+                     if(symbol && symbol.type)
+                     {
+                        // Free Type Contents:
+                        Type dummy { };
+                        *dummy = *specType;
+                        FreeType(dummy);
+
+                        CopyTypeInto(specType, symbol.type);
+                        specType.typeName = CopyString(symbol.type.name);
+                     }
+                     else if(!isTypedef) // !specType.kind)    // TESTING THIS FOR enum / typedef problem
+                     {
+                        // key.sym enum values need FindClass:
+                        specType._class = spec.name ? FindClass(spec.name) : null;
+                        // specType._class = spec.symbol; 
+                        specType.kind = classType;
+                        if(!specType._class)
+                           specType.kind = intType;
+                     }
                   }
                }
                else if(spec.type == enumSpecifier)
@@ -2357,8 +2536,8 @@ public Type ProcessType(OldList specs, Declarator decl)
          // TESTING: Added extendedDeclarator here
          while(d && (d.type == bracketsDeclarator || d.type == extendedDeclarator || d.type == extendedDeclaratorEnd))
          {
-            if((d.type == extendedDeclarator || d.type == extendedDeclaratorEnd) && d.extended.extended && 
-               (!strcmp(d.extended.extended, "__declspec(dllexport)") || !strcmp(d.extended.extended, "dllexport")))
+            if((d.type == extendedDeclarator || d.type == extendedDeclaratorEnd) && d.extended.extended && d.extended.extended.type == extDeclString &&
+               d.extended.extended.s && (!strcmp(d.extended.extended.s, "__declspec(dllexport)") || !strcmp(d.extended.extended.s, "dllexport")))
             {
                dllExport = true;            
             }
@@ -2868,8 +3047,9 @@ public void OutputIntlStrings()
       {
          char * filePrefix = "";
          if(!(srcFile[0] && (srcFile[1] == ':' || srcFile[0] == '/')))
-            filePrefix = (GetRuntimePlatform() == win32) ? ".\\" : "./";
-         GetSystemPathBuffer(srcFileFixed, srcFile);
+            filePrefix = "./"; //(GetRuntimePlatform() == win32) ? ".\\" : "./";
+         // GetSystemPathBuffer(srcFileFixed, srcFile);
+         GetSlashPathBuffer(srcFileFixed, srcFile);
          for(s : intlStrings)
          {
             // TOFIX: (#654) ContextStringPair * pair = &s;