]> git.sur5r.net Git - cc65/blob - src/cc65/symentry.c
Use the xmalloc module from the common directory.
[cc65] / src / cc65 / symentry.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                symentry.c                                 */
4 /*                                                                           */
5 /*               Symbol table entries for the cc65 C compiler                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     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 "../common/xmalloc.h"
37
38 #include "symentry.h"
39
40
41
42 /*****************************************************************************/
43 /*                                   Code                                    */
44 /*****************************************************************************/
45
46
47
48 SymEntry* NewSymEntry (const char* Name, unsigned Flags)
49 /* Create a new symbol table with the given name */
50 {
51     /* Get the length of the name */
52     unsigned Len = strlen (Name);
53
54     /* Allocate memory for the symbol entry */
55     SymEntry* E = xmalloc (sizeof (SymEntry) + Len);
56
57     /* Initialize the entry */
58     E->NextHash = 0;
59     E->PrevSym  = 0;
60     E->NextSym  = 0;
61     E->Link     = 0;
62     E->Owner    = 0;
63     E->Flags    = Flags;
64     E->Type     = 0;
65     memcpy (E->Name, Name, Len+1);
66
67     /* Return the new entry */
68     return E;
69 }
70
71
72
73 void FreeSymEntry (SymEntry* E)
74 /* Free a symbol entry */
75 {
76     TypeFree (E->Type);
77     xfree (E);
78 }
79
80
81
82 void DumpSymEntry (FILE* F, const SymEntry* E)
83 /* Dump the given symbol table entry to the file in readable form */
84 {
85     static const struct {
86         const char*         Name;
87         unsigned            Val;
88     } Flags [] = {
89         /* Beware: Order is important! */
90         { "SC_TYPEDEF",     SC_TYPEDEF  },
91         { "SC_SFLD",        SC_SFLD     },
92         { "SC_STRUCT",      SC_STRUCT   },
93         { "SC_AUTO",        SC_AUTO     },
94         { "SC_REGISTER",    SC_REGISTER },
95         { "SC_STATIC",      SC_STATIC   },
96         { "SC_EXTERN",      SC_EXTERN   },
97         { "SC_ENUM",        SC_ENUM     },
98         { "SC_LABEL",       SC_LABEL    },
99         { "SC_PARAM",       SC_PARAM    },
100         { "SC_FUNC",        SC_FUNC     },
101         { "SC_STORAGE",     SC_STORAGE  },
102         { "SC_DEF",         SC_DEF      },
103         { "SC_REF",         SC_REF      },
104         { "SC_ZEROPAGE",    SC_ZEROPAGE },
105     };
106
107     unsigned I;
108     unsigned SymFlags;
109
110     /* Print the name */
111     fprintf (F, "%s:\n", E->Name);
112
113     /* Print the flags */
114     SymFlags = E->Flags;
115     fprintf (F, "    Flags: ");
116     for (I = 0; I < sizeof (Flags) / sizeof (Flags[0]) && SymFlags != 0; ++I) {
117         if ((SymFlags & Flags[I].Val) == Flags[I].Val) {
118             SymFlags &= ~Flags[I].Val;
119             fprintf (F, "%s ", Flags[I].Name);
120         }
121     }
122     if (SymFlags != 0) {
123         fprintf (F, "%04X", SymFlags);
124     }
125     fprintf (F, "\n");
126
127     /* Print the type */
128     fprintf (F, "    Type:  ");
129     if (E->Type) {
130         PrintType (F, E->Type);
131     } else {
132         fprintf (F, "(none)\n");
133     }
134 }
135
136
137
138 int IsTypeDef (const SymEntry* E)
139 /* Return true if the given entry is a typedef entry */
140 {
141     return ((E->Flags & SC_TYPEDEF) == SC_TYPEDEF);
142 }
143
144
145
146 void ChangeSymType (SymEntry* Entry, type* Type)
147 /* Change the type of the given symbol */
148 {
149     TypeFree (Entry->Type);
150     Entry->Type = TypeDup (Type);
151 }
152
153
154
155