OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [ParseClassDeclaration.cpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2012-2018  Robert Finch, Stratford
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// CC64 - Raptor64 'C' derived language compiler
9
//  - 64 bit CPU
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#include "stdafx.h"
27
 
28
extern TABLE tagtable;
29
extern TYP *head;
30
extern TYP stdconst;
31
extern int bit_next;
32
extern int bit_offset;
33
extern int bit_width;
34
extern int parsingParameterList;
35
extern int funcdecl;
36
extern int isStructDecl;
37
 
38
extern int16_t typeno;
39
extern int isTypedef;
40
extern std::string *classname;
41
extern bool isPrivate;
42
extern SYM *currentClass;
43
 
44
extern int roundSize(TYP *tp);
45
 
46
// Class declarations have the form:
47
//
48
//      class identifier [: base class]
49
//  {
50
//              class members
51
//      }
52
//
53
// We could also have a forward reference:
54
//
55
//      class identifier;
56
//
57
// Or a pointer to a class:
58
//
59
//      class *identifier;
60
//
61
int ClassDeclaration::Parse(int ztype)
62
{
63
    SYM *sp, *bcsp;
64
        int ret;
65
        int psd;
66
        ENODE nd;
67
        ENODE *pnd = &nd;
68
        char *idsave;
69
        int alignment;
70
  SYM *cls;
71
 
72
  cls = currentClass;
73
        dfs.puts("<ParseClassDeclaration>\n");
74
        alignment = 0;
75
        isTypedef = TRUE;
76
        NextToken();
77
        if (lastst != id) {
78
                error(ERR_SYNTAX);
79
                goto lxit;
80
        }
81
//      ws = allocSYM();
82
        idsave = my_strdup(lastid);
83
//      ws->name = idsave;
84
//      ws->storage_class = sc_typedef;
85
        // Passes lastid onto struct parsing
86
 
87
        psd = isStructDecl;
88
        isStructDecl++;
89
        ret = 0;
90
        bit_offset = 0;
91
        bit_next = 0;
92
        bit_width = -1;
93
 
94
  dfs.printf("---------------------------------");
95
  dfs.printf("Class decl:%s\n", lastid);
96
  dfs.printf("---------------------------------");
97
        if((sp = tagtable.Find(std::string(lastid),false)) == NULL) {
98
    sp = allocSYM();
99
    sp->SetName(*(new std::string(lastid)));
100
                sp->tp = nullptr;
101
    NextToken();
102
    dfs.printf("A");
103
                if (lastst == kw_align) {
104
      NextToken();
105
      alignment = (int)GetIntegerExpression(&pnd);
106
    }
107
    else
108
      alignment = AL_STRUCT;
109
 
110
                // Could be a forward structure declaration like:
111
                // struct buf;
112
                if (lastst==semicolon) {
113
      dfs.printf("B");
114
                        ret = 1;
115
                        printf("classdecl insert1\r\n");
116
      tagtable.insert(sp);
117
      NextToken();
118
                }
119
                // Defining a pointer to an unknown struct ?
120
                else if (lastst == star) {
121
      dfs.printf("C");
122
                        printf("classdecl insert2\r\n");
123
      tagtable.insert(sp);
124
                }
125
                else if (lastst==colon) {
126
      dfs.printf("D");
127
                        NextToken();
128
                        // Absorb and ignore public/private keywords
129
                        if (lastst == kw_public || lastst==kw_private)
130
                                NextToken();
131
                        if (lastst != id) {
132
                                error(ERR_SYNTAX);
133
                                goto lxit;
134
                        }
135
                        bcsp = tagtable.Find(std::string(lastid),false);
136
                        if (bcsp==nullptr) {
137
                                error(ERR_UNDEFINED);
138
                                goto lxit;
139
                        }
140
      dfs.printf("E");
141
                        // Copy the type chain of base class
142
                        //sp->tp = TYP::Copy(bcsp->tp);
143
                        // Start off at the size of the base.
144
                        sp->tp = allocTYP();
145
                        sp->tp->lst.SetBase(bcsp->GetIndex());
146
                        dfs.printf("Set base class: %d\n", sp->tp->lst.base);
147
                        sp->tp->size = bcsp->tp->size;
148
                        sp->tp->type = (e_bt)ztype;
149
                        sp->tp->typeno = typeno++;
150
      sp->tp->sname = new std::string(*sp->name);
151
      sp->tp->alignment = alignment;
152
            sp->storage_class = sc_type;
153
                        NextToken();
154
                        if (lastst != begin) {
155
        error(ERR_INCOMPLETE);
156
                                goto lxit;
157
                        }
158
                        /*
159
                        sp->tp = allocTYP();
160
                        sp->tp->typeno = typeno++;
161
      sp->tp->sname =  new std::string(*sp->name);
162
      sp->tp->alignment = alignment;
163
                        sp->tp->type = (e_bt)ztype;
164
                        sp->tp->lst.SetBase(bcsp->GetIndex());
165
            sp->storage_class = sc_type;
166
            */
167
      tagtable.insert(sp);
168
      NextToken();
169
      currentClass = sp;
170
      dfs.printf("f");
171
      ParseMembers(sp,ztype);
172
      dfs.printf("G");
173
                }
174
    else if(lastst != begin)
175
      error(ERR_INCOMPLETE);
176
    else {
177
                        if (sp->tp == nullptr) {
178
                                sp->tp = allocTYP();
179
                        }
180
                        sp->tp->size = 0;
181
                        sp->tp->typeno = typeno++;
182
      sp->tp->sname = new std::string(*sp->name);
183
      sp->tp->alignment = alignment;
184
                        sp->tp->type = (e_bt)ztype;
185
                        sp->tp->lst.SetBase(0);
186
            sp->storage_class = sc_type;
187
      tagtable.insert(sp);
188
      NextToken();
189
      currentClass = sp;
190
      ParseMembers(sp,ztype);
191
    }
192
  }
193
  // Else the class was found in the tag table.
194
        else {
195
    NextToken();
196
    if (lastst==kw_align) {
197
            NextToken();
198
      sp->tp->alignment = (int)GetIntegerExpression(&pnd);
199
    }
200
                if (lastst==begin) {
201
        NextToken();
202
        currentClass = sp;
203
        ParseMembers(sp,ztype);
204
                }
205
        }
206
  head = sp->tp;
207
        isStructDecl = psd;
208
lxit:
209
        isTypedef = TRUE;
210
        if (classname) delete classname;
211
        classname = new std::string(idsave);
212
        currentClass = cls;
213
        dfs.puts("</ParseClassDeclaration>\n");
214
        return ret;
215
}
216
 
217
void ClassDeclaration::ParseMembers(SYM *sym, int ztype)
218
{
219
        int slc;
220
        TYP *tp = sym->tp;
221
        int ist;
222
        SYM *hsym;
223
        std::string *name;
224
 
225
        isPrivate = true;
226
        if (sym->tp->size)
227
                slc = sym->tp->roundSize();
228
        else
229
                slc = 0;
230
 
231
//      slc = 0;
232
  tp->val_flag = 1;
233
//      tp->val_flag = FALSE;
234
        ist = isTypedef;
235
        isTypedef = false;
236
 
237
        // First add a hidden member that indicates the class number. The type
238
        // number is used during virtual function calls to determine which
239
        // method to call. This is the first field in the class so it can be
240
        // refeerenced as 0[r25].
241
        hsym = allocSYM();
242
        name = new std::string("_typeno");
243
  hsym->SetName(*name);
244
        hsym->storage_class = sc_member;
245
        hsym->value.i = sym->tp->typeno;
246
        hsym->tp = TYP::Make(bt_char,2);
247
  hsym->tp->sname = new std::string("_typeno");
248
  hsym->tp->alignment = 2;
249
        tp->lst.insert(hsym);
250
  slc += 2;
251
 
252
  while( lastst != end) {
253
                if (lastst==kw_public)
254
                        isPrivate = false;
255
                if (lastst==kw_private)
256
                        isPrivate = true;
257
                if (lastst==kw_public || lastst==kw_private) {
258
                        NextToken();
259
                        if (lastst==colon)
260
                                NextToken();
261
                }
262
                if (lastst==kw_unique || lastst==kw_static) {
263
                        NextToken();
264
      declare(sym,&(tp->lst),sc_static,slc,ztype);
265
                }
266
                else {
267
                        if(ztype == bt_struct || ztype==bt_class)
268
                                slc += declare(sym,&(tp->lst),sc_member,slc,ztype);
269
                        else
270
                                slc = imax(slc,declare(sym,&(tp->lst),sc_member,0,ztype));
271
                }
272
  }
273
        bit_offset = 0;
274
        bit_next = 0;
275
        bit_width = -1;
276
  tp->size = tp->alignment ? tp->alignment : slc;
277
  NextToken();
278
        isTypedef = ist;
279
}
280
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.