compiler/libec: Fixed crash on inheriting off a struct
[sdk] / compiler / libec / src / ast.ec
index e9aab44..341bd9d 100644 (file)
@@ -81,7 +81,7 @@ public Identifier MkIdentifier(char * string)
          // TODO: Do these better, keep in string?
          if(!strcmp(name, "typed_object"))
          {
-            id._class = MkSpecifierName("class");
+            id._class = MkSpecifierName("typed_object"); //"class");
             id.string = CopyString(namePart);
          }
          else if(!strcmp(name, "property"))
@@ -608,6 +608,91 @@ InitDeclarator MkInitDeclarator(Declarator declarator, Initializer initializer)
 
 public TypeName MkTypeName(OldList qualifiers, Declarator declarator)
 {
+   if(qualifiers != null)
+   {
+      Declarator parentDecl = declarator;
+      Declarator decl = declarator;
+      while(decl && decl.type == arrayDeclarator)
+         decl = decl.declarator;
+      if(decl && decl.type == identifierDeclarator && decl.identifier.string && CheckType(decl.identifier.string) == TYPE_NAME)
+      {
+         Specifier spec;
+         // Check if we're missing a real type specifier here
+         for(spec = qualifiers.first; spec; spec = spec.next)
+         {
+            if(spec.type == baseSpecifier)
+            {
+               if(spec.specifier == CONST || spec.specifier == VOLATILE ||
+                  spec.specifier == EXTERN || spec.specifier == STATIC ||
+                  spec.specifier == AUTO || spec.specifier == REGISTER)
+                  continue;
+               break;
+            }
+            else if(spec.type != extendedSpecifier)
+               break;
+         }
+         if(!spec)
+         {
+            // This is actually a type
+            ListAdd(qualifiers, MkSpecifierName(decl.identifier.string));
+            FreeDeclarator(decl);
+            parentDecl.declarator = null;
+         }
+      }
+   }
+   return { qualifiers = qualifiers, declarator = declarator };
+}
+
+public TypeName MkTypeNameGuessDecl(OldList qualifiers, Declarator declarator)
+{
+   if(qualifiers != null)
+   {
+      bool gotType = false;
+      bool gotFullType = false;
+      Specifier spec, next;
+      for(spec = qualifiers.first; spec; spec = next)
+      {
+         next = 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)
+            {
+               declarator = MkDeclaratorIdentifier(MkIdentifier(s));
+               qualifiers.Remove(spec);
+               FreeSpecifier(spec);
+               spec = null;
+            }
+         }
+         if(spec && spec.type != extendedSpecifier)
+         {
+            if(spec.type == baseSpecifier)
+            {
+               if(spec.specifier == CONST || spec.specifier == VOLATILE ||
+                  spec.specifier == EXTERN || spec.specifier == STATIC ||
+                  spec.specifier == AUTO || spec.specifier == REGISTER)
+                  continue;
+               else if(spec.specifier != UNSIGNED && spec.specifier != SIGNED && spec.specifier != LONG)
+                  gotFullType = true;
+               gotType = true;
+            }
+            else
+            {
+               gotFullType = true;
+               gotType = true;
+            }
+         }
+      }
+   }
    return { qualifiers = qualifiers, declarator = declarator };
 }
 
@@ -716,9 +801,11 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
    
    if(specifiers != null)
    {
-      Specifier spec;
-      for(spec = specifiers.first; spec; spec = spec.next)
+      bool gotType = false;
+      Specifier spec, next;
+      for(spec = specifiers.first; spec; spec = next)
       {
+         next = spec.next;
          if(spec.type == baseSpecifier && spec.specifier == TYPEDEF)
          {
             if(initDeclarators != null)
@@ -744,20 +831,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(s)), null));
+                        specifiers.Remove(spec);
+                        FreeSpecifier(spec);
+                        if(!(curContext.templateTypesOnly ? curContext.parent : curContext).types.Add((BTNode)type))
+                           excludedSymbols->Add(type);
+                     }
                   }
                }
             }
@@ -767,6 +866,31 @@ 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(s)), null));
+                  specifiers.Remove(spec);
+                  FreeSpecifier(spec);
+                  spec = null;
+               }
+            }
+         }
+         if(spec && spec.type != extendedSpecifier)
+            gotType = true;
       }
    }
    if(variable && initDeclarators)
@@ -869,7 +993,39 @@ 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, next;
+      for(spec = specifiers.first; spec; spec = next)
+      {
+         next = 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(MkStructDeclarator(MkDeclaratorIdentifier(MkIdentifier(s)), null));
+               specifiers.Remove(spec);
+               FreeSpecifier(spec);
+               spec = null;
+            }
+         }
+         if(spec && spec.type != extendedSpecifier)
+            gotType = true;
+      }
+   }
+   return decl;
 }
 
 Statement MkLabeledStmt(Identifier id, Statement statement)
