sdk: const correctness
[sdk] / compiler / libec / src / pass1.ec
index bb32546..6578629 100644 (file)
@@ -62,13 +62,13 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
 
       //if(inCompiler)
       {
-         /*FunctionDefinition*/ function = MkFunction(func.specifiers, func.declarator, null);
+         /*FunctionDefinition*/ function = _MkFunction(func.specifiers, func.declarator, null, false);
          function.propSet = func.propSet;
          function.type = func.type;
          if(func.type) func.type.refCount++;
          ProcessFunctionBody(function, func.body);
          external = MkExternalFunction(function);
-         
+
          external.symbol = func.declarator.symbol;
          external.function._class = func._class;
       }
@@ -77,14 +77,14 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
       //symbol.id = func.id; //symid;
       symbol.id = symid;
       symbol.idCode = symidCode;
-      
+
       if(!func.dontMangle)
       {
          // Normal method
          Method method = func.declarator.symbol.method;
          func.declarator.symbol.methodExternal = external;
 
-         // DANGER: Added this one... 
+         // DANGER: Added this one...
 
          if(method && method.symbol)
             //((Symbol)method.symbol).methodExternal = external;
@@ -94,7 +94,7 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
          {
             Type methodDataType;
             ProcessMethodType(method);
-            
+
             methodDataType = method.dataType;
 
             // Do type checking here of type against method.dataType
@@ -135,7 +135,7 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
             }
 
             yylloc = func.loc;
-            if(!MatchTypes(type, methodDataType, null, owningClass, method._class /*owningClass*/, true, true, true, false))
+            if(!MatchTypes(type, methodDataType, null, owningClass, method._class /*owningClass*/, true, true, true, false, true))
             {
 #ifdef _DEBUG
                // MatchTypes(type, methodDataType, null, owningClass, owningClass, true, true, true, false);
@@ -147,11 +147,11 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
                // Mark typed_object/any_object parameters as such according to method type
                Type typeParam;
                Declarator funcDecl = GetFuncDecl(func.declarator);
-               if(funcDecl.function.parameters)
+               if(funcDecl.function.parameters && funcDecl.function.parameters->first)
                {
                   TypeName param = funcDecl.function.parameters->first;
 
-                  for(typeParam = methodDataType.params.first; typeParam; typeParam = typeParam.next)
+                  for(typeParam = methodDataType.params.first; typeParam && param; typeParam = typeParam.next)
                   {
                      if(typeParam.classObjectType)
                      {
@@ -159,7 +159,7 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
                         if(param.declarator && param.declarator.symbol)
                            param.declarator.symbol.type.classObjectType = typeParam.classObjectType;
                      }
-                     param = param.next;
+                     param = param ? param.next : null;
                   }
                }
             }
@@ -213,7 +213,7 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
          {
             propSymbol.externalIsSet = external;
          }
-         else 
+         else
          {
             // Constructor destructor
             //symbol.type = Type { };
@@ -234,6 +234,8 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
             Method method = func.declarator.symbol.method;
             if(method && method.symbol)
                ((Symbol)method.symbol).methodCodeExternal = null;
+            if(func.declarator.symbol && func.declarator.symbol.methodExternal == external)
+               func.declarator.symbol.methodExternal = null;
 
             //external.function = null;
             func.declarator = null;
@@ -246,7 +248,7 @@ External ProcessClassFunction(Class owningClass, ClassFunction func, OldList def
       {
          globalContext /*curContext*/.symbols.Remove((BTNode)symbol);
          excludedSymbols->Add(symbol);
-         
+
          // Testing this...
          defs.Insert(after, external);
 
@@ -271,34 +273,32 @@ void CreateRegisterModuleBody()
       registerModuleBody.compound.context = Context { parent = globalContext };
 
       #if 0
-      ListAdd(registerModuleBody.compound.declarations, 
+      ListAdd(registerModuleBody.compound.declarations,
          MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null) /*MkSpecifierName("ecere::com::Class")*/ /*MkStructOrUnion(SpecifierStruct, MkIdentifier("Class"), null)*/),
                        MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class"))), null))));
       #endif
-      ListAdd(registerModuleBody.compound.declarations, 
+      ListAdd(registerModuleBody.compound.declarations,
          MkDeclaration(MkListOne(MkSpecifierName("ecere::com::Class")),
                        MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("class")), null))));
 
       specifiers = MkList();
       ListAdd(specifiers, MkSpecifier(VOID));
 
-      moduleParam = MkTypeName(MkListOne(/*MkSpecifier(VOID)*/MkSpecifierName("Module")), 
-         //MkDeclaratorPointer(MkPointer(null, null), 
+      moduleParam = MkTypeName(MkListOne(/*MkSpecifier(VOID)*/MkSpecifierName("Module")),
+         //MkDeclaratorPointer(MkPointer(null, null),
          MkDeclaratorIdentifier(MkIdentifier("module")));
 
       GetLastDirectory(outputFile, moduleName);
       //strcpy(moduleName, argv[2]);
       StripExtension(moduleName);
-      ChangeCh(moduleName, '.', '_');
-      ChangeCh(moduleName, ' ', '_');
-      ChangeCh(moduleName, '-', '_');
+      FixModuleName(moduleName);
       sprintf(registerName, "__ecereRegisterModule_%s", moduleName);
 
       declarator = MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(registerName)),
          MkListOne(moduleParam));
 
       {
-         FunctionDefinition function = MkFunction(specifiers, declarator, null);
+         FunctionDefinition function = _MkFunction(specifiers, declarator, null, false);
          ProcessFunctionBody(function, registerModuleBody);
          function.declMode = defaultAccess;
          if(!ast) ast = MkList();
@@ -308,7 +308,7 @@ void CreateRegisterModuleBody()
 
    // Unregister Module
    if(!unregisterModuleBody && inCompiler)
-   {      
+   {
       char registerName[1024], moduleName[MAX_FILENAME];
       OldList * specifiers;
       Declarator declarator;
@@ -320,15 +320,13 @@ void CreateRegisterModuleBody()
       specifiers = MkList();
       ListAdd(specifiers, MkSpecifier(VOID));
 
-      moduleParam = MkTypeName(MkListOne(/*MkSpecifier(VOID)*/ MkSpecifierName("Module")), 
-         //MkDeclaratorPointer(MkPointer(null, null), 
+      moduleParam = MkTypeName(MkListOne(/*MkSpecifier(VOID)*/ MkSpecifierName("Module")),
+         //MkDeclaratorPointer(MkPointer(null, null),
          MkDeclaratorIdentifier(MkIdentifier("module")));
 
       GetLastDirectory(outputFile, moduleName);
       StripExtension(moduleName);
-      ChangeCh(moduleName, '.', '_');
-      ChangeCh(moduleName, ' ', '_');
-      ChangeCh(moduleName, '-', '_');
+      FixModuleName(moduleName);
       //strcpy(moduleName, argv[2]);
       sprintf(registerName, "__ecereUnregisterModule_%s", moduleName);
 
@@ -336,7 +334,7 @@ void CreateRegisterModuleBody()
          MkListOne(moduleParam));
 
       {
-         FunctionDefinition function = MkFunction(specifiers, declarator, null);
+         FunctionDefinition function = _MkFunction(specifiers, declarator, null, false);
          ProcessFunctionBody(function, unregisterModuleBody);
          function.declMode = defaultAccess;
          if(!ast) ast = MkList();
@@ -345,7 +343,7 @@ void CreateRegisterModuleBody()
    }
 }
 
-void RegisterMembersAndProperties(Class regClass, bool isMember, char * className, Statement statement)
+void RegisterMembersAndProperties(Class regClass, bool isMember, const char * className, Statement statement)
 {
    DataMember dataMember = isMember ? (DataMember)regClass : null;
    DataMember member;
@@ -358,7 +356,7 @@ void RegisterMembersAndProperties(Class regClass, bool isMember, char * classNam
    int privateID = 0;
    bool privateMembers = false;
 
-   sprintf(dataMemberSize, "%d", sizeof(DataMember));
+   sprintf(dataMemberSize, "%d", (int)sizeof(DataMember));
    if(!isMember)
    {
       for(prop = regClass.conversions.first; prop; prop = prop.next)
@@ -541,7 +539,7 @@ void RegisterMembersAndProperties(Class regClass, bool isMember, char * classNam
                   MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null,null), null)), MkExpIdentifier(MkIdentifier(name))))));
                ListAdd(registerModuleBody.compound.statements, stmt);
             }
