]> git.sur5r.net Git - cc65/blob - src/cc65/lineinfo.c
Added the lineinfo module. Changed the complete code generation to use the
[cc65] / src / cc65 / lineinfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                lineinfo.c                                 */
4 /*                                                                           */
5 /*                      Source file line info structure                      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* cc65 */
43 #include "input.h"
44 #include "lineinfo.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* Global pointer to line information for the current line */
55 static LineInfo* CurLineInfo = 0;
56
57
58
59 /*****************************************************************************/
60 /*                                   Code                                    */
61 /*****************************************************************************/
62
63
64
65 static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
66 /* Create and return a new line info. Ref count will be 1. */
67 {
68     /* Calculate the length of the line */
69     unsigned Len = strlen (Line);
70
71     /* Allocate memory */
72     LineInfo* LI = xmalloc (sizeof (LineInfo) + Len);
73
74     /* Initialize the fields */
75     LI->RefCount  = 1;
76     LI->InputFile = F;
77     LI->LineNum   = LineNum;
78     memcpy (LI->Line, Line, Len+1);
79
80     /* Return the new struct */
81     return LI;
82 }
83
84
85
86 static void FreeLineInfo (LineInfo* LI)
87 /* Free a LineInfo structure */
88 {
89     xfree (LI);
90 }
91
92
93
94 LineInfo* UseLineInfo (LineInfo* LI)
95 /* Increase the reference count of the given line info and return it. */
96 {
97     CHECK (LI != 0);
98     ++LI->RefCount;
99     return LI;
100 }
101
102
103
104 void ReleaseLineInfo (LineInfo* LI)
105 /* Release a reference to the given line info, free the structure if the
106  * reference count drops to zero.
107  */
108 {
109     CHECK (LI && LI->RefCount > 0);
110     if (--LI->RefCount == 0) {
111         /* No more references, free it */
112         FreeLineInfo (LI);
113     }
114 }
115
116
117
118 LineInfo* GetCurLineInfo (void)
119 /* Return a pointer to the current line info. The reference count is NOT
120  * increased, use UseLineInfo for that purpose.
121  */
122 {
123     return CurLineInfo;
124 }
125
126
127
128 void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
129 /* Update the line info - called if a new line is read */
130 {
131     /* If a current line info exists, release it */
132     if (CurLineInfo) {
133         ReleaseLineInfo (CurLineInfo);
134     }
135
136     /* Create a new line info */
137     CurLineInfo = NewLineInfo (F, LineNum, Line);
138 }
139
140
141
142 const char* GetInputName (const LineInfo* LI)
143 /* Return the file name from a line info */
144 {
145     PRECONDITION (LI != 0);
146     return LI->InputFile->Name;
147 }
148
149
150
151 unsigned GetInputLine (const LineInfo* LI)
152 /* Return the line number from a line info */
153 {
154     PRECONDITION (LI != 0);
155     return LI->LineNum;
156 }
157
158
159