@@ -952,6 +1108,35 @@ Statement MkReturnStmt(OldList exp)
 
 FunctionDefinition MkFunction(OldList specifiers, Declarator declarator, OldList declarationList)
 {
+   return _MkFunction(specifiers, declarator, declarationList, true);
+}
+
+FunctionDefinition _MkFunction(OldList specifiers, Declarator declarator, OldList declarationList, bool errorOnOmit)
+{
+   if(errorOnOmit)
+   {
+      Declarator funcDecl = GetFuncDecl(declarator);
+      if(funcDecl && funcDecl.function.parameters)
+      {
+         TypeName tn;
+         for(tn = funcDecl.function.parameters->first; tn; tn = tn.next)
+         {
+            if(tn.qualifiers || tn.declarator)
+            {
+               Identifier declID = tn.declarator ? GetDeclId(tn.declarator) : null;
+               if(!declID)
+               {
+                  // Check for (void)
+                  Specifier spec = tn.qualifiers ? tn.qualifiers->first : null;
+                  if(!tn.declarator && !tn.prev && !tn.next && spec && !spec.next && spec.type == baseSpecifier && spec.specifier == VOID);
+                  else
+                     Compiler_Error("parameter name omitted\n");
+                  break;
+               }
+            }
+         }
+      }
+   }
    return { specifiers = specifiers, declarator = declarator, declarations = declarationList };
 }
 
@@ -1627,7 +1812,7 @@ void SetupBaseSpecs(Symbol symbol, OldList baseSpecs)
       if(tpl) *tpl = 0;
       
       baseClass = FindClass(name);
-      if(baseClass.ctx)
+      if(baseClass && baseClass.ctx)
       {
          TemplatedType copy;
          for(copy = (TemplatedType)baseClass.ctx.templateTypes.first; copy; copy = (TemplatedType)copy.next)
@@ -1637,7 +1822,7 @@ void SetupBaseSpecs(Symbol symbol, OldList baseSpecs)
                delete type;
          }
       }
-      else if(baseClass.registered)
+      else if(baseClass && baseClass.registered)
       {
          Class sClass;
          for(sClass = baseClass.registered; sClass; sClass = sClass.base)
@@ -1726,7 +1911,7 @@ PropertyDef MkProperty(OldList specs, Declarator decl, Identifier id, Statement
    {
       char typeString[1024];
       typeString[0] = '\0';
-      PrintType(type, typeString, false, true);
+      PrintTypeNoConst(type, typeString, false, true);
       id = MkIdentifier(typeString);
       prop.conversion = true;
    }
@@ -1814,7 +1999,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);
    }
@@ -2014,7 +2205,6 @@ void CopyTypeInto(Type type, Type src)
 {
    type = *src;
    type.name = CopyString(src.name);
-   type.enumName = CopyString(src.enumName);
    type.refCount = 1;
 
    if(src.kind == enumType)
@@ -2027,6 +2217,7 @@ void CopyTypeInto(Type type, Type src)
       {
          type.members.Add(NamedLink { name = CopyString(member.name), data = member.data });
       }
+      type.enumName = CopyString(src.enumName);
    }
    else if(src.kind == structType || src.kind == unionType)
    {
@@ -2034,6 +2225,7 @@ void CopyTypeInto(Type type, Type src)
       // Tricky stuff... will be removed from list only when ref count reaches 0
       for(member = type.members.first; member; member = member.next)
          member.refCount++;
+      type.enumName = CopyString(src.enumName);
    }
    else if(src.kind == functionType)
    {
@@ -2054,646 +2246,463 @@ void CopyTypeInto(Type type, Type src)
    }
 }
 