-            if(((Symbol)prop.symbol).propCategory)
+            if(prop.symbol && ((Symbol)prop.symbol).propCategory)
             {
                stmt = MkExpressionStmt(MkListOne(
                   MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier(nameM)), MkIdentifier("category")), '=',
@@ -556,13 +554,13 @@ void RegisterMembersAndProperties(Class regClass, bool isMember, char * classNam
                ListAdd(list, MkExpOp(MkExpIdentifier(MkIdentifier(nameM)), '=', MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null,null), null)), MkExpConstant("0"))));
                // stmt = MkIfStmt(MkListOne(MkExpOp(null, '!', MkExpIdentifier(MkIdentifier(name)))), MkExpressionStmt(list), null);
                stmt = MkIfStmt(MkListOne(MkExpOp(
-                     MkExpMember(MkExpIdentifier(MkIdentifier("module")), MkIdentifier("application")), EQ_OP, 
+                     MkExpMember(MkExpIdentifier(MkIdentifier("module")), MkIdentifier("application")), EQ_OP,
                      MkExpMember(MkExpIdentifier(MkIdentifier("__thisModule")), MkIdentifier("application")))), MkExpressionStmt(list), null);
 
                ListAdd(registerModuleBody.compound.statements, stmt);
 
                // Unregister 2nd property
