]> git.sur5r.net Git - cc65/blobdiff - src/cc65/compile.c
In case of parse errors for structs, don't just set the type of the result to
[cc65] / src / cc65 / compile.c
index 8475657a4f0df470223202f35e10c8ee9c52a2cd..3dd7bffa43a8e84aff4a73c8c35567ba52eebdb5 100644 (file)
@@ -2,14 +2,14 @@
 /*                                                                           */
 /*                                compile.c                                 */
 /*                                                                           */
-/*                      Top level compiler subroutine                       */
+/*                      Top level compiler subroutine                       */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2008 Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2000-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -55,6 +55,7 @@
 #include "input.h"
 #include "litpool.h"
 #include "macrotab.h"
+#include "output.h"
 #include "pragma.h"
 #include "preproc.h"
 #include "standard.h"
@@ -82,7 +83,6 @@ static void Parse (void)
     while (CurTok.Tok != TOK_CEOF) {
 
        DeclSpec        Spec;
-       Declaration     Decl;
 
        /* Check for empty statements */
        if (CurTok.Tok == TOK_SEMI) {
@@ -127,6 +127,8 @@ static void Parse (void)
        comma = 0;
                while (1) {
 
+            Declaration         Decl;
+
            /* Read the next declaration */
            ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
            if (Decl.Ident[0] == '\0') {
@@ -140,8 +142,8 @@ static void Parse (void)
              * specified as static. This means that "extern int i" will not
              * get storage allocated.
              */
-           if ((Decl.StorageClass & SC_FUNC) == 0      &&
-                (Decl.StorageClass & SC_TYPEDEF) == 0   &&
+           if ((Decl.StorageClass & SC_FUNC) != SC_FUNC          &&
+                (Decl.StorageClass & SC_TYPEDEF) != SC_TYPEDEF    &&
                 ((Spec.Flags & DS_DEF_STORAGE) != 0  ||
                  (Decl.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC)) {
 
@@ -167,6 +169,9 @@ static void Parse (void)
            /* Add an entry to the symbol table */
            Entry = AddGlobalSym (Decl.Ident, Decl.Type, Decl.StorageClass);
 
+            /* Add declaration attributes */
+            SymUseAttr (Entry, &Decl);
+
            /* Reserve storage for the variable if we need to */
                    if (Decl.StorageClass & SC_STORAGE) {
 
@@ -191,9 +196,14 @@ static void Parse (void)
                        }
                    }
 
-                   /* Switch to the data or rodata segment */
-                   if (IsQualConst (Decl.Type)) {
-                       g_userodata ();
+                   /* Switch to the data or rodata segment. For arrays, check
+                      * the element qualifiers, since not the array but its
+                      * elements are const.
+                      */
+                   if (IsQualConst (Decl.Type) ||
+                        (IsTypeArray (Decl.Type) &&
+                         IsQualConst (GetElementType (Decl.Type)))) {
+                       g_userodata ();
                    } else {
                        g_usedata ();
                    }
@@ -294,7 +304,7 @@ void Compile (const char* FileName)
     };
 
     /* Add macros that are always defined */
-    DefineNumericMacro ("__CC65__", VERSION);
+    DefineNumericMacro ("__CC65__", GetVersionAsNumber ());
 
     /* Language standard that is supported */
     DefineNumericMacro ("__CC65_STD_C89__", STD_C89);
@@ -329,6 +339,10 @@ void Compile (const char* FileName)
     DefineTextMacro ("__DATE__", DateStr);
     DefineTextMacro ("__TIME__", TimeStr);
 
+    /* Other standard macros */
+    /* DefineNumericMacro ("__STDC__", 1);      <- not now */
+    DefineNumericMacro ("__STDC_HOSTED__", 1);
+
     /* Initialize the literal pool */
     InitLiteralPool ();
 
@@ -344,11 +358,18 @@ void Compile (const char* FileName)
     /* Are we supposed to compile or just preprocess the input? */
     if (PreprocessOnly) {
 
+       /* Open the file */
+        OpenOutputFile ();
+
+        /* Preprocess each line and write it to the output file */
         while (NextLine ()) {
             Preprocess ();
-            printf ("%.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
+            WriteOutput ("%.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
         }
 
+        /* Close the output file */
+        CloseOutputFile ();
+
         if (Debug) {
             PrintMacroStats (stdout);
         }