-public Type ProcessType(OldList specs, Declarator decl)
+static Type ProcessTypeSpecs(OldList specs, bool assumeEllipsis, bool keepTypeName)
 {
-   Type type = null;
-   bool isTypedef = false;
-   if(!specs || specs.first)
+   Type specType { refCount = 1, kind = intType, isSigned = true };
+   if(specs != null)
    {
-      Declarator funcDecl = GetFuncDecl(decl);
-      Type specType { };
-      bool dllExport = false;
-
-      specType.kind = intType;
-      specType.isSigned = true;   
-      specType.refCount = 1;
-
-      type = Type { refCount = 1 };
-
-      while(decl && (decl.type == structDeclarator || decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd))
+      bool isTypedef = false;
+      Specifier spec;
+      bool isLong = false;
+      for(spec = specs.first; spec; spec = spec.next)
       {
-         if(decl.type == structDeclarator && decl.structDecl.exp)
+         if(spec.type == extendedSpecifier)
          {
-            ProcessExpressionType(decl.structDecl.exp);
-            ComputeExpression(decl.structDecl.exp);
-            if(decl.structDecl.exp.type == constantExp)
-               specType.bitFieldCount = strtoul(decl.structDecl.exp.constant, null, 0);
+            ExtDecl extDecl = spec.extDecl;
+            if(extDecl.type == extDeclString)
+            {
+               String s = spec.extDecl.s;
+               if(!strcmp(spec.extDecl.s, "__declspec(dllexport)") || !strcmp(spec.extDecl.s, "dllexport"))
+                  specType.dllExport = true;
+               else if(!strcmp(spec.extDecl.s, "__declspec(stdcall)") || !strcmp(spec.extDecl.s, "stdcall"))
+                  specType.attrStdcall = true;
+            }
+            else if(extDecl.type == extDeclAttrib)
+            {
+               OldList * attribs = extDecl.attr.attribs;
+               if(attribs)
+               {
+                  Attribute attr;
+                  for(attr = attribs->first; attr; attr = attr.next)
+                  {
+                     String s = attr.attr;
+                     if(s)
+                     {
+                        if(!strcmp(s, "dllexport"))
+                           specType.dllExport = true;
+                        else if(!strcmp(s, "stdcall"))
+                           specType.attrStdcall = true;
+                     }
+                  }
+               }
+               specType.keepCast = true;
+            }
          }
-         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")))
+
+         if(spec.specifier != CONST && (specType.kind == structType || specType.kind == unionType))
          {
-            dllExport = true;
+            FreeType(specType);
+            specType = { kind = intType, isSigned = true, refCount = 1 };
          }
-         if((decl.type == extendedDeclarator || decl.type == extendedDeclaratorEnd) && decl.extended.extended && decl.extended.extended.type == extDeclAttrib)
+
+         if(isTypedef && keepTypeName)
          {
-            specType.keepCast = true;
+            specType.kind = dummyType;
+            return specType;
          }
-         decl = decl.declarator;
-      }
-
-      // If we'll be using the specType
-      if(funcDecl || !decl || decl.type == identifierDeclarator)
-      {
-         Specifier spec;
-         if(specs != null)
+         else if(spec.type == baseSpecifier)
          {
-            bool isLong = false;
-            for(spec = specs.first; spec; spec = spec.next)
+            if(spec.specifier == TYPEDEF)
+               isTypedef = true;
+            else if(spec.specifier == VOID) specType.kind = voidType;
+            else if(spec.specifier == CHAR) specType.kind = charType;
+            else if(spec.specifier == INT) { if(specType.kind != shortType && specType.kind != longType && !isLong) specType.kind = intType; }
+            else if(spec.specifier == UINT) { if(specType.kind != shortType && specType.kind != longType) specType.kind = intType; specType.isSigned = false; }
+            else if(spec.specifier == INT64) specType.kind = int64Type;
+            else if(spec.specifier == VALIST) 
+               specType.kind = vaListType;
+            else if(spec.specifier == SHORT) specType.kind = shortType;
+            else if(spec.specifier == LONG) 
             {
-               if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && spec.extDecl.s && (!strcmp(spec.extDecl.s, "__declspec(dllexport)") || !strcmp(spec.extDecl.s, "dllexport")))
+               if(isLong || (targetBits == 64 && targetPlatform != win32))
+                  specType.kind = int64Type;
+               else
+                  specType.kind = intType;
+               isLong = true;
+            }
+            else if(spec.specifier == FLOAT) specType.kind = floatType;
+            else if(spec.specifier == DOUBLE) specType.kind = doubleType;
+            else if(spec.specifier == SIGNED) specType.isSigned = true;
+            else if(spec.specifier == UNSIGNED) specType.isSigned = false;
+            else if(spec.specifier == CONST)
+               specType.constant = true;
+            else if(spec.specifier == TYPED_OBJECT || spec.specifier == ANY_OBJECT || spec.specifier == CLASS)
+            { 
+               switch(spec.specifier)
                {
-                  dllExport = true;
+                  case TYPED_OBJECT:   specType.classObjectType = typedObject;   break;
+                  case ANY_OBJECT:     specType.classObjectType = anyObject;     break;
+                  case CLASS:          specType.classObjectType = classPointer;  break;
                }
-               if(spec.type == extendedSpecifier && spec.extDecl.type == extDeclAttrib)
+               specType.kind = classType;
+               specType._class = FindClass("class");
+            }
+            else if(spec.specifier == THISCLASS)
+               specType.kind = thisClassType;
+         }
+         else if(spec.type == nameSpecifier)
+         {
+            if(spec.name && (!strcmp(spec.name, "intptr") || !strcmp(spec.name, "uintptr")))
+            {
+               specType.kind = intPtrType;
+               if(!strcmp(spec.name, "uintptr"))
+                  specType.isSigned = false;
+            }
+            else if(spec.name && (!strcmp(spec.name, "uintsize") || !strcmp(spec.name, "intsize")))
+            {
+               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)
                {
-                  specType.keepCast = true;
-               }
+                  // Free Type Contents:
+                  Type dummy { };
+                  *dummy = *specType;
+                  FreeType(dummy);
 
-               if(spec.specifier != CONST && (specType.kind == structType || specType.kind == unionType))
+                  CopyTypeInto(specType, symbol.type);
+                  specType.typeName = CopyString(symbol.type.name);
+               }
+               else if(!isTypedef) // !specType.kind)    // TESTING THIS FOR enum / typedef problem
                {
-                  FreeType(specType);
-                  specType = { kind = intType, isSigned = true, refCount = 1 };
+                  // key.sym enum values need FindClass:
+                  specType._class = spec.name ? FindClass(spec.name) : null;
+                  specType.kind = classType;
+                  if(!specType._class)
+                     specType.kind = intType;
                }
+            }
+         }
+         else if(spec.type == enumSpecifier)
+         {
+            specType.kind = enumType;
+            specType.enumName = spec.id ? CopyString(spec.id.string) : null;
 
-               if(spec.type == baseSpecifier)
+            if(spec.list)
+            {
+               Enumerator e;
+               int nextValue = 0;
+               for(e = spec.list->first; e; e = e.next)
                {
-                  if(spec.specifier == TYPEDEF) isTypedef = true;
-                  else if(spec.specifier == VOID) specType.kind = voidType;
-                  else if(spec.specifier == CHAR) specType.kind = charType;
-                  else if(spec.specifier == INT) { if(specType.kind != shortType && specType.kind != longType) specType.kind = intType; }
-                  else if(spec.specifier == UINT) { if(specType.kind != shortType && specType.kind != longType) specType.kind = intType; specType.isSigned = false; }
-                  else if(spec.specifier == INT64) specType.kind = int64Type;
-                  else if(spec.specifier == VALIST) 
-                     specType.kind = vaListType;
-                  else if(spec.specifier == SHORT) specType.kind = shortType;
-                  else if(spec.specifier == LONG) 
-                  {
-                     if(isLong)
-                        specType.kind = int64Type;
-                     else
-                        specType.kind = intType;
-                     isLong = true;
-                     // specType.kind = longType;
-                  }
-                  else if(spec.specifier == FLOAT) specType.kind = floatType;
-                  else if(spec.specifier == DOUBLE) specType.kind = doubleType;
-                  else if(spec.specifier == SIGNED) specType.isSigned = true;
-                  else if(spec.specifier == UNSIGNED) specType.isSigned = false;
-                  else if(spec.specifier == CONST) specType.constant = true;
-                  else if(spec.specifier == TYPED_OBJECT) 
-                  { 
-                     specType.classObjectType = typedObject; specType.kind = classType; specType._class = FindClass("class"); 
-                  }
-                  else if(spec.specifier == ANY_OBJECT) 
-                  { 
-                     specType.classObjectType = anyObject; specType.kind = classType; specType._class = FindClass("class"); 
-                  }
-                  else if(spec.specifier == CLASS)
-                  {
-                     specType.classObjectType = classPointer; specType.kind = classType; specType._class = FindClass("class");
-                  }
-                  else if(spec.specifier == THISCLASS)
-                     specType.kind = thisClassType;
+                  // TOFIX: NamedItem i { } causes cryptic error, bad .c!
+                  NamedLink i { name = CopyString(e.id.string) };
+                  specType.members.Add(i);
                }
-               else if(spec.type == nameSpecifier)
+            }
+         }
+         else if(spec.type == templateTypeSpecifier)
+         {
+            specType.kind = templateType;
+            specType.templateParameter = spec.templateParameter;
+         }
+         else if(spec.type == structSpecifier || spec.type == unionSpecifier)
+         {
+            Symbol _class = spec.id ? FindClass(spec.id.string) : null;
+            if(_class)
+            {
+               specType.declaredWithStruct = true;
+               if(!_class.registered || _class.registered.type != structClass)
+                  specType.directClassAccess = true;     // TODO: Need to clarify what 'directClassAccess' is about
+               specType._class = _class;
+               specType.kind = classType;
+               break;
+            }
+            specType.members.Clear();
+            if(spec.type == structSpecifier)
+               specType.kind = structType;
+            else if(spec.type == unionSpecifier)
+               specType.kind = unionType;
+            if(spec.id)
+            {
+               // TESTING THIS HERE... Had 0 type size 
+               if(!spec.definitions && !isTypedef)
                {
-                  Symbol symbol = spec.name ? FindType(curContext, spec.name) : null;
+                  Symbol symbol = spec.id.string ? FindSymbol(spec.id.string, curContext, globalContext, true, false) : null;
                   if(symbol && symbol.type)
                   {
-                     // Free Type Contents:
-                     Type dummy { };
-                     *dummy = *specType;
-                     FreeType(dummy);
+                     specType = *symbol.type;
+                     specType.name = CopyString(symbol.type.name);
+                     specType.typeName = CopyString(spec.name);
+                     specType.enumName = CopyString(symbol.type.enumName);
+                     specType.refCount = 1;
 
-                     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)
-               {
-
-                  specType.kind = enumType;
-                  specType.enumName = spec.id ? CopyString(spec.id.string) : null;
-
-                  if(spec.list)
-                  {
-                     Enumerator e;
-                     int nextValue = 0;
-                     for(e = spec.list->first; e; e = e.next)
+                     if(symbol.type.kind == enumType)
                      {
-                        // TOFIX: NamedItem i { } causes cryptic error, bad .c!
-                        NamedLink i { name = CopyString(e.id.string) };
-                        specType.members.Add(i);
-                        /*
-                        if(e.exp && ComputeExpression(e.exp), e.exp.isConstant && e.exp.expType.kind == intType)
-                           value.data = (void *) nextValue = strtol(e.exp.string, null, 0);
-                        else
-                           value.data = (void *)nextValue++;
-                        */
-                     }
-                  }
-                  /*
-                  if(spec.list)
-                  {
-                     Declaration decl;
-                     for(enumerator = spec.list->first; enumerator; enumerator = enumerator.next)
-                        if(decl.declarators)
+                        NamedLink member;
+
+                        specType.members.Clear();
+                        for(member = symbol.type.members.first; member; member = member.next)
                         {
-                           Declarator d;
-                           for(d = decl.declarators.first; d; d = d.next)
-                           {
-                              Type memberType = ProcessType(decl.specifiers, d);
-                              specType.members.Add(memberType);
-                           }
+                           NamedLink item { name = CopyString(member.name), data = member.data };
+                           specType.members.Add(item);
                         }
-                        else if(decl.specifiers)
+                     }
+                     else if(symbol.type.kind == structType || symbol.type.kind == unionType)
+                     {
+                        Type member;
+                        // Tricky stuff... will be removed from list only when ref count reaches 0
+                        for(member = specType.members.first; member; member = member.next)
+                           member.refCount++;
+                     }
+                     else if(symbol.type.kind == functionType)
+                     {
+                        Type param;
+                        specType.returnType.refCount++;
+                        for(param = specType.params.first; param; param = param.next)
+                           param.refCount++;
+                     }
+                     else if(symbol.type.kind == pointerType || symbol.type.kind == arrayType)
+                     {
+                        specType.type.refCount++;
+                        if(symbol.type.kind == arrayType)
                         {
-                           Type memberType = ProcessType(decl.specifiers, null);
-                           specType.members.Add(memberType);
+                           if(specType.arraySizeExp)
+                              specType.arraySizeExp = CopyExpression(specType.arraySizeExp);
                         }
+
+                     }
                   }
-                  */
-               }
-               else if(spec.type == templateTypeSpecifier)
-               {
-                  /*
-                  printf("spec %x\n", spec);
-                  printf("template param %x\n", spec.templateParameter);
-                  printf("identifier %x\n", spec.templateParameter.identifier);
-                  printf("string %x\n", spec.templateParameter.identifier.string);
-                  */
-                  specType.kind = templateType;
-                  specType.templateParameter = spec.templateParameter;
+                  else
+                     specType.enumName = CopyString(spec.id.string);
                }
-               else if(spec.type == structSpecifier || spec.type == unionSpecifier)
+               else
+                  specType.enumName = CopyString(spec.id.string);
+            }
+
+            if(spec.definitions)
+            {
+               ClassDef def;
+               for(def = spec.definitions->first; def; def = def.next)
                {
-                  Symbol _class = spec.id ? FindClass(spec.id.string) : null;
-                  if(_class)
-                  {
-                     if(!_class.registered || _class.registered.type != structClass)
-                        specType.directClassAccess = true;
-                     specType._class = _class;
-                     specType.kind = classType;
-                     break;
-                  }
-                  if(spec.type == structSpecifier)
-                     specType.kind = structType;
-                  else if(spec.type == unionSpecifier)
-                     specType.kind = unionType;
-                  if(spec.id)
+                  if(def.type == declarationClassDef && def.decl.type == structDeclaration)
                   {
-                     // TESTING THIS HERE... Had 0 type size 
-                     if(!spec.definitions && !isTypedef)
+                     Declaration decl = def.decl;
+                     if(decl.declarators)
                      {
-                        Symbol symbol = spec.id.string ? FindSymbol(spec.id.string, curContext, globalContext, true, false) : null;
-                        if(symbol && symbol.type)
+                        Declarator d;
+                        for(d = decl.declarators->first; d; d = d.next)
                         {
-                           specType = *symbol.type;
-                           specType.name = CopyString(symbol.type.name);
-                           specType.typeName = CopyString(spec.name);
-                           specType.enumName = CopyString(symbol.type.enumName);
-                           specType.refCount = 1;
-
-                           if(symbol.type.kind == enumType)
-                           {
-                              NamedLink member;
-
-                              specType.members.Clear();
-                              for(member = symbol.type.members.first; member; member = member.next)
-                              {
-                                 NamedLink item { name = CopyString(member.name), data = member.data };
-                                 specType.members.Add(item);
-                              }
-                           }
-                           else if(symbol.type.kind == structType || symbol.type.kind == unionType)
-                           {
-                              Type member;
-                              // Tricky stuff... will be removed from list only when ref count reaches 0
-                              for(member = specType.members.first; member; member = member.next)
-                                 member.refCount++;
-                           }
-                           else if(symbol.type.kind == functionType)
-                           {
-                              Type param;
-                              specType.returnType.refCount++;
-                              for(param = specType.params.first; param; param = param.next)
-                                 param.refCount++;
-                           }
-                           else if(symbol.type.kind == pointerType || symbol.type.kind == arrayType)
-                           {
-                              specType.type.refCount++;
-                              if(symbol.type.kind == arrayType)
-                              {
-                                 if(specType.arraySizeExp)
-                                    specType.arraySizeExp = CopyExpression(specType.arraySizeExp);
-                              }
-
-                           }
+                           Type memberType = ProcessType(decl.specifiers, d);
+                           specType.members.Add(memberType);
                         }
-                        else
-                           specType.enumName = CopyString(spec.id.string);
                      }
-                     else
-                        specType.enumName = CopyString(spec.id.string);
-                  }
-
-                  if(spec.definitions)
-                  {
-                     ClassDef def;
-                     for(def = spec.definitions->first; def; def = def.next)
+                     else if(decl.specifiers)
                      {
-                        if(def.type == declarationClassDef && def.decl.type == structDeclaration)
-                        {
-                           Declaration decl = def.decl;
-                           if(decl.declarators)
-                           {
-                              Declarator d;
-                              for(d = decl.declarators->first; d; d = d.next)
-                              {
-                                 Type memberType = ProcessType(decl.specifiers, d);
-                                 specType.members.Add(memberType);
-                              }
-                           }
-                           else if(decl.specifiers)
-                           {
-                              Type memberType = ProcessType(decl.specifiers, null);
-                              specType.members.Add(memberType);
-                           }
-                        }
+                        Type memberType = ProcessType(decl.specifiers, null);
+                        specType.members.Add(memberType);
                      }
                   }
-                  break;
                }
-               else if(spec.type == subClassSpecifier)
-               {
-                  specType.kind = specType.kind = subClassType;
-                  specType._class = spec._class.symbol; // FindClass(spec._class.name);
-               }
-               /*
-               else if(spec.type == classSpecifier)
-               {
-                  specType._class = FindClass(spec.name);
-                  specType.kind = classType;
-               }
-               */
             }
+            break;
+         }
+         else if(spec.type == subClassSpecifier)
+         {
+            specType.kind = specType.kind = subClassType;
+            specType._class = spec._class.symbol;
          }
-         else if(!decl)
-            specType.kind = ellipsisType;
       }