-               stmt = MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(nameM)), '=', 
+               stmt = MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(nameM)), '=',
                   MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null,null), null)), MkExpConstant("0")))));
                ListAdd(unregisterModuleBody.compound.statements, stmt);
             }
@@ -658,7 +656,7 @@ void RegisterMembersAndProperties(Class regClass, bool isMember, char * classNam
 
             // Declaration Mode
             ListAdd(args, MkExpIdentifier(MkIdentifier("privateAccess")));
-            
+
             stmt = MkExpressionStmt(MkListOne(
                MkExpCall(MkExpIdentifier(MkIdentifier(isMember ? "eMember_AddDataMember" : "eClass_AddDataMember")), args)));
 
@@ -718,7 +716,7 @@ void RegisterMembersAndProperties(Class regClass, bool isMember, char * classNam
                   ListAdd(args, MkExpIdentifier(MkIdentifier("publicAccess")));
                   break;
             }
-            
+
             stmt = MkExpressionStmt(MkListOne(
                MkExpCall(MkExpIdentifier(MkIdentifier(isMember ? "eMember_AddDataMember" : "eClass_AddDataMember")), args)));
 
@@ -738,9 +736,9 @@ void RegisterMembersAndProperties(Class regClass, bool isMember, char * classNam
 
             curContext = statement.compound.context;
             context = PushContext();
-            
+
             args = MkListOne(MkExpIdentifier(MkIdentifier((member.type == unionMember) ? "unionMember" : "structMember")));
-            
+
             // Declaration Mode
             switch(member.memberAccess)
             {
@@ -755,12 +753,12 @@ void RegisterMembersAndProperties(Class regClass, bool isMember, char * classNam
                   ListAdd(args, MkExpIdentifier(MkIdentifier("publicAccess")));
                   break;
             }
-            
-            compound = MkCompoundStmt(MkListOne(MkDeclaration(MkListOne(MkSpecifierName("DataMember")), 
-               MkListOne(MkInitDeclarator(/*MkDeclaratorPointer(MkPointer(null, null), */MkDeclaratorIdentifier(MkIdentifier(memberName))/*)*/, 
+
+            compound = MkCompoundStmt(MkListOne(MkDeclaration(MkListOne(MkSpecifierName("DataMember")),
+               MkListOne(MkInitDeclarator(/*MkDeclaratorPointer(MkPointer(null, null), */MkDeclaratorIdentifier(MkIdentifier(memberName))/*)*/,
                   MkInitializerAssignment(MkExpCall(MkExpIdentifier(MkIdentifier("eMember_New")), args)))))), MkList());
             compound.compound.context = context;
-         
+
             args = MkList();
 
             // Class class / DataMember member
@@ -896,7 +894,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
 {
    ClassDef def;
    Class regClass = symbol.registered;
-   
+
    if(regClass)
    {
       classType = regClass.type;
@@ -913,7 +911,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                   refCount = 1;
                };
                e.exp.destType = destType;
-            
+
                // Set parsingType to avoid producing errors
                parsingType = true;
                ProcessExpressionType(e.exp);
@@ -925,7 +923,29 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                   destType._class = symbol;
                   ProcessExpressionType(e.exp);
                }
-               ComputeExpression(e.exp);
+               if(e.exp.type == identifierExp && e.exp.expType && e.exp.identifier && e.exp.identifier.string && e.exp.expType.kind == enumType)
+               {
+                  // Resolve enums here
+                  NamedLink l;
+                  char * string = e.exp.identifier.string;
+                  for(l = e.exp.expType.members.first; l; l = l.next)
+                  {
+                     if(!strcmp(l.name, string))
+                     {
+                        if(l.data)
+                        {
+                           FreeExpContents(e.exp);
+                           e.exp.type = constantExp;
+                           e.exp.constant = PrintUInt((uint)l.data);
+                           FreeType(e.exp.expType);
+                           e.exp.expType = ProcessTypeString("uint", false);
+                        }
+                        break;
+                     }
+                  }
+               }
+               else
+                  ComputeExpression(e.exp);
                if(e.exp.isConstant && /*e.exp.expType.kind == intType*/ e.exp.type == constantExp)
                {
                   Operand op = GetOperand(e.exp);
@@ -943,11 +963,25 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                }
                else
                   // Sort of an error
-                  eEnum_AddValue(regClass, e.id.string);                        
+                  eEnum_AddValue(regClass, e.id.string);
             }
             else
                eEnum_AddValue(regClass, e.id.string);
          }
+         // Fix up derivatives enums largest
+         {
+            EnumClassData baseData = regClass.data;
+            OldLink deriv;
+            for(deriv = regClass.derivatives.first; deriv; deriv = deriv.next)
+            {
+               Class c = deriv.data;
+               if(c && c.type == enumClass)
+               {
+                  EnumClassData data = c.data;
+                  data.largest = baseData.largest;
+               }
+            }
+         }
       }
 
       // Process all functions
@@ -999,9 +1033,9 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                      ListAdd(specifiers, MkSpecifier(VOID));
 
                      ast->Insert(curExternal.prev, externalDecl);
-               
-                     decl = MkDeclaration(specifiers, 
-                        MkListOne(MkInitDeclarator(MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)), 
+
+                     decl = MkDeclaration(specifiers,
+                        MkListOne(MkInitDeclarator(MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
                            MkListOne(MkTypeName(MkListOne(MkSpecifierName(regClass.fullName)), null))), null)));
                      externalDecl.declaration = decl;
                      if(decl.symbol && !decl.symbol.methodExternal)
@@ -1013,13 +1047,10 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                         decl.symbol.idCode = symbol.idCode;
                      }
                   }
-            
+
                   propWatch.compound = null;
                   definitions.Insert(null, MkClassDefFunction(func));
 
-                  stmt.type = expressionStmt;
-                  stmt.expressions = MkList();
-
                   for(propID = propWatch.properties->first; propID; propID = propID.next)
                   {
                      Property prop = eClass_FindProperty(regClass, propID.string, privateModule);
@@ -1028,7 +1059,11 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                         // eProperty_SelfWatch(_class, name, callback);
                         OldList * args = MkList();
                         ListAdd(args, MkExpIdentifier(MkIdentifier("class")));
-                        ListAdd(args, MkExpString(QMkString(propID.string)));
+                        {
+                           char * s = QMkString(propID.string);
+                           ListAdd(args, MkExpString(s));
+                           delete s;
+                        }
                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
 
                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("eProperty_SelfWatch")), args));
@@ -1061,7 +1096,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
          OldList * args = MkList();
          Method method;
          Expression exp;
-         char * registerFunction = null;
+         const char * registerFunction = null;
          AccessMode inheritanceAccess = publicAccess;
 
          CreateRegisterModuleBody();
@@ -1140,7 +1175,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
 
          //ListAdd(args, class.declaredStruct ? MkExpTypeSize(MkTypeName(MkListOne(
             //MkSpecifierName(class.structName)), null)) : MkExpConstant("0"));
-      
+
          if(regClass.type == structClass || regClass.type == normalClass || regClass.type == noHeadClass)
          {
             // Check if we have a data member in this class
@@ -1195,11 +1230,11 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
          if(regClass.type == normalClass || regClass.type == noHeadClass)
          {
             // Constructor
-            ListAdd(args, symbol.needConstructor ? 
+            ListAdd(args, symbol.needConstructor ?
                MkExpIdentifier(MkIdentifier(symbol.constructorName)) :
                MkExpConstant("0"));
             // Destructor
-            ListAdd(args, symbol.needDestructor ? 
+            ListAdd(args, symbol.needDestructor ?
                MkExpIdentifier(MkIdentifier(symbol.destructorName)) :
                MkExpConstant("0"));
          }
@@ -1230,7 +1265,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                ListAdd(args, MkExpIdentifier(MkIdentifier(buildingECERECOMModule ? "baseSystemAccess" : "publicAccess")));
                break;
          }
-         
+
          // Declaration Mode
          switch(inheritanceAccess)
          {
@@ -1259,7 +1294,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
 
          stmt = MkIfStmt(MkListOne(
                   MkExpOp(MkExpOp(
-                     MkExpMember(MkExpIdentifier(MkIdentifier("module")), MkIdentifier("application")), EQ_OP, 
+                     MkExpMember(MkExpIdentifier(MkIdentifier("module")), MkIdentifier("application")), EQ_OP,
                      MkExpMember(MkExpIdentifier(MkIdentifier("__thisModule")), MkIdentifier("application"))), AND_OP, MkExpIdentifier(MkIdentifier("class")))),
                MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(symbol.className)), '=',
             MkExpIdentifier(MkIdentifier("class"))))), null);
