i18n: (#858) Handling localization for libraries within static executables
[sdk] / compiler / libec / src / ast.ec
index 21ce91c..4d5e08f 100644 (file)
@@ -12,7 +12,9 @@ bool strictNameSpaces;
 public void SetStrictNameSpaces(bool b) { strictNameSpaces = b; }
 
 AccessMode declMode = privateAccess;
-public void SetDeclMode(AccessMode accessMode) { declMode = accessMode; }
+AccessMode structDeclMode = privateAccess;
+
+public void SetDeclMode(AccessMode accessMode) { structDeclMode = declMode = accessMode; }
 AccessMode defaultDeclMode = privateAccess;
 public void SetDefaultDeclMode(AccessMode accessMode) { defaultDeclMode = accessMode; }
 
@@ -55,7 +57,7 @@ public Identifier MkIdentifier(char * string)
 {
    Identifier id { };
    int c;
-   
+
    id._class = null; // Default class...
 
    if(string)
@@ -119,7 +121,7 @@ public Identifier MkIdentifier(char * string)
                else
                   id.string = CopyString(string);
             }
-         }         
+         }
       }
       else if(gotColon)
       {
@@ -271,7 +273,7 @@ public struct ContextStringPair
    }
 };
 
-Map<ContextStringPair, List<Location> > intlStrings { };
+Map<ContextStringPair, List<Location>> intlStrings { };
 
 Expression MkExpIntlString(char * string, char * context)
 {
@@ -298,7 +300,8 @@ Expression MkExpIntlString(char * string, char * context)
       }
       list.Add(yylloc);
    }