+   }
+   else if(assumeEllipsis)
+      specType.kind = ellipsisType;
+   return specType;     
+}
 
-      if(funcDecl)
+static Type ProcessTypeDecls(OldList specs, Declarator decl, Type parentType)
+{
+   Type type = parentType;
+   Declarator subDecl = decl ? decl.declarator : null;
+   if(!parentType)
+      type = ProcessTypeSpecs(specs, decl == null, (decl && decl.type == extendedDeclaratorEnd) ? true : false);
+   if(decl)
+   {
+      switch(decl.type)
       {
-         Declarator d = funcDecl.declarator;
-         Type funcType { };
-         TypeName param;
-
-         funcType.kind = functionType;
-         funcType.refCount = 1;
-         if(funcDecl.function.parameters)
+         case bracketsDeclarator: break;
+         case extendedDeclarator:
+         case extendedDeclaratorEnd:
          {
-            for(param = funcDecl.function.parameters->first; param; param = param.next)
+            ExtDecl extDecl = decl.extended.extended;
+            if(extDecl)
             {
-               /*
-               if(param.typedObject)
+               switch(extDecl.type)
                {
-                  Type typedObjectType
+                  case extDeclString:
                   {
-                     refCount = 1;
-                     byReference = param.byReference;
-                     kind = TypeTypedObject;
-                  };
-                  funcType.params.Add(typedObjectType);
+                     String s = extDecl.s;
+                     if(s)
+                     {
+                        if(!strcmp(s, "__declspec(dllexport)") || !strcmp(s, "dllexport"))
+                           type.dllExport = true;
+                        else if(!strcmp(s, "__declspec(stdcall)") || !strcmp(s, "stdcall"))
+                           type.attrStdcall = true;
+                     }
+                     break;
+                  }
+                  case extDeclAttrib:
+                  {
+                     OldList * attribs = extDecl.attr.attribs;
+                     if(attribs)
+                     {
+                        Attribute attr;
+                        for(attr = attribs->first; attr; attr = attr.next)
+                        {
+                           String s = attr.attr;
+                           if(s)
+                           {
+                              if(!strcmp(s, "dllexport"))
+                                 type.dllExport = true;
+                              else if(!strcmp(s, "stdcall"))
+                                 type.attrStdcall = true;
+                           }
+                        }
+                     }
+                     type.keepCast = true;
+                     break;
+                  }
                }
-               else*/
-                  funcType.params.Add(ProcessType(param.qualifiers, param.declarator));
             }
+            break;
          }
-
-         // Function returning a pointer...
-         if(decl.type == pointerDeclarator)
+         case structDeclarator:
          {
-            Pointer pointer = decl.pointer.pointer;
-            Type ptrType { };
-            funcType.returnType = ptrType;
-            funcType.returnType.refCount = 1;
-            while(pointer)
+            Expression exp = decl.structDecl.exp;
+            if(exp)
             {
-               ptrType.kind = pointerType;
-               pointer = pointer.pointer;
-               if(pointer)
-               {
-                  ptrType.type = Type { refCount = 1 };
-                  ptrType = ptrType.type;
-               }
+               ProcessExpressionType(exp);
+               ComputeExpression(exp);
+               if(exp.type == constantExp)
+                  type.bitFieldCount = (uint)strtoul(exp.constant, null, 0);
             }
-            ptrType.type = Type { refCount = 1 };
-            *ptrType.type = specType;
-         }
-         else
-         {
-            funcType.returnType = Type { refCount = 1 };
-            *funcType.returnType = specType;
+            break;
          }
-
-         // TESTING: Added extendedDeclarator here
-         while(d && (d.type == bracketsDeclarator || d.type == extendedDeclarator || d.type == extendedDeclaratorEnd))
+         case functionDeclarator:
          {
-            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")))
+            type = { refCount = 1, kind = functionType, returnType = type, dllExport = type.dllExport, attrStdcall = type.attrStdcall };
+            if(decl.function.parameters)
             {
-               dllExport = true;            
+               TypeName param;
+               for(param = decl.function.parameters->first; param; param = param.next)
+                  type.params.Add(ProcessType(param.qualifiers, param.declarator));
             }
-            d = d.declarator;
+            break;
          }
-
-         funcType.dllExport = dllExport;
-
-         if(d && d.type == pointerDeclarator)
+         case arrayDeclarator:
          {
-            Type ptrType;
-            Identifier id;
-
-            if(d.declarator && d.declarator.type == arrayDeclarator)
+            type = { refCount = 1, kind = arrayType, arraySizeExp = CopyExpression(decl.array.exp), freeExp = true, type = type, dllExport = type.dllExport, attrStdcall = type.attrStdcall };
+            if(decl.array.enumClass)
+               type.enumClass = decl.array.enumClass.symbol; 
+            break;
+         }
+         case pointerDeclarator:
+         {
+            Pointer pointer = decl.pointer.pointer;
+            while(pointer)
             {
-               // Arrays of pointers to functions (extremely tricky :()
-               Pointer pointer = d.pointer.pointer;
-
-               // TO WORK ON: Fixed the order for the array...
-               type.kind = arrayType;
-               type.arraySizeExp = CopyExpression(d.declarator.array.exp);
-               type.freeExp = true;
-               if(d.declarator.array.enumClass)
-                  type.enumClass = d.declarator.array.enumClass.symbol; // FindClass(d.declarator.array.enumClass.name);
-               if(d.declarator.declarator && d.declarator.declarator.type == arrayDeclarator)
-               {
-                  Type tmpType = type;
-                  Type inType;
-                  type = ProcessType(null, d.declarator.declarator);
-                  inType = type.type;
-                  type.type = tmpType;
-                  tmpType.type = inType;
-               }
+               OldList * qualifiers = pointer.qualifiers;
+               if(type.classObjectType)
+                  type.byReference = true;
                else
-                  type.type = ProcessType(null, d.declarator.declarator);
-
-               for(ptrType = type.type; ptrType && ptrType.kind && ptrType.type; ptrType = ptrType.type);
-
-               while(pointer)
-               {
-                  ptrType.kind = pointerType;
-                  pointer = pointer.pointer;
-                  if(pointer)
-                  {
-                     ptrType.type = Type { refCount = 1 };
-                     ptrType = ptrType.type;
-                  }
-               }
-               ptrType.type = ProcessType(specs, null);
-            }
-            else
-            {
-               // WARNING: Not caring if this declarator contains a declarator between
-               //          the pointer and the function other than brackets (like in the case of array of pointers to functions)...
-               // *********** Could it ever go in here???  Yes: void (* converters_table[10]) (); ***********
-               Pointer pointer = d.pointer.pointer;
-
-               ptrType = type;
-               while(pointer)
-               {
-                  ptrType.kind = pointerType;
-                  ptrType.type = Type { refCount = 1 };
-                  pointer = pointer.pointer;
-                  if(pointer)
-                     ptrType = ptrType.type;
-               }
-            }
-
-            *ptrType.type = funcType;
-            id = GetDeclId(d);
-            if(id)
-            {
-               if(id._class && !id._class.name)
-                  ptrType.type.staticMethod =  true;
-               else 
+                  type = { refCount = 1, kind = pointerType, type = type, dllExport = type.dllExport, attrStdcall = type.attrStdcall };
+               if(qualifiers)
                {
-                  // TODO : Ensure classSym has been resolved here... (Is this gonna cause problems? Supposed to do this later...)
-                  if(!id.classSym)
-                  {
-                     if(id._class && id._class.name)
-                     {
-                        id.classSym = id._class.symbol; // FindClass(id._class.name);
-                        /* TODO: Name Space Fix ups
-                        if(!id.classSym)
-                           id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
-                        */
-                     }
-                  }
-
-                  ptrType.type.thisClass = id.classSym;
-                  if(ptrType.type.thisClass && strcmp(ptrType.type.thisClass.string, "class"))
-                     ptrType.type.extraParam = true;
-                  else if(id._class && id._class.name && !strcmp(id._class.name, "any_object"))
+                  Specifier spec;
+                  for(spec = qualifiers->first; spec; spec = spec.next)
                   {
-                     ptrType.type.extraParam = true;
-                     ptrType.type.thisClass = FindClass("class");
+                     if(spec.type == baseSpecifier && spec.specifier == CONST)
+                        type.constant = true;
                   }
                }
-
-               type.name = CopyString(id.string);
+               pointer = pointer.pointer;
             }
+            break;
          }
-         else if(!d || d.type == identifierDeclarator)
+         case identifierDeclarator:
          {
-
-            *type = funcType;
-            if(d)
+            Identifier id = decl.identifier;
+            Specifier _class = id._class;
+            delete type.name;
+            type.name = CopyString(id.string);
+            if(_class)
             {
-               if(d.identifier._class && d.identifier._class.type == templateTypeSpecifier)
+               if(_class.type == templateTypeSpecifier)
                {
-                  type.thisClassTemplate = d.identifier._class.templateParameter;
+                  type.thisClassTemplate = _class.templateParameter;
                   type.extraParam = true;
                }
                else
                {
-                  if(d.identifier._class && !d.identifier._class.name)
+                  String name = _class.name;
+                  if(!name)
                      type.staticMethod = true;
                   else
                   {
-                     if(d.identifier._class && d.identifier._class.name && d.identifier._class.name[strlen(d.identifier._class.name)-1] == '&')
+                     if(!id.classSym)
+                        id.classSym = _class.symbol; // FindClass(_class.name);
+                     /* TODO: Name Space Fix ups
+                        id.nameSpace = eSystem_FindNameSpace(privateModule, _class.name);
+                     */
+
+                     if(name[strlen(name)-1] == '&')
                      {
                         type.thisClass = FindClass("class");
                         type.byReference = true;
                      }
                      else
-                        type.thisClass = d.identifier._class ? d.identifier._class.symbol /*FindClass(d.identifier._class.name)*/ : null;
+                        type.thisClass = _class.symbol;
+
                      if(type.thisClass && strcmp(type.thisClass.string, "class"))
+                        type.extraParam = true;
+                     else if(!strcmp(name, "any_object"))
                      {
                         type.extraParam = true;
+                        type.thisClass = FindClass("class");
                      }
-                     else if(d.identifier._class && d.identifier._class.name && !strcmp(d.identifier._class.name, "any_object"))
+                     else if(!strcmp(name, "class"))
                      {
-                        type.extraParam = true;
                         type.thisClass = FindClass("class");
+                        type.classObjectType = classPointer;   // This is used for class properties
                      }
-                     else if(d.identifier._class && d.identifier._class.name && !strcmp(d.identifier._class.name, "class"))
+                     else if(!strcmp(name, "typed_object") || !strcmp(name, "typed_object&"))
                      {
-                        //type.extraParam = true;
                         type.thisClass = FindClass("class");
-                        type.classObjectType = classPointer;
+                        type.classObjectType = typedObject;
                      }
                   }
                }
-               type.name = CopyString(d.identifier.string);
             }
+            break;
          }
-         delete funcType;
-      }
-      else if(decl && decl.type == pointerDeclarator)
-      {
-         if(decl.declarator && decl.declarator.type == arrayDeclarator)
-         {
-            // Arrays of pointers (tricky :))
-            Identifier id;
-            Pointer pointer = decl.pointer.pointer;
-            Type ptrType;
-
-            // TO WORK ON: Fixed the order for the array...
-            type.kind = arrayType;
-            type.arraySizeExp = CopyExpression(decl.declarator.array.exp);
-            type.freeExp = true;
-            if(decl.declarator.array.enumClass)
-               type.enumClass = decl.declarator.array.enumClass.symbol; // FindClass(decl.declarator.array.enumClass.name);
-            if(decl.declarator.declarator && decl.declarator.declarator.type == arrayDeclarator)
-            {
-               Type tmpType = type;
-               Type inType;
-               type = ProcessType(null, decl.declarator.declarator);
-               inType = type.type;
-               type.type = tmpType;
-               tmpType.type = inType;
-            }
-            else
-               type.type = ProcessType(null, decl.declarator.declarator);
-            /*
-            type.type = ProcessType(null, decl.declarator.declarator);
-            type.kind = arrayType;
-            type.arraySizeExp = CopyExpression(decl.declarator.array.exp);
-            type.arraySizeExp.freeExp = true;
-            if(decl.array.enumClass)
-               type.enumClass = FindClass(decl.array.enumClass.name);
-            */
-
-            for(ptrType = type.type; ptrType && ptrType.kind && ptrType.type; ptrType = ptrType.type);
-
-            while(pointer)
-            {
-               ptrType.kind = pointerType;
-               pointer = pointer.pointer;
-               if(pointer)
-               {
-                  ptrType.type = Type { refCount = 1 };
-                  ptrType = ptrType.type;
-               }
-            }
-            ptrType.type = ProcessType(specs, null);
-            id = GetDeclId(decl);
-            if(id) type.name = CopyString(id.string);
-         }
-         else
-         {
-            Identifier id;
-            Pointer pointer = decl.pointer.pointer;
-            Type ptrType = type;
-
-            if(type.classObjectType)
-            {
-               type.byReference = true;
-            }
-            else
-            {
-               while(pointer)
-               {
-                  ptrType.kind = pointerType;
-                  pointer = pointer.pointer;
-                  if(pointer)
-                  {
-                     ptrType.type = Type { refCount = 1 };
-                     ptrType = ptrType.type;
-                  }
-               }
-               ptrType.type = ProcessType(specs, decl.declarator);
-
-               if(type.type.classObjectType)
-               {
-                  Type subType = type.type;
-                  type.classObjectType = subType.classObjectType;
-                  type.kind = subType.kind;
-                  type._class = subType._class;
-                  type.byReference = true;
-
-                  FreeType(subType);
-               }
-               id = GetDeclId(decl);
-               if(id) type.name = CopyString(id.string);
-            }
-         }
-      }
-      else if(decl && decl.type == arrayDeclarator)
-      {
-         Identifier id;
-
-         type.kind = arrayType;
-         
-         type.arraySizeExp = CopyExpression(decl.array.exp);
-         type.freeExp = true;
-         if(decl.array.enumClass)
-            type.enumClass = decl.array.enumClass.symbol; // FindClass(decl.array.enumClass.name);
-         id = GetDeclId(decl);
-
-         // TO WORK ON: Fixed the order for the array...
-         if(decl.declarator && decl.declarator.type == arrayDeclarator)
-         {
-            Type tmpType = type;
-            Type inType;
-            type = ProcessType(specs, decl.declarator);
-            inType = type.type;
-            type.type = tmpType;
-            tmpType.type = inType;
-         }
-         else
-            type.type = ProcessType(specs, decl.declarator);
-
-         if(id)
-         {
-            delete type.name;
-            type.name = CopyString(id.string);
-         }
+         default:
+            PrintLn("Unhandled Declarator Type: ", decl.type);
       }
-      else
+   }
+   if(subDecl)
+   {
+      Type curType = type;
+      type = ProcessTypeDecls(null, subDecl, type);
+      if(curType && type.kind != functionType)
       {
-         if(!decl || decl.type == identifierDeclarator)
-         {
-            *type = specType;
-            delete type.name;
-            type.name = decl ? CopyString(decl.identifier.string) : null;
-
-         }   
+         curType.thisClassTemplate = type.thisClassTemplate;
+         curType.extraParam = type.extraParam;
+         curType.staticMethod = type.staticMethod;
+         curType.thisClass = type.thisClass;
+         curType.byReference = type.byReference;
+         curType.classObjectType = type.classObjectType;
       }
-      delete specType;
    }
    return type;
 }
 
+public Type ProcessType(OldList specs, Declarator decl)
+{
+   return ProcessTypeDecls(specs, decl, null);
+}
+
 public Type ProcessTypeString(char * string, bool staticMethod)
 {
    OldList * specs = MkList();
@@ -2942,6 +2951,7 @@ public void OutputIntlStrings()
 
 default extern OldList * ast;
 default extern int yyparse ();
+default extern int yylex ();
 
 public void SetAST(OldList * list) { ast = list; }
 public OldList * GetAST() { return ast; }
@@ -2949,3 +2959,13 @@ public void ParseEc()
 {
    yyparse();
 }
+
+public int LexEc()
+{
+   return yylex();
+}
+
+public const char * GetYYText()
+{
+   return yytext;
+}