@@ -1288,12 +1323,12 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
             for(c = 0; c<base.vTblSize; c++)
             {
                Symbol method = (Symbol)regClass._vTbl[c];
-               if((void *) method != DummyMethod && base._vTbl[c] != (void *)method ) // TOLOOKAT: Module check here?
+               if((void *) method != DummyMethod && base._vTbl[c] != (void *)method && method.methodExternal) // TOLOOKAT: Module check here?
                {
                   External external = method.methodExternal;
                   OldList * args = MkList();
                   Identifier id = external.function ? GetDeclId(external.function.declarator) : null;
-               
+
                   // Class class
                   ListAdd(args, MkExpIdentifier(MkIdentifier("class")));
                   // char * name
@@ -1486,7 +1521,8 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                // uint value
                {
                   char temp[1024];
-                  sprintf(temp, "%d", value.data);
+                  // TODO: Support 64 bit enums
+                  sprintf(temp, "%d", (int)value.data);
                   ListAdd(args, MkExpConstant(temp));
                }
 
@@ -1534,10 +1570,10 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                   {
                      char memberTypeString[132] = "TemplateMemberType::";
                      bool needClass = true;
-                     
+
                      param.memberType.OnGetString(memberTypeString + strlen(memberTypeString), null, &needClass);
-                     ListAdd(args, 
-                        MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)), 
+                     ListAdd(args,
+                        MkExpCast(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null)),
                            MkExpIdentifier(MkIdentifier(memberTypeString))));
                      break;
                   }