-   ListAdd(list, QMkExpId("__thisModule"));
+   //ListAdd(list, QMkExpId("__thisModule"));
+   ListAdd(list, MkExpString(QMkString(i18nModuleName ? i18nModuleName : "")));
    ListAdd(list, MkExpString(string));
    if(context)
    {
@@ -421,7 +424,12 @@ Expression MkExpVaArg(Expression exp, TypeName type)
 
 Specifier MkSpecifier(int specifier)
 {
-   return { type = baseSpecifier, specifier = specifier };
+   if(specifier == _BOOL && (declMode != defaultAccess && defaultDeclMode != defaultAccess))
+      return MkSpecifierName("bool");
+   else if(specifier == _BOOL || specifier == BOOL)
+      return { type = baseSpecifier, specifier = specifier };
+   else
+      return { type = baseSpecifier, specifier = specifier };
 }
 
 Specifier MkSpecifierTypeOf(Expression expression)
@@ -441,7 +449,7 @@ Specifier MkSpecifierExtended(ExtDecl extDecl)
 
 Specifier MkEnum(Identifier id, OldList list)
 {
-   Specifier spec 
+   Specifier spec
    {
       type = enumSpecifier;
       id = id;
@@ -482,9 +490,9 @@ Specifier MkStructOrUnion(SpecifierType type, Identifier id, OldList definitions
 {
    Specifier spec { type = type, id = id };
    if(id && FindType(curContext, id.string))
-      declMode = defaultAccess;
+      structDeclMode = defaultAccess;
    spec.definitions = definitions;
-   if(definitions && id && !declMode)
+   if(definitions && id && structDeclMode == defaultAccess)
    {
       OldList specs { };
       Symbol symbol;
@@ -635,7 +643,6 @@ public TypeName MkTypeName(OldList qualifiers, Declarator declarator)
          {
             // This is actually a type
             ListAdd(qualifiers, MkSpecifierName(decl.identifier.string));
-            decl.identifier.string = null;
             FreeDeclarator(decl);
             parentDecl.declarator = null;
          }
@@ -799,7 +806,7 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
 {
    Declaration decl { type = initDeclaration, declarators = initDeclarators, specifiers = specifiers, loc = yylloc };
    bool variable = true;
-   
+
    if(specifiers != null)
    {
       bool gotType = false;
@@ -812,7 +819,7 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
             if(initDeclarators != null)
             {
                InitDeclarator d;
-         
+
                for(d = initDeclarators.first; d; d = d.next)
                {
                   if(GetDeclId(d.declarator).string)
@@ -864,7 +871,7 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
             variable = false;
             break;
          }
-         else if(spec.type == baseSpecifier && 
+         else if(spec.type == baseSpecifier &&
             (spec.specifier == STRUCT || spec.specifier == UNION))
             variable = false;
          else
@@ -931,7 +938,7 @@ Declaration MkDeclaration(OldList specifiers, OldList initDeclarators)
                   delete id.string;
                   id.string = CopyString(name);
                }
-               
+
                // Avoid memory leaks on duplicated symbols (BinaryTree::Add Would Fail)
                symbol = (Symbol)(curContext.templateTypesOnly ? curContext.parent : curContext).symbols.FindString(id.string);
                if(!symbol)
@@ -1016,7 +1023,7 @@ Declaration MkStructDeclaration(OldList specifiers, OldList declarators, Specifi
             }
             if(s)
             {
-               decl.declarators = declarators = MkListOne(MkDeclaratorIdentifier(MkIdentifier(s)));
+               decl.declarators = declarators = MkListOne(MkStructDeclarator(MkDeclaratorIdentifier(MkIdentifier(s)), null));
                specifiers.Remove(spec);
                FreeSpecifier(spec);
                spec = null;
@@ -1109,7 +1116,7 @@ Statement MkReturnStmt(OldList exp)
 
 FunctionDefinition MkFunction(OldList specifiers, Declarator declarator, OldList declarationList)
 {
-   _MkFunction(specifiers, declarator, declarationList, true);
+   return _MkFunction(specifiers, declarator, declarationList, true);
 }
 
 FunctionDefinition _MkFunction(OldList specifiers, Declarator declarator, OldList declarationList, bool errorOnOmit)
@@ -1255,7 +1262,7 @@ External MkExternalFunction(FunctionDefinition function)
       for(spec = function.specifiers->first; spec; spec = spec.next)
          if(spec.type == baseSpecifier && spec.specifier == STATIC)
          {
-            declMode = staticAccess;
+            structDeclMode = declMode = staticAccess;
             break;
          }
    }
@@ -1300,12 +1307,12 @@ External MkExternalDeclaration(Declaration declaration)
       for(spec = declaration.specifiers->first; spec; spec = spec.next)
          if(spec.type == baseSpecifier && spec.specifier == TYPEDEF)
          {
-            declMode = defaultAccess;
+            structDeclMode = declMode = defaultAccess;
             break;
          }
          else if(spec.type == baseSpecifier && spec.specifier == STATIC)
          {
-            declMode = staticAccess;
+            structDeclMode = declMode = staticAccess;
             break;
          }
    }
@@ -1361,7 +1368,7 @@ void SetClassTemplateArgs(Specifier spec, OldList templateArgs)
                   fileInput = backFileInput;
                   if(fileInput)
                   {
-                     fileInput.Seek(yylloc.start.pos, start); 
+                     fileInput.Seek(yylloc.start.pos, start);
                      resetScannerPos(&yylloc.start);
                      yychar = -2;
                   }
@@ -1454,7 +1461,7 @@ Specifier _MkSpecifierName(char * name, Symbol symbol, OldList templateArgs)
          }
       }
       else if(symbol)
-         spec.name = CopyString(symbol.string);   
+         spec.name = CopyString(symbol.string);
       else
          spec.name = CopyString(name);
       spec.symbol = symbol;
@@ -1541,7 +1548,7 @@ void ProcessClassFunctionBody(ClassFunction func, Statement body)
 
       symbol = Symbol
       {
-         
+
       };
 
       {
@@ -1643,7 +1650,7 @@ Instantiation MkInstantiationNamed(OldList specs, Expression exp, OldList member
          }
 
       FreeList(specs, FreeSpecifier);
-         
+
       if(!spec)
       {
          Compiler_Error($"Expecting class specifier\n");
@@ -1768,7 +1775,7 @@ Symbol _DeclClass(int symbolID, char * name)
       for(classContext = curContext; classContext && !classContext.classDef; classContext = classContext.parent);
       if(classContext)
       {
-         
+
       }
       */
       if(name[0] == ':' && name[1] == ':')
@@ -1811,9 +1818,9 @@ void SetupBaseSpecs(Symbol symbol, OldList baseSpecs)
       strcpy(name, ((Specifier)baseSpecs.first).name);
       tpl = strchr(name, '<');
       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)
@@ -1823,7 +1830,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)
@@ -1842,7 +1849,7 @@ void SetupBaseSpecs(Symbol symbol, OldList baseSpecs)
                   {
                      p.param = param = TemplateParameter
                      {
-                        identifier = MkIdentifier(p.name), type = p.type, 
+                        identifier = MkIdentifier(p.name), type = p.type,
                         dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
                      };
                   }
@@ -2028,7 +2035,7 @@ TemplatedType FindTemplateTypeParameter(Context ctx, char * name)
 bool ModuleAccess(Module searchIn, Module searchFor)
 {
    SubModule subModule;
-   
+
    if(searchFor == searchIn)
       return true;
 
@@ -2053,8 +2060,8 @@ ModuleImport FindModule(Module moduleToFind)
          break;
    if(!module)
    {
-      module = ModuleImport 
-      { 
+      module = ModuleImport
+      {
          name = CopyString(moduleToFind.name), importType = moduleToFind.importType,
          importAccess = ModuleAccess(privateModule, moduleToFind) ? publicAccess : privateAccess
       };
@@ -2071,7 +2078,7 @@ static void GetFullClassNameSpace(NameSpace * ns, char * name)
    {
       GetFullClassNameSpace(ns->parent, name);
       strcat(name, ns->name);
-      strcat(name, "::");      
+      strcat(name, "::");
    }
 }
 
@@ -2155,7 +2162,7 @@ public Symbol FindClass(char * name)
          }
          */
          if(cl.shortName && !strcmp(cl.shortName, name))
-            break;            
+            break;
       }
 #ifdef _TIMINGS
       findClassIgnoreNSTotalTime += GetTime() - startTime;
@@ -2308,12 +2315,14 @@ static Type ProcessTypeSpecs(OldList specs, bool assumeEllipsis, bool keepTypeNa
             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 == _BOOL || spec.specifier == BOOL)
+               specType.kind = _BoolType;
             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) 
+            else if(spec.specifier == VALIST)
                specType.kind = vaListType;
             else if(spec.specifier == SHORT) specType.kind = shortType;
-            else if(spec.specifier == LONG) 
+            else if(spec.specifier == LONG)
             {
                if(isLong || (targetBits == 64 && targetPlatform != win32))
                   specType.kind = int64Type;
@@ -2328,7 +2337,7 @@ static Type ProcessTypeSpecs(OldList specs, bool assumeEllipsis, bool keepTypeNa
             else if(spec.specifier == CONST)
                specType.constant = true;
             else if(spec.specifier == TYPED_OBJECT || spec.specifier == ANY_OBJECT || spec.specifier == CLASS)
-            { 
+            {
                switch(spec.specifier)
                {
                   case TYPED_OBJECT:   specType.classObjectType = typedObject;   break;
@@ -2405,19 +2414,21 @@ static Type ProcessTypeSpecs(OldList specs, bool assumeEllipsis, bool keepTypeNa
             Symbol _class = spec.id ? FindClass(spec.id.string) : null;
             if(_class)
             {
+               specType.declaredWithStruct = true;
                if(!_class.registered || _class.registered.type != structClass)
-                  specType.directClassAccess = true;
+                  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 
+               // TESTING THIS HERE... Had 0 type size
                if(!spec.definitions && !isTypedef)
                {
                   Symbol symbol = spec.id.string ? FindSymbol(spec.id.string, curContext, globalContext, true, false) : null;
@@ -2508,7 +2519,7 @@ static Type ProcessTypeSpecs(OldList specs, bool assumeEllipsis, bool keepTypeNa
    }
    else if(assumeEllipsis)
       specType.kind = ellipsisType;
-   return specType;     
+   return specType;
 }
 
 static Type ProcessTypeDecls(OldList specs, Declarator decl, Type parentType)
@@ -2594,7 +2605,7 @@ static Type ProcessTypeDecls(OldList specs, Declarator decl, Type parentType)
          {
             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; 
+               type.enumClass = decl.array.enumClass.symbol;
             break;
          }
          case pointerDeclarator:
@@ -2745,15 +2756,15 @@ public Type MkClassType(char * name)
    return null;
 }
 
-AsmField MkAsmField(char * command, Expression expression)
+AsmField MkAsmField(char * command, Expression expression, Identifier symbolic)
 {
-   return { command = command, expression = expression };
+   return { command = command, expression = expression, symbolic = symbolic };
 }
 
 Statement MkAsmStmt(Specifier spec, char * statements, OldList inputFields, OldList outputFields, OldList clobberedFields)
 {
-   return { type = asmStmt, asmStmt.spec = spec, asmStmt.statements = statements, 
-      asmStmt.inputFields = inputFields, asmStmt.outputFields = outputFields, 
+   return { type = asmStmt, asmStmt.spec = spec, asmStmt.statements = statements,
+      asmStmt.inputFields = inputFields, asmStmt.outputFields = outputFields,
       asmStmt.clobberedFields = clobberedFields };
 }
 
@@ -2881,7 +2892,7 @@ Expression GetTemplateArgExpByName(char * paramName, Class curClass, TemplatePar
          char idString[32];
          char className[1024];
          Expression classExp;
-         
+
          sprintf(idString, "%d", id);
          strcpy(className, "__ecereClass_");
          FullClassNameCat(className, _class.fullName, true);
@@ -2889,11 +2900,11 @@ Expression GetTemplateArgExpByName(char * paramName, Class curClass, TemplatePar
          DeclareClass(FindClass(_class.fullName), className);
 
          argExp = MkExpIndex((/*pointer ? MkExpPointer : */MkExpMember)
-               (MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class")) /*MkExpIdentifier(MkIdentifier(className))*/, 
+               (MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class")) /*MkExpIdentifier(MkIdentifier(className))*/,
                MkIdentifier("templateArgs")), MkListOne(MkExpConstant(idString)));
       }
    }
-   return argExp; 
+   return argExp;
 }
 
 Expression GetTemplateArgExp(TemplateParameter param, Class curClass, bool pointer)
@@ -2950,6 +2961,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; }
@@ -2957,3 +2969,13 @@ public void ParseEc()
 {
    yyparse();
 }
+
+public int LexEc()
+{
+   return yylex();
+}
+
+public const char * GetYYText()
+{
+   return yytext;
+}