]> git.sur5r.net Git - cc65/blobdiff - src/ca65/ulabel.c
Finished implemenation of commands to delete macros. Added the new commands to
[cc65] / src / ca65 / ulabel.c
index c997d9b497655eaf0f3a29ad6a5b1ef6d0362c77..023dd13757e12f3fcf461d952f58ec0a37a3df41 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2004 Ullrich von Bassewitz                                       */
-/*               Römerstraße 52                                              */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2000-2011, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 /* common */
 #include "check.h"
 #include "coll.h"
-#include "filepos.h"
 #include "xmalloc.h"
 
 /* ca65 */
 #include "error.h"
 #include "expr.h"
+#include "lineinfo.h"
 #include "scanner.h"
 #include "ulabel.h"
 
@@ -56,9 +56,9 @@
 /* Struct that describes an unnamed label */
 typedef struct ULabel ULabel;
 struct ULabel {
-    FilePos    Pos;                    /* Position of the label in the source */
-    ExprNode*  Val;                    /* The label value - may be NULL */
-    unsigned    Ref;                    /* Number of references */
+    Collection  LineInfos;      /* Position of the label in the source */
+    ExprNode*  Val;            /* The label value - may be NULL */
+    unsigned    Ref;            /* Number of references */
 };
 
 /* List management */
@@ -82,9 +82,10 @@ static ULabel* NewULabel (ExprNode* Val)
     ULabel* L = xmalloc (sizeof (ULabel));
 
     /* Initialize the fields */
-    L->Pos = CurPos;
-    L->Val = Val;
-    L->Ref = 0;
+    L->LineInfos = EmptyCollection;
+    GetFullLineInfo (&L->LineInfos, 0);
+    L->Val       = Val;
+    L->Ref       = 0;
 
     /* Insert the label into the collection */
     CollAppend (&ULabList, L);
@@ -113,7 +114,7 @@ ExprNode* ULabRef (int Which)
     if (Which > 0) {
        --Which;
     }
-    Index = (int) CollCount (&ULabList) + Which;
+    Index = (int) ULabDefCount + Which;
 
     /* We cannot have negative label indices */
     if (Index < 0) {
@@ -123,19 +124,27 @@ ExprNode* ULabRef (int Which)
        return GenCurrentPC();
     }
 
-    /* If the label does already exist, return it's value, otherwise create
-     * enough forward references, and return a label reference.
-     */
+    /* Check if the label exists. If not, generate enough forward labels. */
     if (Index < (int) CollCount (&ULabList)) {
+        /* The label exists, get it. */
        L = CollAtUnchecked (&ULabList, Index);
-        ++L->Ref;
-       return CloneExpr (L->Val);
     } else {
+        /* Generate new, undefined labels */
        while (Index >= (int) CollCount (&ULabList)) {
             L = NewULabel (0);
        }
-        ++L->Ref;
-       return GenULabelExpr (Index);
+    }
+
+    /* Mark the label as referenced */
+    ++L->Ref;
+
+    /* If the label is already defined, return its value, otherwise return
+     * just a reference.
+     */
+    if (L->Val) {
+        return CloneExpr (L->Val);
+    } else {
+        return GenULabelExpr (Index);
     }
 }
 
@@ -152,7 +161,7 @@ void ULabDef (void)
        ULabel* L = CollAtUnchecked (&ULabList, ULabDefCount);
        CHECK (L->Val == 0);
        L->Val = GenCurrentPC ();
-       L->Pos = CurPos;
+        GetFullLineInfo (&L->LineInfos, 0);
     } else {
        /* There is no such label, create it */
                NewULabel (GenCurrentPC ());
@@ -196,7 +205,7 @@ void ULabCheck (void)
     unsigned I = ULabDefCount;
     while (I < CollCount (&ULabList)) {
        ULabel* L = CollAtUnchecked (&ULabList, I);
-       PError (&L->Pos, "Undefined label");
+               LIError (&L->LineInfos, "Undefined label");
        ++I;
     }
 
@@ -206,10 +215,11 @@ void ULabCheck (void)
     for (I = 0; I < CollCount (&ULabList); ++I) {
         ULabel* L = CollAtUnchecked (&ULabList, I);
         if (L->Ref == 0) {
-            PWarning (&L->Pos, 1, "No reference to unnamed label");
+            LIWarning (&L->LineInfos, 1, "No reference to unnamed label");
         }
     }
 }
 
 
 
+