@@ -1551,7 +1587,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                   {
                      case type:
                      {
-                        char * typeString = param.defaultArgument.templateDatatype ? 
+                        char * typeString = param.defaultArgument.templateDatatype ?
                            StringFromSpecDecl(param.defaultArgument.templateDatatype.specifiers, param.defaultArgument.templateDatatype.decl) : null;
                         char * string = QMkString(typeString);
 
@@ -1583,7 +1619,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
 
                         // TOFIX: UNIONS ARE BROKEN FOR ASSIGNMENTS, USING dataTypeString INSTEAD
                         // members->Add(MkMemberInit(MkListOne(MkIdentifier("memberString")), MkInitializerAssignment(MkExpString(string))));
-                        members->Add(MkMemberInit(MkListOne(MkIdentifier("dataTypeString")), MkInitializerAssignment(MkExpString(string))));                        
+                        members->Add(MkMemberInit(MkListOne(MkIdentifier("dataTypeString")), MkInitializerAssignment(MkExpString(string))));
 
                         delete string;
                         break;
@@ -1605,7 +1641,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
 
                         //string = QMkString(ui64String);
                         members->Add(MkMemberInit(ids, MkInitializerAssignment(MkExpConstant(ui64String))));
-                        
+
                         delete string;
                         delete ui64String;
                         break;
@@ -1622,7 +1658,7 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                ListAdd(registerModuleBody.compound.statements, stmt);
             }
             stmt = MkExpressionStmt(MkListOne(
-               MkExpCall(MkExpIdentifier(MkIdentifier("eClass_DoneAddingTemplateParameters")), 
+               MkExpCall(MkExpIdentifier(MkIdentifier("eClass_DoneAddingTemplateParameters")),
                   MkListOne(MkExpIdentifier(MkIdentifier("class"))))));
             ListAdd(registerModuleBody.compound.statements, stmt);
          }
@@ -1646,9 +1682,13 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                   DeclareClass(FindClass(def.designer), className);
                   */
 
-                  stmt = MkIfStmt(MkListOne(MkExpIdentifier(MkIdentifier("class"))), MkExpressionStmt(MkListOne(
-                     MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("class")), MkIdentifier("designerClass")), '=',
-                        MkExpString(QMkString(def.designer))))), null);
+                  {
+                     char * s = QMkString(def.designer);
+                     stmt = MkIfStmt(MkListOne(MkExpIdentifier(MkIdentifier("class"))), MkExpressionStmt(MkListOne(
+                        MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("class")), MkIdentifier("designerClass")), '=',
+                           MkExpString(s)))), null);
+                     delete s;
+                  }
                   ListAdd(registerModuleBody.compound.statements, stmt);
                }
                else if(def.type == classNoExpansionClassDef)
@@ -1669,24 +1709,18 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
                }
                else if(def.type == designerDefaultPropertyClassDef)
                {
+                  char * s = QMkString(def.defaultProperty.string);
                   stmt = MkIfStmt(MkListOne(MkExpIdentifier(MkIdentifier("class"))), MkExpressionStmt(MkListOne(
                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("class")), MkIdentifier("defaultProperty")), '=',
-                        MkExpString(QMkString(def.defaultProperty.string))))), null);
+                        MkExpString(s)))), null);
                   ListAdd(registerModuleBody.compound.statements, stmt);
+                  delete s;
                }
                else if(def.type == classPropertyValueClassDef)
                {
-                  OldList * args = MkList();
-                  ListAdd(args, MkExpIdentifier(MkIdentifier("class")));
-                  ListAdd(args, MkExpString(QMkString(def.id.string)));
-                  ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(INT64)), null), def.initializer.exp));
-
+                  classPropValues.Add(ClassPropertyValue { regClass = regClass, id = def.id, exp = def.initializer.exp });
+                  def.id = null;
                   def.initializer.exp = null;
-
-                  stmt = MkExpressionStmt(MkListOne(
-                     MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eClass_SetProperty")), args)));
-
-                  ListAdd(registerModuleBody.compound.statements, stmt);
                }
             }
          }
@@ -1694,6 +1728,20 @@ static void ProcessClass(ClassType classType, OldList definitions, Symbol symbol
    }
 }
 
+class ClassPropertyValue
+{
+   Class regClass;
+   Identifier id;
+   Expression exp;
+
+   ~ClassPropertyValue()
+   {
+      FreeIdentifier(id);
+   }
+};
+
+static List<ClassPropertyValue> classPropValues { };
+
 public void ProcessClassDefinitions()
 {
    External external, next;
@@ -1721,9 +1769,14 @@ public void ProcessClassDefinitions()
          }
          else if(external.type == functionExternal)
          {
+            bool setStaticMethod = false;
             // Mark
-            if(external.symbol && !external.symbol.type.thisClass)
+            if(external.symbol && !external.symbol.type.thisClass && !external.symbol.type.staticMethod)
+            {
+               // TOCHECK: Where do we actually need this to be set?
                external.symbol.type.staticMethod = true;
+               setStaticMethod = true;
+            }
 
             if(inCompiler)
             {
@@ -1758,7 +1811,12 @@ public void ProcessClassDefinitions()
                {
                   char * string;
                   char type[1024] = "";
+                  // We don't want functions to be marked as static methods
+                  if(setStaticMethod)
+                     function.declarator.symbol.type.staticMethod = false;
                   PrintType(function.declarator.symbol.type, type, true, true);
+                  if(setStaticMethod)
+                     function.declarator.symbol.type.staticMethod = true;
                   string = QMkString(type);
                   ListAdd(args, MkExpString(string));
                   delete string;
@@ -1809,10 +1867,10 @@ public void ProcessClassDefinitions()
                {
                   Specifier specifier;
                   bool removeExternal = false;
-               
+
                   for(specifier = declaration.specifiers->first; specifier; specifier = specifier.next)
                   {
-                     if((specifier.type == enumSpecifier || specifier.type == structSpecifier || specifier.type == unionSpecifier) && specifier.id && specifier.id.string && 
+                     if((specifier.type == enumSpecifier || specifier.type == structSpecifier || specifier.type == unionSpecifier) && specifier.id && specifier.id.string &&
                         (declaration.declMode || specifier.baseSpecs || (specifier.type == enumSpecifier && specifier.definitions)))
                      {
                         Symbol symbol = FindClass(specifier.id.string);
@@ -1821,7 +1879,7 @@ public void ProcessClassDefinitions()
                            ClassType classType;
                            if(specifier.type == enumSpecifier)
                               classType = enumClass;
-                           else 
+                           else
                               classType = structClass;
 
                            removeExternal = true;
@@ -1832,7 +1890,7 @@ public void ProcessClassDefinitions()
                            ProcessClass(classType, specifier.definitions, symbol, specifier.baseSpecs, specifier.list,ast, external, declaration.declMode);
                         }
                      }
-                  }               
+                  }
                   if(inCompiler && removeExternal)
                   {
                      ast->Remove(external);
@@ -1892,5 +1950,34 @@ public void ProcessClassDefinitions()
             }
          }
       }
+
+      // Set the class properties at the very end
+      for(v : classPropValues)
+      {
+         OldList * findClassArgs = MkList();
+         OldList * args = MkList();
+         Statement compoundStmt;
+         String s;
+
+         ListAdd(findClassArgs, MkExpIdentifier(MkIdentifier("module")));
+         s = QMkString(v.regClass.name);
+         ListAdd(findClassArgs, MkExpString(s));
+         delete s;
+
+         ListAdd(args, MkExpIdentifier(MkIdentifier("_class")));
+         s = QMkString(v.id.string);
+         ListAdd(args, MkExpString(s));
+         delete s;
+         ListAdd(args, MkExpCast(MkTypeName(MkListOne(MkSpecifier(INT64)), null), v.exp));
+         compoundStmt = MkCompoundStmt(MkListOne(MkDeclaration(MkListOne(MkSpecifierName("ecere::com::Class")),
+                       MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("_class")),
+                        MkInitializerAssignment(MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eSystem_FindClass")), findClassArgs)))))),
+            MkListOne(MkExpressionStmt(MkListOne(
+               MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eClass_SetProperty")), args)))));
+         compoundStmt.compound.context = Context { parent = registerModuleBody.compound.context };
+         ListAdd(registerModuleBody.compound.statements, compoundStmt);
+      }
+
+      classPropValues.Free();
    }
 }