1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* declare.c */ |
4 | /* */ |
5 | /* Parse variable and function declarations */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 1998-2015, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
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 <stdio.h> |
37 | #include <string.h> |
38 | #include <errno.h> |
39 | |
40 | /* common */ |
41 | #include "addrsize.h" |
42 | #include "mmodel.h" |
43 | #include "xmalloc.h" |
44 | |
45 | /* cc65 */ |
46 | #include "anonname.h" |
47 | #include "codegen.h" |
48 | #include "datatype.h" |
49 | #include "declare.h" |
50 | #include "declattr.h" |
51 | #include "error.h" |
52 | #include "expr.h" |
53 | #include "funcdesc.h" |
54 | #include "function.h" |
55 | #include "global.h" |
56 | #include "litpool.h" |
57 | #include "pragma.h" |
58 | #include "scanner.h" |
59 | #include "standard.h" |
60 | #include "symtab.h" |
61 | #include "wrappedcall.h" |
62 | #include "typeconv.h" |
63 | |
64 | |
65 | |
66 | /*****************************************************************************/ |
67 | /* Data */ |
68 | /*****************************************************************************/ |
69 | |
70 | |
71 | |
72 | typedef struct StructInitData StructInitData; |
73 | struct StructInitData { |
74 | unsigned Size; /* Size of struct */ |
75 | unsigned Offs; /* Current offset in struct */ |
76 | unsigned BitVal; /* Summed up bit-field value */ |
77 | unsigned ValBits; /* Valid bits in Val */ |
78 | }; |
79 | |
80 | |
81 | |
82 | /*****************************************************************************/ |
83 | /* Forwards */ |
84 | /*****************************************************************************/ |
85 | |
86 | |
87 | |
88 | static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers); |
89 | /* Parse a type specifier */ |
90 | |
91 | static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers); |
92 | /* Parse initialization of variables. Return the number of data bytes. */ |
93 | |
94 | |
95 | |
96 | /*****************************************************************************/ |
97 | /* Internal functions */ |
98 | /*****************************************************************************/ |
99 | |
100 | |
101 | |
102 | static void DuplicateQualifier (const char* Name) |
103 | /* Print an error message */ |
104 | { |
105 | Warning ("Duplicate qualifier: '%s'" , Name); |
106 | } |
107 | |
108 | |
109 | |
110 | static TypeCode OptionalQualifiers (TypeCode Allowed) |
111 | /* Read type qualifiers if we have any. Allowed specifies the allowed |
112 | ** qualifiers. |
113 | */ |
114 | { |
115 | /* We start without any qualifiers */ |
116 | TypeCode Q = T_QUAL_NONE; |
117 | |
118 | /* Check for more qualifiers */ |
119 | while (1) { |
120 | |
121 | switch (CurTok.Tok) { |
122 | |
123 | case TOK_CONST: |
124 | if (Allowed & T_QUAL_CONST) { |
125 | if (Q & T_QUAL_CONST) { |
126 | DuplicateQualifier ("const" ); |
127 | } |
128 | Q |= T_QUAL_CONST; |
129 | } else { |
130 | goto Done; |
131 | } |
132 | break; |
133 | |
134 | case TOK_VOLATILE: |
135 | if (Allowed & T_QUAL_VOLATILE) { |
136 | if (Q & T_QUAL_VOLATILE) { |
137 | DuplicateQualifier ("volatile" ); |
138 | } |
139 | Q |= T_QUAL_VOLATILE; |
140 | } else { |
141 | goto Done; |
142 | } |
143 | break; |
144 | |
145 | case TOK_RESTRICT: |
146 | if (Allowed & T_QUAL_RESTRICT) { |
147 | if (Q & T_QUAL_RESTRICT) { |
148 | DuplicateQualifier ("restrict" ); |
149 | } |
150 | Q |= T_QUAL_RESTRICT; |
151 | } else { |
152 | goto Done; |
153 | } |
154 | break; |
155 | |
156 | case TOK_NEAR: |
157 | if (Allowed & T_QUAL_NEAR) { |
158 | if (Q & T_QUAL_NEAR) { |
159 | DuplicateQualifier ("near" ); |
160 | } |
161 | Q |= T_QUAL_NEAR; |
162 | } else { |
163 | goto Done; |
164 | } |
165 | break; |
166 | |
167 | case TOK_FAR: |
168 | if (Allowed & T_QUAL_FAR) { |
169 | if (Q & T_QUAL_FAR) { |
170 | DuplicateQualifier ("far" ); |
171 | } |
172 | Q |= T_QUAL_FAR; |
173 | } else { |
174 | goto Done; |
175 | } |
176 | break; |
177 | |
178 | case TOK_FASTCALL: |
179 | if (Allowed & T_QUAL_FASTCALL) { |
180 | if (Q & T_QUAL_FASTCALL) { |
181 | DuplicateQualifier ("fastcall" ); |
182 | } |
183 | Q |= T_QUAL_FASTCALL; |
184 | } else { |
185 | goto Done; |
186 | } |
187 | break; |
188 | |
189 | case TOK_CDECL: |
190 | if (Allowed & T_QUAL_CDECL) { |
191 | if (Q & T_QUAL_CDECL) { |
192 | DuplicateQualifier ("cdecl" ); |
193 | } |
194 | Q |= T_QUAL_CDECL; |
195 | } else { |
196 | goto Done; |
197 | } |
198 | break; |
199 | |
200 | default: |
201 | goto Done; |
202 | |
203 | } |
204 | |
205 | /* Skip the token */ |
206 | NextToken (); |
207 | } |
208 | |
209 | Done: |
210 | /* We cannot have more than one address size far qualifier */ |
211 | switch (Q & T_QUAL_ADDRSIZE) { |
212 | |
213 | case T_QUAL_NONE: |
214 | case T_QUAL_NEAR: |
215 | case T_QUAL_FAR: |
216 | break; |
217 | |
218 | default: |
219 | Error ("Cannot specify more than one address size qualifier" ); |
220 | Q &= ~T_QUAL_ADDRSIZE; |
221 | } |
222 | |
223 | /* We cannot have more than one calling convention specifier */ |
224 | switch (Q & T_QUAL_CCONV) { |
225 | |
226 | case T_QUAL_NONE: |
227 | case T_QUAL_FASTCALL: |
228 | case T_QUAL_CDECL: |
229 | break; |
230 | |
231 | default: |
232 | Error ("Cannot specify more than one calling convention qualifier" ); |
233 | Q &= ~T_QUAL_CCONV; |
234 | } |
235 | |
236 | /* Return the qualifiers read */ |
237 | return Q; |
238 | } |
239 | |
240 | |
241 | |
242 | static void OptionalInt (void) |
243 | /* Eat an optional "int" token */ |
244 | { |
245 | if (CurTok.Tok == TOK_INT) { |
246 | /* Skip it */ |
247 | NextToken (); |
248 | } |
249 | } |
250 | |
251 | |
252 | |
253 | static void OptionalSigned (void) |
254 | /* Eat an optional "signed" token */ |
255 | { |
256 | if (CurTok.Tok == TOK_SIGNED) { |
257 | /* Skip it */ |
258 | NextToken (); |
259 | } |
260 | } |
261 | |
262 | |
263 | |
264 | static void InitDeclSpec (DeclSpec* D) |
265 | /* Initialize the DeclSpec struct for use */ |
266 | { |
267 | D->StorageClass = 0; |
268 | D->Type[0].C = T_END; |
269 | D->Flags = 0; |
270 | } |
271 | |
272 | |
273 | |
274 | static void InitDeclaration (Declaration* D) |
275 | /* Initialize the Declaration struct for use */ |
276 | { |
277 | D->Ident[0] = '\0'; |
278 | D->Type[0].C = T_END; |
279 | D->Index = 0; |
280 | D->Attributes = 0; |
281 | } |
282 | |
283 | |
284 | |
285 | static void NeedTypeSpace (Declaration* D, unsigned Count) |
286 | /* Check if there is enough space for Count type specifiers within D */ |
287 | { |
288 | if (D->Index + Count >= MAXTYPELEN) { |
289 | /* We must call Fatal() here, since calling Error() will try to |
290 | ** continue, and the declaration type is not correctly terminated |
291 | ** in case we come here. |
292 | */ |
293 | Fatal ("Too many type specifiers" ); |
294 | } |
295 | } |
296 | |
297 | |
298 | |
299 | static void AddTypeToDeclaration (Declaration* D, TypeCode T) |
300 | /* Add a type specifier to the type of a declaration */ |
301 | { |
302 | NeedTypeSpace (D, 1); |
303 | D->Type[D->Index++].C = T; |
304 | } |
305 | |
306 | |
307 | |
308 | static void FixQualifiers (Type* DataType) |
309 | /* Apply several fixes to qualifiers */ |
310 | { |
311 | Type* T; |
312 | TypeCode Q; |
313 | |
314 | /* Using typedefs, it is possible to generate declarations that have |
315 | ** type qualifiers attached to an array, not the element type. Go and |
316 | ** fix these here. |
317 | */ |
318 | T = DataType; |
319 | Q = T_QUAL_NONE; |
320 | while (T->C != T_END) { |
321 | if (IsTypeArray (T)) { |
322 | /* Extract any type qualifiers */ |
323 | Q |= GetQualifier (T); |
324 | T->C = UnqualifiedType (T->C); |
325 | } else { |
326 | /* Add extracted type qualifiers here */ |
327 | T->C |= Q; |
328 | Q = T_QUAL_NONE; |
329 | } |
330 | ++T; |
331 | } |
332 | /* Q must be empty now */ |
333 | CHECK (Q == T_QUAL_NONE); |
334 | |
335 | /* Do some fixes on pointers and functions. */ |
336 | T = DataType; |
337 | while (T->C != T_END) { |
338 | if (IsTypePtr (T)) { |
339 | /* Calling convention qualifier on the pointer? */ |
340 | if (IsQualCConv (T)) { |
341 | /* Pull the convention off of the pointer */ |
342 | Q = T[0].C & T_QUAL_CCONV; |
343 | T[0].C &= ~T_QUAL_CCONV; |
344 | |
345 | /* Pointer to a function which doesn't have an explicit convention? */ |
346 | if (IsTypeFunc (T + 1)) { |
347 | if (IsQualCConv (T + 1)) { |
348 | if ((T[1].C & T_QUAL_CCONV) == Q) { |
349 | Warning ("Pointer duplicates function's calling convention" ); |
350 | } else { |
351 | Error ("Function's and pointer's calling conventions are different" ); |
352 | } |
353 | } else { |
354 | if (Q == T_QUAL_FASTCALL && IsVariadicFunc (T + 1)) { |
355 | Error ("Variadic-function pointers cannot be __fastcall__" ); |
356 | } else { |
357 | /* Move the qualifier from the pointer to the function. */ |
358 | T[1].C |= Q; |
359 | } |
360 | } |
361 | } else { |
362 | Error ("Not pointer to a function; can't use a calling convention" ); |
363 | } |
364 | } |
365 | |
366 | /* Apply the default far and near qualifiers if none are given */ |
367 | Q = (T[0].C & T_QUAL_ADDRSIZE); |
368 | if (Q == T_QUAL_NONE) { |
369 | /* No address size qualifiers specified */ |
370 | if (IsTypeFunc (T+1)) { |
371 | /* Pointer to function. Use the qualifier from the function, |
372 | ** or the default if the function doesn't have one. |
373 | */ |
374 | Q = (T[1].C & T_QUAL_ADDRSIZE); |
375 | if (Q == T_QUAL_NONE) { |
376 | Q = CodeAddrSizeQualifier (); |
377 | } |
378 | } else { |
379 | Q = DataAddrSizeQualifier (); |
380 | } |
381 | T[0].C |= Q; |
382 | } else { |
383 | /* We have address size qualifiers. If followed by a function, |
384 | ** apply them to the function also. |
385 | */ |
386 | if (IsTypeFunc (T+1)) { |
387 | TypeCode FQ = (T[1].C & T_QUAL_ADDRSIZE); |
388 | if (FQ == T_QUAL_NONE) { |
389 | T[1].C |= Q; |
390 | } else if (FQ != Q) { |
391 | Error ("Address size qualifier mismatch" ); |
392 | T[1].C = (T[1].C & ~T_QUAL_ADDRSIZE) | Q; |
393 | } |
394 | } |
395 | } |
396 | |
397 | } else if (IsTypeFunc (T)) { |
398 | |
399 | /* Apply the default far and near qualifiers if none are given */ |
400 | if ((T[0].C & T_QUAL_ADDRSIZE) == 0) { |
401 | T[0].C |= CodeAddrSizeQualifier (); |
402 | } |
403 | |
404 | } |
405 | ++T; |
406 | } |
407 | } |
408 | |
409 | |
410 | |
411 | static void ParseStorageClass (DeclSpec* D, unsigned DefStorage) |
412 | /* Parse a storage class */ |
413 | { |
414 | /* Assume we're using an explicit storage class */ |
415 | D->Flags &= ~DS_DEF_STORAGE; |
416 | |
417 | /* Check the storage class given */ |
418 | switch (CurTok.Tok) { |
419 | |
420 | case TOK_EXTERN: |
421 | D->StorageClass = SC_EXTERN | SC_STATIC; |
422 | NextToken (); |
423 | break; |
424 | |
425 | case TOK_STATIC: |
426 | D->StorageClass = SC_STATIC; |
427 | NextToken (); |
428 | break; |
429 | |
430 | case TOK_REGISTER: |
431 | D->StorageClass = SC_REGISTER | SC_STATIC; |
432 | NextToken (); |
433 | break; |
434 | |
435 | case TOK_AUTO: |
436 | D->StorageClass = SC_AUTO; |
437 | NextToken (); |
438 | break; |
439 | |
440 | case TOK_TYPEDEF: |
441 | D->StorageClass = SC_TYPEDEF; |
442 | NextToken (); |
443 | break; |
444 | |
445 | default: |
446 | /* No storage class given, use default */ |
447 | D->Flags |= DS_DEF_STORAGE; |
448 | D->StorageClass = DefStorage; |
449 | break; |
450 | } |
451 | } |
452 | |
453 | |
454 | |
455 | static void ParseEnumDecl (void) |
456 | /* Process an enum declaration . */ |
457 | { |
458 | int EnumVal; |
459 | ident Ident; |
460 | |
461 | /* Accept forward definitions */ |
462 | if (CurTok.Tok != TOK_LCURLY) { |
463 | return; |
464 | } |
465 | |
466 | /* Skip the opening curly brace */ |
467 | NextToken (); |
468 | |
469 | /* Read the enum tags */ |
470 | EnumVal = 0; |
471 | while (CurTok.Tok != TOK_RCURLY) { |
472 | |
473 | /* We expect an identifier */ |
474 | if (CurTok.Tok != TOK_IDENT) { |
475 | Error ("Identifier expected" ); |
476 | continue; |
477 | } |
478 | |
479 | /* Remember the identifier and skip it */ |
480 | strcpy (Ident, CurTok.Ident); |
481 | NextToken (); |
482 | |
483 | /* Check for an assigned value */ |
484 | if (CurTok.Tok == TOK_ASSIGN) { |
485 | ExprDesc Expr; |
486 | NextToken (); |
487 | ConstAbsIntExpr (hie1, &Expr); |
488 | EnumVal = Expr.IVal; |
489 | } |
490 | |
491 | /* Add an entry to the symbol table */ |
492 | AddConstSym (Ident, type_int, SC_ENUM, EnumVal++); |
493 | |
494 | /* Check for end of definition */ |
495 | if (CurTok.Tok != TOK_COMMA) |
496 | break; |
497 | NextToken (); |
498 | } |
499 | ConsumeRCurly (); |
500 | } |
501 | |
502 | |
503 | |
504 | static int ParseFieldWidth (Declaration* Decl) |
505 | /* Parse an optional field width. Returns -1 if no field width is specified, |
506 | ** otherwise the width of the field. |
507 | */ |
508 | { |
509 | ExprDesc Expr; |
510 | |
511 | if (CurTok.Tok != TOK_COLON) { |
512 | /* No bit-field declaration */ |
513 | return -1; |
514 | } |
515 | |
516 | /* Read the width */ |
517 | NextToken (); |
518 | ConstAbsIntExpr (hie1, &Expr); |
519 | if (Expr.IVal < 0) { |
520 | Error ("Negative width in bit-field" ); |
521 | return -1; |
522 | } |
523 | if (Expr.IVal > (int) INT_BITS) { |
524 | Error ("Width of bit-field exceeds its type" ); |
525 | return -1; |
526 | } |
527 | if (Expr.IVal == 0 && Decl->Ident[0] != '\0') { |
528 | Error ("Zero width for named bit-field" ); |
529 | return -1; |
530 | } |
531 | if (!IsTypeInt (Decl->Type)) { |
532 | /* Only integer types may be used for bit-fields */ |
533 | Error ("Bit-field has invalid type" ); |
534 | return -1; |
535 | } |
536 | |
537 | /* Return the field width */ |
538 | return (int) Expr.IVal; |
539 | } |
540 | |
541 | |
542 | |
543 | static SymEntry* StructOrUnionForwardDecl (const char* Name, unsigned Type) |
544 | /* Handle a struct or union forward decl */ |
545 | { |
546 | /* Try to find a struct/union with the given name. If there is none, |
547 | ** insert a forward declaration into the current lexical level. |
548 | */ |
549 | SymEntry* Entry = FindTagSym (Name); |
550 | if (Entry == 0) { |
551 | Entry = AddStructSym (Name, Type, 0, 0); |
552 | } else if ((Entry->Flags & SC_TYPEMASK) != Type) { |
553 | /* Already defined, but no struct */ |
554 | Error ("Symbol '%s' is already different kind" , Name); |
555 | } |
556 | return Entry; |
557 | } |
558 | |
559 | |
560 | |
561 | static unsigned CopyAnonStructFields (const Declaration* Decl, int Offs) |
562 | /* Copy fields from an anon union/struct into the current lexical level. The |
563 | ** function returns the size of the embedded struct/union. |
564 | */ |
565 | { |
566 | /* Get the pointer to the symbol table entry of the anon struct */ |
567 | SymEntry* Entry = GetSymEntry (Decl->Type); |
568 | |
569 | /* Get the size of the anon struct */ |
570 | unsigned Size = Entry->V.S.Size; |
571 | |
572 | /* Get the symbol table containing the fields. If it is empty, there has |
573 | ** been an error before, so bail out. |
574 | */ |
575 | SymTable* Tab = Entry->V.S.SymTab; |
576 | if (Tab == 0) { |
577 | /* Incomplete definition - has been flagged before */ |
578 | return Size; |
579 | } |
580 | |
581 | /* Get a pointer to the list of symbols. Then walk the list adding copies |
582 | ** of the embedded struct to the current level. |
583 | */ |
584 | Entry = Tab->SymHead; |
585 | while (Entry) { |
586 | |
587 | /* Enter a copy of this symbol adjusting the offset. We will just |
588 | ** reuse the type string here. |
589 | */ |
590 | AddLocalSym (Entry->Name, Entry->Type, SC_STRUCTFIELD, Offs + Entry->V.Offs); |
591 | |
592 | /* Currently, there can not be any attributes, but if there will be |
593 | ** some in the future, we want to know this. |
594 | */ |
595 | CHECK (Entry->Attr == 0); |
596 | |
597 | /* Next entry */ |
598 | Entry = Entry->NextSym; |
599 | } |
600 | |
601 | /* Return the size of the embedded struct */ |
602 | return Size; |
603 | } |
604 | |
605 | |
606 | |
607 | static SymEntry* ParseUnionDecl (const char* Name) |
608 | /* Parse a union declaration. */ |
609 | { |
610 | |
611 | unsigned UnionSize; |
612 | unsigned FieldSize; |
613 | int FieldWidth; /* Width in bits, -1 if not a bit-field */ |
614 | SymTable* FieldTab; |
615 | |
616 | |
617 | if (CurTok.Tok != TOK_LCURLY) { |
618 | /* Just a forward declaration. */ |
619 | return StructOrUnionForwardDecl (Name, SC_UNION); |
620 | } |
621 | |
622 | /* Add a forward declaration for the struct in the current lexical level */ |
623 | AddStructSym (Name, SC_UNION, 0, 0); |
624 | |
625 | /* Skip the curly brace */ |
626 | NextToken (); |
627 | |
628 | /* Enter a new lexical level for the struct */ |
629 | EnterStructLevel (); |
630 | |
631 | /* Parse union fields */ |
632 | UnionSize = 0; |
633 | while (CurTok.Tok != TOK_RCURLY) { |
634 | |
635 | /* Get the type of the entry */ |
636 | DeclSpec Spec; |
637 | InitDeclSpec (&Spec); |
638 | ParseTypeSpec (&Spec, -1, T_QUAL_NONE); |
639 | |
640 | /* Read fields with this type */ |
641 | while (1) { |
642 | |
643 | Declaration Decl; |
644 | |
645 | /* Get type and name of the struct field */ |
646 | ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT); |
647 | |
648 | /* Check for a bit-field declaration */ |
649 | FieldWidth = ParseFieldWidth (&Decl); |
650 | |
651 | /* Ignore zero sized bit fields in a union */ |
652 | if (FieldWidth == 0) { |
653 | goto NextMember; |
654 | } |
655 | |
656 | /* Check for fields without a name */ |
657 | if (Decl.Ident[0] == '\0') { |
658 | /* In cc65 mode, we allow anonymous structs/unions within |
659 | ** a union. |
660 | */ |
661 | if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) { |
662 | /* This is an anonymous struct or union. Copy the fields |
663 | ** into the current level. |
664 | */ |
665 | FieldSize = CopyAnonStructFields (&Decl, 0); |
666 | if (FieldSize > UnionSize) { |
667 | UnionSize = FieldSize; |
668 | } |
669 | |
670 | } else { |
671 | /* A non bit-field without a name is legal but useless */ |
672 | Warning ("Declaration does not declare anything" ); |
673 | } |
674 | goto NextMember; |
675 | } |
676 | |
677 | /* Handle sizes */ |
678 | FieldSize = CheckedSizeOf (Decl.Type); |
679 | if (FieldSize > UnionSize) { |
680 | UnionSize = FieldSize; |
681 | } |
682 | |
683 | /* Add a field entry to the table. */ |
684 | if (FieldWidth > 0) { |
685 | AddBitField (Decl.Ident, 0, 0, FieldWidth); |
686 | } else { |
687 | AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, 0); |
688 | } |
689 | |
690 | NextMember: if (CurTok.Tok != TOK_COMMA) { |
691 | break; |
692 | } |
693 | NextToken (); |
694 | } |
695 | ConsumeSemi (); |
696 | } |
697 | |
698 | /* Skip the closing brace */ |
699 | NextToken (); |
700 | |
701 | /* Remember the symbol table and leave the struct level */ |
702 | FieldTab = GetSymTab (); |
703 | LeaveStructLevel (); |
704 | |
705 | /* Make a real entry from the forward decl and return it */ |
706 | return AddStructSym (Name, SC_UNION, UnionSize, FieldTab); |
707 | } |
708 | |
709 | |
710 | |
711 | static SymEntry* ParseStructDecl (const char* Name) |
712 | /* Parse a struct declaration. */ |
713 | { |
714 | |
715 | unsigned StructSize; |
716 | int FlexibleMember; |
717 | int BitOffs; /* Bit offset for bit-fields */ |
718 | int FieldWidth; /* Width in bits, -1 if not a bit-field */ |
719 | SymTable* FieldTab; |
720 | |
721 | |
722 | if (CurTok.Tok != TOK_LCURLY) { |
723 | /* Just a forward declaration. */ |
724 | return StructOrUnionForwardDecl (Name, SC_STRUCT); |
725 | } |
726 | |
727 | /* Add a forward declaration for the struct in the current lexical level */ |
728 | AddStructSym (Name, SC_STRUCT, 0, 0); |
729 | |
730 | /* Skip the curly brace */ |
731 | NextToken (); |
732 | |
733 | /* Enter a new lexical level for the struct */ |
734 | EnterStructLevel (); |
735 | |
736 | /* Parse struct fields */ |
737 | FlexibleMember = 0; |
738 | StructSize = 0; |
739 | BitOffs = 0; |
740 | while (CurTok.Tok != TOK_RCURLY) { |
741 | |
742 | /* Get the type of the entry */ |
743 | DeclSpec Spec; |
744 | InitDeclSpec (&Spec); |
745 | ParseTypeSpec (&Spec, -1, T_QUAL_NONE); |
746 | |
747 | /* Read fields with this type */ |
748 | while (1) { |
749 | |
750 | Declaration Decl; |
751 | ident Ident; |
752 | |
753 | /* If we had a flexible array member before, no other fields can |
754 | ** follow. |
755 | */ |
756 | if (FlexibleMember) { |
757 | Error ("Flexible array member must be last field" ); |
758 | FlexibleMember = 0; /* Avoid further errors */ |
759 | } |
760 | |
761 | /* Get type and name of the struct field */ |
762 | ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT); |
763 | |
764 | /* Check for a bit-field declaration */ |
765 | FieldWidth = ParseFieldWidth (&Decl); |
766 | |
767 | /* If this is not a bit field, or the bit field is too large for |
768 | ** the remainder of the current member, or we have a bit field |
769 | ** with width zero, align the struct to the next member by adding |
770 | ** a member with an anonymous name. |
771 | */ |
772 | if (BitOffs > 0) { |
773 | if (FieldWidth <= 0 || (BitOffs + FieldWidth) > (int) INT_BITS) { |
774 | |
775 | /* We need an anonymous name */ |
776 | AnonName (Ident, "bit-field" ); |
777 | |
778 | /* Add an anonymous bit-field that aligns to the next |
779 | ** storage unit. |
780 | */ |
781 | AddBitField (Ident, StructSize, BitOffs, INT_BITS - BitOffs); |
782 | |
783 | /* No bits left */ |
784 | StructSize += SIZEOF_INT; |
785 | BitOffs = 0; |
786 | } |
787 | } |
788 | |
789 | /* Apart from the above, a bit field with width 0 is not processed |
790 | ** further. |
791 | */ |
792 | if (FieldWidth == 0) { |
793 | goto NextMember; |
794 | } |
795 | |
796 | /* Check if this field is a flexible array member, and |
797 | ** calculate the size of the field. |
798 | */ |
799 | if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) { |
800 | /* Array with unspecified size */ |
801 | if (StructSize == 0) { |
802 | Error ("Flexible array member cannot be first struct field" ); |
803 | } |
804 | FlexibleMember = 1; |
805 | /* Assume zero for size calculations */ |
806 | SetElementCount (Decl.Type, FLEXIBLE); |
807 | } |
808 | |
809 | /* Check for fields without names */ |
810 | if (Decl.Ident[0] == '\0') { |
811 | if (FieldWidth < 0) { |
812 | /* In cc65 mode, we allow anonymous structs/unions within |
813 | ** a struct. |
814 | */ |
815 | if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) { |
816 | |
817 | /* This is an anonymous struct or union. Copy the |
818 | ** fields into the current level. |
819 | */ |
820 | StructSize += CopyAnonStructFields (&Decl, StructSize); |
821 | |
822 | } else { |
823 | /* A non bit-field without a name is legal but useless */ |
824 | Warning ("Declaration does not declare anything" ); |
825 | } |
826 | goto NextMember; |
827 | } else { |
828 | /* A bit-field without a name will get an anonymous one */ |
829 | AnonName (Decl.Ident, "bit-field" ); |
830 | } |
831 | } |
832 | |
833 | /* Add a field entry to the table */ |
834 | if (FieldWidth > 0) { |
835 | /* Add full byte from the bit offset to the variable offset. |
836 | ** This simplifies handling he bit-field as a char type |
837 | ** in expressions. |
838 | */ |
839 | unsigned Offs = StructSize + (BitOffs / CHAR_BITS); |
840 | AddBitField (Decl.Ident, Offs, BitOffs % CHAR_BITS, FieldWidth); |
841 | BitOffs += FieldWidth; |
842 | CHECK (BitOffs <= (int) INT_BITS); |
843 | if (BitOffs == INT_BITS) { |
844 | StructSize += SIZEOF_INT; |
845 | BitOffs = 0; |
846 | } |
847 | } else { |
848 | AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, StructSize); |
849 | if (!FlexibleMember) { |
850 | StructSize += CheckedSizeOf (Decl.Type); |
851 | } |
852 | } |
853 | |
854 | NextMember: if (CurTok.Tok != TOK_COMMA) { |
855 | break; |
856 | } |
857 | NextToken (); |
858 | } |
859 | ConsumeSemi (); |
860 | } |
861 | |
862 | /* If we have bits from bit-fields left, add them to the size. */ |
863 | if (BitOffs > 0) { |
864 | StructSize += ((BitOffs + CHAR_BITS - 1) / CHAR_BITS); |
865 | } |
866 | |
867 | /* Skip the closing brace */ |
868 | NextToken (); |
869 | |
870 | /* Remember the symbol table and leave the struct level */ |
871 | FieldTab = GetSymTab (); |
872 | LeaveStructLevel (); |
873 | |
874 | /* Make a real entry from the forward decl and return it */ |
875 | return AddStructSym (Name, SC_STRUCT, StructSize, FieldTab); |
876 | } |
877 | |
878 | |
879 | |
880 | static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers) |
881 | /* Parse a type specifier */ |
882 | { |
883 | ident Ident; |
884 | SymEntry* Entry; |
885 | |
886 | /* Assume we have an explicit type */ |
887 | D->Flags &= ~DS_DEF_TYPE; |
888 | |
889 | /* Read type qualifiers if we have any */ |
890 | Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE); |
891 | |
892 | /* Look at the data type */ |
893 | switch (CurTok.Tok) { |
894 | |
895 | case TOK_VOID: |
896 | NextToken (); |
897 | D->Type[0].C = T_VOID; |
898 | D->Type[0].A.U = 0; |
899 | D->Type[1].C = T_END; |
900 | break; |
901 | |
902 | case TOK_CHAR: |
903 | NextToken (); |
904 | D->Type[0].C = GetDefaultChar(); |
905 | D->Type[1].C = T_END; |
906 | break; |
907 | |
908 | case TOK_LONG: |
909 | NextToken (); |
910 | if (CurTok.Tok == TOK_UNSIGNED) { |
911 | NextToken (); |
912 | OptionalInt (); |
913 | D->Type[0].C = T_ULONG; |
914 | D->Type[1].C = T_END; |
915 | } else { |
916 | OptionalSigned (); |
917 | OptionalInt (); |
918 | D->Type[0].C = T_LONG; |
919 | D->Type[1].C = T_END; |
920 | } |
921 | break; |
922 | |
923 | case TOK_SHORT: |
924 | NextToken (); |
925 | if (CurTok.Tok == TOK_UNSIGNED) { |
926 | NextToken (); |
927 | OptionalInt (); |
928 | D->Type[0].C = T_USHORT; |
929 | D->Type[1].C = T_END; |
930 | } else { |
931 | OptionalSigned (); |
932 | OptionalInt (); |
933 | D->Type[0].C = T_SHORT; |
934 | D->Type[1].C = T_END; |
935 | } |
936 | break; |
937 | |
938 | case TOK_INT: |
939 | NextToken (); |
940 | D->Type[0].C = T_INT; |
941 | D->Type[1].C = T_END; |
942 | break; |
943 | |
944 | case TOK_SIGNED: |
945 | NextToken (); |
946 | switch (CurTok.Tok) { |
947 | |
948 | case TOK_CHAR: |
949 | NextToken (); |
950 | D->Type[0].C = T_SCHAR; |
951 | D->Type[1].C = T_END; |
952 | break; |
953 | |
954 | case TOK_SHORT: |
955 | NextToken (); |
956 | OptionalInt (); |
957 | D->Type[0].C = T_SHORT; |
958 | D->Type[1].C = T_END; |
959 | break; |
960 | |
961 | case TOK_LONG: |
962 | NextToken (); |
963 | OptionalInt (); |
964 | D->Type[0].C = T_LONG; |
965 | D->Type[1].C = T_END; |
966 | break; |
967 | |
968 | case TOK_INT: |
969 | NextToken (); |
970 | /* FALL THROUGH */ |
971 | |
972 | default: |
973 | D->Type[0].C = T_INT; |
974 | D->Type[1].C = T_END; |
975 | break; |
976 | } |
977 | break; |
978 | |
979 | case TOK_UNSIGNED: |
980 | NextToken (); |
981 | switch (CurTok.Tok) { |
982 | |
983 | case TOK_CHAR: |
984 | NextToken (); |
985 | D->Type[0].C = T_UCHAR; |
986 | D->Type[1].C = T_END; |
987 | break; |
988 | |
989 | case TOK_SHORT: |
990 | NextToken (); |
991 | OptionalInt (); |
992 | D->Type[0].C = T_USHORT; |
993 | D->Type[1].C = T_END; |
994 | break; |
995 | |
996 | case TOK_LONG: |
997 | NextToken (); |
998 | OptionalInt (); |
999 | D->Type[0].C = T_ULONG; |
1000 | D->Type[1].C = T_END; |
1001 | break; |
1002 | |
1003 | case TOK_INT: |
1004 | NextToken (); |
1005 | /* FALL THROUGH */ |
1006 | |
1007 | default: |
1008 | D->Type[0].C = T_UINT; |
1009 | D->Type[1].C = T_END; |
1010 | break; |
1011 | } |
1012 | break; |
1013 | |
1014 | case TOK_FLOAT: |
1015 | NextToken (); |
1016 | D->Type[0].C = T_FLOAT; |
1017 | D->Type[1].C = T_END; |
1018 | break; |
1019 | |
1020 | case TOK_DOUBLE: |
1021 | NextToken (); |
1022 | D->Type[0].C = T_DOUBLE; |
1023 | D->Type[1].C = T_END; |
1024 | break; |
1025 | |
1026 | case TOK_UNION: |
1027 | NextToken (); |
1028 | /* */ |
1029 | if (CurTok.Tok == TOK_IDENT) { |
1030 | strcpy (Ident, CurTok.Ident); |
1031 | NextToken (); |
1032 | } else { |
1033 | AnonName (Ident, "union" ); |
1034 | } |
1035 | /* Remember we have an extra type decl */ |
1036 | D->Flags |= DS_EXTRA_TYPE; |
1037 | /* Declare the union in the current scope */ |
1038 | Entry = ParseUnionDecl (Ident); |
1039 | /* Encode the union entry into the type */ |
1040 | D->Type[0].C = T_UNION; |
1041 | SetSymEntry (D->Type, Entry); |
1042 | D->Type[1].C = T_END; |
1043 | break; |
1044 | |
1045 | case TOK_STRUCT: |
1046 | NextToken (); |
1047 | /* */ |
1048 | if (CurTok.Tok == TOK_IDENT) { |
1049 | strcpy (Ident, CurTok.Ident); |
1050 | NextToken (); |
1051 | } else { |
1052 | AnonName (Ident, "struct" ); |
1053 | } |
1054 | /* Remember we have an extra type decl */ |
1055 | D->Flags |= DS_EXTRA_TYPE; |
1056 | /* Declare the struct in the current scope */ |
1057 | Entry = ParseStructDecl (Ident); |
1058 | /* Encode the struct entry into the type */ |
1059 | D->Type[0].C = T_STRUCT; |
1060 | SetSymEntry (D->Type, Entry); |
1061 | D->Type[1].C = T_END; |
1062 | break; |
1063 | |
1064 | case TOK_ENUM: |
1065 | NextToken (); |
1066 | if (CurTok.Tok != TOK_LCURLY) { |
1067 | /* Named enum */ |
1068 | if (CurTok.Tok == TOK_IDENT) { |
1069 | /* Find an entry with this name */ |
1070 | Entry = FindTagSym (CurTok.Ident); |
1071 | if (Entry) { |
1072 | if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) { |
1073 | Error ("Symbol '%s' is already different kind" , Entry->Name); |
1074 | } |
1075 | } else { |
1076 | /* Insert entry into table ### */ |
1077 | } |
1078 | /* Skip the identifier */ |
1079 | NextToken (); |
1080 | } else { |
1081 | Error ("Identifier expected" ); |
1082 | } |
1083 | } |
1084 | /* Remember we have an extra type decl */ |
1085 | D->Flags |= DS_EXTRA_TYPE; |
1086 | /* Parse the enum decl */ |
1087 | ParseEnumDecl (); |
1088 | D->Type[0].C = T_INT; |
1089 | D->Type[1].C = T_END; |
1090 | break; |
1091 | |
1092 | case TOK_IDENT: |
1093 | Entry = FindSym (CurTok.Ident); |
1094 | if (Entry && SymIsTypeDef (Entry)) { |
1095 | /* It's a typedef */ |
1096 | NextToken (); |
1097 | TypeCopy (D->Type, Entry->Type); |
1098 | break; |
1099 | } |
1100 | /* FALL THROUGH */ |
1101 | |
1102 | default: |
1103 | if (Default < 0) { |
1104 | Error ("Type expected" ); |
1105 | D->Type[0].C = T_INT; |
1106 | D->Type[1].C = T_END; |
1107 | } else { |
1108 | D->Flags |= DS_DEF_TYPE; |
1109 | D->Type[0].C = (TypeCode) Default; |
1110 | D->Type[1].C = T_END; |
1111 | } |
1112 | break; |
1113 | } |
1114 | |
1115 | /* There may also be qualifiers *after* the initial type */ |
1116 | D->Type[0].C |= (Qualifiers | OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE)); |
1117 | } |
1118 | |
1119 | |
1120 | |
1121 | static Type* ParamTypeCvt (Type* T) |
1122 | /* If T is an array, convert it to a pointer else do nothing. Return the |
1123 | ** resulting type. |
1124 | */ |
1125 | { |
1126 | if (IsTypeArray (T)) { |
1127 | T->C = T_PTR; |
1128 | } |
1129 | return T; |
1130 | } |
1131 | |
1132 | |
1133 | |
1134 | static void ParseOldStyleParamList (FuncDesc* F) |
1135 | /* Parse an old-style (K&R) parameter list */ |
1136 | { |
1137 | /* Some fix point tokens that are used for error recovery */ |
1138 | static const token_t TokenList[] = { TOK_COMMA, TOK_RPAREN, TOK_SEMI }; |
1139 | |
1140 | /* Parse params */ |
1141 | while (CurTok.Tok != TOK_RPAREN) { |
1142 | |
1143 | /* List of identifiers expected */ |
1144 | if (CurTok.Tok == TOK_IDENT) { |
1145 | |
1146 | /* Create a symbol table entry with type int */ |
1147 | AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0); |
1148 | |
1149 | /* Count arguments */ |
1150 | ++F->ParamCount; |
1151 | |
1152 | /* Skip the identifier */ |
1153 | NextToken (); |
1154 | |
1155 | } else { |
1156 | /* Not a parameter name */ |
1157 | Error ("Identifier expected" ); |
1158 | |
1159 | /* Try some smart error recovery */ |
1160 | SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0])); |
1161 | } |
1162 | |
1163 | /* Check for more parameters */ |
1164 | if (CurTok.Tok == TOK_COMMA) { |
1165 | NextToken (); |
1166 | } else { |
1167 | break; |
1168 | } |
1169 | } |
1170 | |
1171 | /* Skip right paren. We must explicitly check for one here, since some of |
1172 | ** the breaks above bail out without checking. |
1173 | */ |
1174 | ConsumeRParen (); |
1175 | |
1176 | /* An optional list of type specifications follows */ |
1177 | while (CurTok.Tok != TOK_LCURLY) { |
1178 | |
1179 | DeclSpec Spec; |
1180 | |
1181 | /* Read the declaration specifier */ |
1182 | ParseDeclSpec (&Spec, SC_AUTO, T_INT); |
1183 | |
1184 | /* We accept only auto and register as storage class specifiers, but |
1185 | ** we ignore all this, since we use auto anyway. |
1186 | */ |
1187 | if ((Spec.StorageClass & SC_AUTO) == 0 && |
1188 | (Spec.StorageClass & SC_REGISTER) == 0) { |
1189 | Error ("Illegal storage class" ); |
1190 | } |
1191 | |
1192 | /* Parse a comma separated variable list */ |
1193 | while (1) { |
1194 | |
1195 | Declaration Decl; |
1196 | |
1197 | /* Read the parameter */ |
1198 | ParseDecl (&Spec, &Decl, DM_NEED_IDENT); |
1199 | if (Decl.Ident[0] != '\0') { |
1200 | |
1201 | /* We have a name given. Search for the symbol */ |
1202 | SymEntry* Sym = FindLocalSym (Decl.Ident); |
1203 | if (Sym) { |
1204 | /* Check if we already changed the type for this |
1205 | ** parameter |
1206 | */ |
1207 | if (Sym->Flags & SC_DEFTYPE) { |
1208 | /* Found it, change the default type to the one given */ |
1209 | ChangeSymType (Sym, ParamTypeCvt (Decl.Type)); |
1210 | /* Reset the "default type" flag */ |
1211 | Sym->Flags &= ~SC_DEFTYPE; |
1212 | } else { |
1213 | /* Type has already been changed */ |
1214 | Error ("Redefinition for parameter '%s'" , Sym->Name); |
1215 | } |
1216 | } else { |
1217 | Error ("Unknown identifier: '%s'" , Decl.Ident); |
1218 | } |
1219 | } |
1220 | |
1221 | if (CurTok.Tok == TOK_COMMA) { |
1222 | NextToken (); |
1223 | } else { |
1224 | break; |
1225 | } |
1226 | |
1227 | } |
1228 | |
1229 | /* Variable list must be semicolon terminated */ |
1230 | ConsumeSemi (); |
1231 | } |
1232 | } |
1233 | |
1234 | |
1235 | |
1236 | static void ParseAnsiParamList (FuncDesc* F) |
1237 | /* Parse a new-style (ANSI) parameter list */ |
1238 | { |
1239 | /* Parse params */ |
1240 | while (CurTok.Tok != TOK_RPAREN) { |
1241 | |
1242 | DeclSpec Spec; |
1243 | Declaration Decl; |
1244 | SymEntry* Sym; |
1245 | |
1246 | /* Allow an ellipsis as last parameter */ |
1247 | if (CurTok.Tok == TOK_ELLIPSIS) { |
1248 | NextToken (); |
1249 | F->Flags |= FD_VARIADIC; |
1250 | break; |
1251 | } |
1252 | |
1253 | /* Read the declaration specifier */ |
1254 | ParseDeclSpec (&Spec, SC_AUTO, T_INT); |
1255 | |
1256 | /* We accept only auto and register as storage class specifiers */ |
1257 | if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) { |
1258 | Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF; |
1259 | } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) { |
1260 | Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF; |
1261 | } else { |
1262 | Error ("Illegal storage class" ); |
1263 | Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF; |
1264 | } |
1265 | |
1266 | /* Allow parameters without a name, but remember if we had some to |
1267 | ** eventually print an error message later. |
1268 | */ |
1269 | ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT); |
1270 | if (Decl.Ident[0] == '\0') { |
1271 | |
1272 | /* Unnamed symbol. Generate a name that is not user accessible, |
1273 | ** then handle the symbol normal. |
1274 | */ |
1275 | AnonName (Decl.Ident, "param" ); |
1276 | F->Flags |= FD_UNNAMED_PARAMS; |
1277 | |
1278 | /* Clear defined bit on nonames */ |
1279 | Decl.StorageClass &= ~SC_DEF; |
1280 | } |
1281 | |
1282 | /* Parse attributes for this parameter */ |
1283 | ParseAttribute (&Decl); |
1284 | |
1285 | /* Create a symbol table entry */ |
1286 | Sym = AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Decl.StorageClass, 0); |
1287 | |
1288 | /* Add attributes if we have any */ |
1289 | SymUseAttr (Sym, &Decl); |
1290 | |
1291 | /* If the parameter is a struct or union, emit a warning */ |
1292 | if (IsClassStruct (Decl.Type)) { |
1293 | if (IS_Get (&WarnStructParam)) { |
1294 | Warning ("Passing struct by value for parameter '%s'" , Decl.Ident); |
1295 | } |
1296 | } |
1297 | |
1298 | /* Count arguments */ |
1299 | ++F->ParamCount; |
1300 | |
1301 | /* Check for more parameters */ |
1302 | if (CurTok.Tok == TOK_COMMA) { |
1303 | NextToken (); |
1304 | } else { |
1305 | break; |
1306 | } |
1307 | } |
1308 | |
1309 | /* Skip right paren. We must explicitly check for one here, since some of |
1310 | ** the breaks above bail out without checking. |
1311 | */ |
1312 | ConsumeRParen (); |
1313 | } |
1314 | |
1315 | |
1316 | |
1317 | static FuncDesc* ParseFuncDecl (void) |
1318 | /* Parse the argument list of a function. */ |
1319 | { |
1320 | unsigned Offs; |
1321 | SymEntry* Sym; |
1322 | SymEntry* WrappedCall; |
1323 | unsigned char WrappedCallData; |
1324 | |
1325 | /* Create a new function descriptor */ |
1326 | FuncDesc* F = NewFuncDesc (); |
1327 | |
1328 | /* Enter a new lexical level */ |
1329 | EnterFunctionLevel (); |
1330 | |
1331 | /* Check for several special parameter lists */ |
1332 | if (CurTok.Tok == TOK_RPAREN) { |
1333 | /* Parameter list is empty (K&R-style) */ |
1334 | F->Flags |= FD_EMPTY; |
1335 | } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) { |
1336 | /* Parameter list declared as void */ |
1337 | NextToken (); |
1338 | F->Flags |= FD_VOID_PARAM; |
1339 | } else if (CurTok.Tok == TOK_IDENT && |
1340 | (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) { |
1341 | /* If the identifier is a typedef, we have a new-style parameter list; |
1342 | ** if it's some other identifier, it's an old-style parameter list. |
1343 | */ |
1344 | Sym = FindSym (CurTok.Ident); |
1345 | if (Sym == 0 || !SymIsTypeDef (Sym)) { |
1346 | /* Old-style (K&R) function. */ |
1347 | F->Flags |= FD_OLDSTYLE; |
1348 | } |
1349 | } |
1350 | |
1351 | /* Parse params */ |
1352 | if ((F->Flags & FD_OLDSTYLE) == 0) { |
1353 | /* New-style function */ |
1354 | ParseAnsiParamList (F); |
1355 | } else { |
1356 | /* Old-style function */ |
1357 | ParseOldStyleParamList (F); |
1358 | } |
1359 | |
1360 | /* Remember the last function parameter. We need it later for several |
1361 | ** purposes, for example when passing stuff to fastcall functions. Since |
1362 | ** more symbols are added to the table, it is easier if we remember it |
1363 | ** now, since it is currently the last entry in the symbol table. |
1364 | */ |
1365 | F->LastParam = GetSymTab()->SymTail; |
1366 | |
1367 | /* Assign offsets. If the function has a variable parameter list, |
1368 | ** there's one additional byte (the arg size). |
1369 | */ |
1370 | Offs = (F->Flags & FD_VARIADIC)? 1 : 0; |
1371 | Sym = F->LastParam; |
1372 | while (Sym) { |
1373 | unsigned Size = CheckedSizeOf (Sym->Type); |
1374 | if (SymIsRegVar (Sym)) { |
1375 | Sym->V.R.SaveOffs = Offs; |
1376 | } else { |
1377 | Sym->V.Offs = Offs; |
1378 | } |
1379 | Offs += Size; |
1380 | F->ParamSize += Size; |
1381 | Sym = Sym->PrevSym; |
1382 | } |
1383 | |
1384 | /* Leave the lexical level remembering the symbol tables */ |
1385 | RememberFunctionLevel (F); |
1386 | |
1387 | /* Did we have a WrappedCall for this function? */ |
1388 | GetWrappedCall((void **) &WrappedCall, &WrappedCallData); |
1389 | if (WrappedCall) { |
1390 | F->WrappedCall = WrappedCall; |
1391 | F->WrappedCallData = WrappedCallData; |
1392 | } |
1393 | |
1394 | /* Return the function descriptor */ |
1395 | return F; |
1396 | } |
1397 | |
1398 | |
1399 | |
1400 | static void Declarator (const DeclSpec* Spec, Declaration* D, declmode_t Mode) |
1401 | /* Recursively process declarators. Build a type array in reverse order. */ |
1402 | { |
1403 | /* Read optional function or pointer qualifiers. They modify the |
1404 | ** identifier or token to the right. For convenience, we allow a calling |
1405 | ** convention also for pointers here. If it's a pointer-to-function, the |
1406 | ** qualifier later will be transfered to the function itself. If it's a |
1407 | ** pointer to something else, it will be flagged as an error. |
1408 | */ |
1409 | TypeCode Qualifiers = OptionalQualifiers (T_QUAL_ADDRSIZE | T_QUAL_CCONV); |
1410 | |
1411 | /* Pointer to something */ |
1412 | if (CurTok.Tok == TOK_STAR) { |
1413 | |
1414 | /* Skip the star */ |
1415 | NextToken (); |
1416 | |
1417 | /* Allow const, restrict, and volatile qualifiers */ |
1418 | Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE | T_QUAL_RESTRICT); |
1419 | |
1420 | /* Parse the type that the pointer points to */ |
1421 | Declarator (Spec, D, Mode); |
1422 | |
1423 | /* Add the type */ |
1424 | AddTypeToDeclaration (D, T_PTR | Qualifiers); |
1425 | return; |
1426 | } |
1427 | |
1428 | if (CurTok.Tok == TOK_LPAREN) { |
1429 | NextToken (); |
1430 | Declarator (Spec, D, Mode); |
1431 | ConsumeRParen (); |
1432 | } else { |
1433 | /* Things depend on Mode now: |
1434 | ** - Mode == DM_NEED_IDENT means: |
1435 | ** we *must* have a type and a variable identifer. |
1436 | ** - Mode == DM_NO_IDENT means: |
1437 | ** we must have a type but no variable identifer |
1438 | ** (if there is one, it's not read). |
1439 | ** - Mode == DM_ACCEPT_IDENT means: |
1440 | ** we *may* have an identifier. If there is an identifier, |
1441 | ** it is read, but it is no error, if there is none. |
1442 | */ |
1443 | if (Mode == DM_NO_IDENT) { |
1444 | D->Ident[0] = '\0'; |
1445 | } else if (CurTok.Tok == TOK_IDENT) { |
1446 | strcpy (D->Ident, CurTok.Ident); |
1447 | NextToken (); |
1448 | } else { |
1449 | if (Mode == DM_NEED_IDENT) { |
1450 | Error ("Identifier expected" ); |
1451 | } |
1452 | D->Ident[0] = '\0'; |
1453 | } |
1454 | } |
1455 | |
1456 | while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) { |
1457 | if (CurTok.Tok == TOK_LPAREN) { |
1458 | |
1459 | /* Function declaration */ |
1460 | FuncDesc* F; |
1461 | SymEntry* PrevEntry; |
1462 | |
1463 | /* Skip the opening paren */ |
1464 | NextToken (); |
1465 | |
1466 | /* Parse the function declaration */ |
1467 | F = ParseFuncDecl (); |
1468 | |
1469 | /* We cannot specify fastcall for variadic functions */ |
1470 | if ((F->Flags & FD_VARIADIC) && (Qualifiers & T_QUAL_FASTCALL)) { |
1471 | Error ("Variadic functions cannot be __fastcall__" ); |
1472 | Qualifiers &= ~T_QUAL_FASTCALL; |
1473 | } |
1474 | |
1475 | /* Was there a previous entry? If so, copy WrappedCall info from it */ |
1476 | PrevEntry = FindGlobalSym (D->Ident); |
1477 | if (PrevEntry && PrevEntry->Flags & SC_FUNC) { |
1478 | FuncDesc* D = PrevEntry->V.F.Func; |
1479 | if (D->WrappedCall && !F->WrappedCall) { |
1480 | F->WrappedCall = D->WrappedCall; |
1481 | F->WrappedCallData = D->WrappedCallData; |
1482 | } |
1483 | } |
1484 | |
1485 | /* Add the function type. Be sure to bounds check the type buffer */ |
1486 | NeedTypeSpace (D, 1); |
1487 | D->Type[D->Index].C = T_FUNC | Qualifiers; |
1488 | D->Type[D->Index].A.P = F; |
1489 | ++D->Index; |
1490 | |
1491 | /* Qualifiers now used */ |
1492 | Qualifiers = T_QUAL_NONE; |
1493 | |
1494 | } else { |
1495 | /* Array declaration. */ |
1496 | long Size = UNSPECIFIED; |
1497 | |
1498 | /* We cannot have any qualifiers for an array */ |
1499 | if (Qualifiers != T_QUAL_NONE) { |
1500 | Error ("Invalid qualifiers for array" ); |
1501 | Qualifiers = T_QUAL_NONE; |
1502 | } |
1503 | |
1504 | /* Skip the left bracket */ |
1505 | NextToken (); |
1506 | |
1507 | /* Read the size if it is given */ |
1508 | if (CurTok.Tok != TOK_RBRACK) { |
1509 | ExprDesc Expr; |
1510 | ConstAbsIntExpr (hie1, &Expr); |
1511 | if (Expr.IVal <= 0) { |
1512 | if (D->Ident[0] != '\0') { |
1513 | Error ("Size of array '%s' is invalid" , D->Ident); |
1514 | } else { |
1515 | Error ("Size of array is invalid" ); |
1516 | } |
1517 | Expr.IVal = 1; |
1518 | } |
1519 | Size = Expr.IVal; |
1520 | } |
1521 | |
1522 | /* Skip the right bracket */ |
1523 | ConsumeRBrack (); |
1524 | |
1525 | /* Add the array type with the size to the type */ |
1526 | NeedTypeSpace (D, 1); |
1527 | D->Type[D->Index].C = T_ARRAY; |
1528 | D->Type[D->Index].A.L = Size; |
1529 | ++D->Index; |
1530 | } |
1531 | } |
1532 | |
1533 | /* If we have remaining qualifiers, flag them as invalid */ |
1534 | if (Qualifiers & T_QUAL_NEAR) { |
1535 | Error ("Invalid '__near__' qualifier" ); |
1536 | } |
1537 | if (Qualifiers & T_QUAL_FAR) { |
1538 | Error ("Invalid '__far__' qualifier" ); |
1539 | } |
1540 | if (Qualifiers & T_QUAL_FASTCALL) { |
1541 | Error ("Invalid '__fastcall__' qualifier" ); |
1542 | } |
1543 | if (Qualifiers & T_QUAL_CDECL) { |
1544 | Error ("Invalid '__cdecl__' qualifier" ); |
1545 | } |
1546 | } |
1547 | |
1548 | |
1549 | |
1550 | /*****************************************************************************/ |
1551 | /* code */ |
1552 | /*****************************************************************************/ |
1553 | |
1554 | |
1555 | |
1556 | Type* ParseType (Type* T) |
1557 | /* Parse a complete type specification */ |
1558 | { |
1559 | DeclSpec Spec; |
1560 | Declaration Decl; |
1561 | |
1562 | /* Get a type without a default */ |
1563 | InitDeclSpec (&Spec); |
1564 | ParseTypeSpec (&Spec, -1, T_QUAL_NONE); |
1565 | |
1566 | /* Parse additional declarators */ |
1567 | ParseDecl (&Spec, &Decl, DM_NO_IDENT); |
1568 | |
1569 | /* Copy the type to the target buffer */ |
1570 | TypeCopy (T, Decl.Type); |
1571 | |
1572 | /* Return a pointer to the target buffer */ |
1573 | return T; |
1574 | } |
1575 | |
1576 | |
1577 | |
1578 | void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode) |
1579 | /* Parse a variable, type or function declaration */ |
1580 | { |
1581 | /* Initialize the Declaration struct */ |
1582 | InitDeclaration (D); |
1583 | |
1584 | /* Get additional declarators and the identifier */ |
1585 | Declarator (Spec, D, Mode); |
1586 | |
1587 | /* Add the base type. */ |
1588 | NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */ |
1589 | TypeCopy (D->Type + D->Index, Spec->Type); |
1590 | |
1591 | /* Use the storage class from the declspec */ |
1592 | D->StorageClass = Spec->StorageClass; |
1593 | |
1594 | /* Do several fixes on qualifiers */ |
1595 | FixQualifiers (D->Type); |
1596 | |
1597 | /* If we have a function, add a special storage class */ |
1598 | if (IsTypeFunc (D->Type)) { |
1599 | D->StorageClass |= SC_FUNC; |
1600 | } |
1601 | |
1602 | /* Parse attributes for this declaration */ |
1603 | ParseAttribute (D); |
1604 | |
1605 | /* Check several things for function or function pointer types */ |
1606 | if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) { |
1607 | |
1608 | /* A function. Check the return type */ |
1609 | Type* RetType = GetFuncReturn (D->Type); |
1610 | |
1611 | /* Functions may not return functions or arrays */ |
1612 | if (IsTypeFunc (RetType)) { |
1613 | Error ("Functions are not allowed to return functions" ); |
1614 | } else if (IsTypeArray (RetType)) { |
1615 | Error ("Functions are not allowed to return arrays" ); |
1616 | } |
1617 | |
1618 | /* The return type must not be qualified */ |
1619 | if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) { |
1620 | |
1621 | if (GetType (RetType) == T_TYPE_VOID) { |
1622 | /* A qualified void type is always an error */ |
1623 | Error ("function definition has qualified void return type" ); |
1624 | } else { |
1625 | /* For others, qualifiers are ignored */ |
1626 | Warning ("type qualifiers ignored on function return type" ); |
1627 | RetType[0].C = UnqualifiedType (RetType[0].C); |
1628 | } |
1629 | } |
1630 | |
1631 | /* Warn about an implicit int return in the function */ |
1632 | if ((Spec->Flags & DS_DEF_TYPE) != 0 && |
1633 | RetType[0].C == T_INT && RetType[1].C == T_END) { |
1634 | /* Function has an implicit int return. Output a warning if we don't |
1635 | ** have the C89 standard enabled explicitly. |
1636 | */ |
1637 | if (IS_Get (&Standard) >= STD_C99) { |
1638 | Warning ("Implicit 'int' return type is an obsolete feature" ); |
1639 | } |
1640 | GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET; |
1641 | } |
1642 | |
1643 | } |
1644 | |
1645 | /* For anthing that is not a function or typedef, check for an implicit |
1646 | ** int declaration. |
1647 | */ |
1648 | if ((D->StorageClass & SC_FUNC) != SC_FUNC && |
1649 | (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) { |
1650 | /* If the standard was not set explicitly to C89, print a warning |
1651 | ** for variables with implicit int type. |
1652 | */ |
1653 | if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) { |
1654 | Warning ("Implicit 'int' is an obsolete feature" ); |
1655 | } |
1656 | } |
1657 | |
1658 | /* Check the size of the generated type */ |
1659 | if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) { |
1660 | unsigned Size = SizeOf (D->Type); |
1661 | if (Size >= 0x10000) { |
1662 | if (D->Ident[0] != '\0') { |
1663 | Error ("Size of '%s' is invalid (0x%06X)" , D->Ident, Size); |
1664 | } else { |
1665 | Error ("Invalid size in declaration (0x%06X)" , Size); |
1666 | } |
1667 | } |
1668 | } |
1669 | |
1670 | } |
1671 | |
1672 | |
1673 | |
1674 | void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType) |
1675 | /* Parse a declaration specification */ |
1676 | { |
1677 | TypeCode Qualifiers; |
1678 | |
1679 | /* Initialize the DeclSpec struct */ |
1680 | InitDeclSpec (D); |
1681 | |
1682 | /* There may be qualifiers *before* the storage class specifier */ |
1683 | Qualifiers = OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE); |
1684 | |
1685 | /* Now get the storage class specifier for this declaration */ |
1686 | ParseStorageClass (D, DefStorage); |
1687 | |
1688 | /* Parse the type specifiers passing any initial type qualifiers */ |
1689 | ParseTypeSpec (D, DefType, Qualifiers); |
1690 | } |
1691 | |
1692 | |
1693 | |
1694 | void CheckEmptyDecl (const DeclSpec* D) |
1695 | /* Called after an empty type declaration (that is, a type declaration without |
1696 | ** a variable). Checks if the declaration does really make sense and issues a |
1697 | ** warning if not. |
1698 | */ |
1699 | { |
1700 | if ((D->Flags & DS_EXTRA_TYPE) == 0) { |
1701 | Warning ("Useless declaration" ); |
1702 | } |
1703 | } |
1704 | |
1705 | |
1706 | |
1707 | static void SkipInitializer (unsigned BracesExpected) |
1708 | /* Skip the remainder of an initializer in case of errors. Try to be somewhat |
1709 | ** smart so we don't have too many following errors. |
1710 | */ |
1711 | { |
1712 | while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) { |
1713 | switch (CurTok.Tok) { |
1714 | case TOK_RCURLY: --BracesExpected; break; |
1715 | case TOK_LCURLY: ++BracesExpected; break; |
1716 | default: break; |
1717 | } |
1718 | NextToken (); |
1719 | } |
1720 | } |
1721 | |
1722 | |
1723 | |
1724 | static unsigned OpeningCurlyBraces (unsigned BracesNeeded) |
1725 | /* Accept any number of opening curly braces around an initialization, skip |
1726 | ** them and return the number. If the number of curly braces is less than |
1727 | ** BracesNeeded, issue a warning. |
1728 | */ |
1729 | { |
1730 | unsigned BraceCount = 0; |
1731 | while (CurTok.Tok == TOK_LCURLY) { |
1732 | ++BraceCount; |
1733 | NextToken (); |
1734 | } |
1735 | if (BraceCount < BracesNeeded) { |
1736 | Error ("'{' expected" ); |
1737 | } |
1738 | return BraceCount; |
1739 | } |
1740 | |
1741 | |
1742 | |
1743 | static void ClosingCurlyBraces (unsigned BracesExpected) |
1744 | /* Accept and skip the given number of closing curly braces together with |
1745 | ** an optional comma. Output an error messages, if the input does not contain |
1746 | ** the expected number of braces. |
1747 | */ |
1748 | { |
1749 | while (BracesExpected) { |
1750 | if (CurTok.Tok == TOK_RCURLY) { |
1751 | NextToken (); |
1752 | } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) { |
1753 | NextToken (); |
1754 | NextToken (); |
1755 | } else { |
1756 | Error ("'}' expected" ); |
1757 | return; |
1758 | } |
1759 | --BracesExpected; |
1760 | } |
1761 | } |
1762 | |
1763 | |
1764 | |
1765 | static void DefineData (ExprDesc* Expr) |
1766 | /* Output a data definition for the given expression */ |
1767 | { |
1768 | switch (ED_GetLoc (Expr)) { |
1769 | |
1770 | case E_LOC_ABS: |
1771 | /* Absolute: numeric address or const */ |
1772 | g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0); |
1773 | break; |
1774 | |
1775 | case E_LOC_GLOBAL: |
1776 | /* Global variable */ |
1777 | g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal); |
1778 | break; |
1779 | |
1780 | case E_LOC_STATIC: |
1781 | case E_LOC_LITERAL: |
1782 | /* Static variable or literal in the literal pool */ |
1783 | g_defdata (CF_STATIC, Expr->Name, Expr->IVal); |
1784 | break; |
1785 | |
1786 | case E_LOC_REGISTER: |
1787 | /* Register variable. Taking the address is usually not |
1788 | ** allowed. |
1789 | */ |
1790 | if (IS_Get (&AllowRegVarAddr) == 0) { |
1791 | Error ("Cannot take the address of a register variable" ); |
1792 | } |
1793 | g_defdata (CF_REGVAR, Expr->Name, Expr->IVal); |
1794 | break; |
1795 | |
1796 | case E_LOC_STACK: |
1797 | case E_LOC_PRIMARY: |
1798 | case E_LOC_EXPR: |
1799 | Error ("Non constant initializer" ); |
1800 | break; |
1801 | |
1802 | default: |
1803 | Internal ("Unknown constant type: 0x%04X" , ED_GetLoc (Expr)); |
1804 | } |
1805 | } |
1806 | |
1807 | |
1808 | |
1809 | static void OutputBitFieldData (StructInitData* SI) |
1810 | /* Output bit field data */ |
1811 | { |
1812 | /* Ignore if we have no data */ |
1813 | if (SI->ValBits > 0) { |
1814 | |
1815 | /* Output the data */ |
1816 | g_defdata (CF_INT | CF_UNSIGNED | CF_CONST, SI->BitVal, 0); |
1817 | |
1818 | /* Clear the data from SI and account for the size */ |
1819 | SI->BitVal = 0; |
1820 | SI->ValBits = 0; |
1821 | SI->Offs += SIZEOF_INT; |
1822 | } |
1823 | } |
1824 | |
1825 | |
1826 | |
1827 | static void ParseScalarInitInternal (Type* T, ExprDesc* ED) |
1828 | /* Parse initializaton for scalar data types. This function will not output the |
1829 | ** data but return it in ED. |
1830 | */ |
1831 | { |
1832 | /* Optional opening brace */ |
1833 | unsigned BraceCount = OpeningCurlyBraces (0); |
1834 | |
1835 | /* We warn if an initializer for a scalar contains braces, because this is |
1836 | ** quite unusual and often a sign for some problem in the input. |
1837 | */ |
1838 | if (BraceCount > 0) { |
1839 | Warning ("Braces around scalar initializer" ); |
1840 | } |
1841 | |
1842 | /* Get the expression and convert it to the target type */ |
1843 | ConstExpr (hie1, ED); |
1844 | TypeConversion (ED, T); |
1845 | |
1846 | /* Close eventually opening braces */ |
1847 | ClosingCurlyBraces (BraceCount); |
1848 | } |
1849 | |
1850 | |
1851 | |
1852 | static unsigned ParseScalarInit (Type* T) |
1853 | /* Parse initializaton for scalar data types. Return the number of data bytes. */ |
1854 | { |
1855 | ExprDesc ED; |
1856 | |
1857 | /* Parse initialization */ |
1858 | ParseScalarInitInternal (T, &ED); |
1859 | |
1860 | /* Output the data */ |
1861 | DefineData (&ED); |
1862 | |
1863 | /* Done */ |
1864 | return SizeOf (T); |
1865 | } |
1866 | |
1867 | |
1868 | |
1869 | static unsigned ParsePointerInit (Type* T) |
1870 | /* Parse initializaton for pointer data types. Return the number of data bytes. */ |
1871 | { |
1872 | /* Optional opening brace */ |
1873 | unsigned BraceCount = OpeningCurlyBraces (0); |
1874 | |
1875 | /* Expression */ |
1876 | ExprDesc ED; |
1877 | ConstExpr (hie1, &ED); |
1878 | TypeConversion (&ED, T); |
1879 | |
1880 | /* Output the data */ |
1881 | DefineData (&ED); |
1882 | |
1883 | /* Close eventually opening braces */ |
1884 | ClosingCurlyBraces (BraceCount); |
1885 | |
1886 | /* Done */ |
1887 | return SIZEOF_PTR; |
1888 | } |
1889 | |
1890 | |
1891 | |
1892 | static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers) |
1893 | /* Parse initializaton for arrays. Return the number of data bytes. */ |
1894 | { |
1895 | int Count; |
1896 | |
1897 | /* Get the array data */ |
1898 | Type* ElementType = GetElementType (T); |
1899 | unsigned ElementSize = CheckedSizeOf (ElementType); |
1900 | long ElementCount = GetElementCount (T); |
1901 | |
1902 | /* Special handling for a character array initialized by a literal */ |
1903 | if (IsTypeChar (ElementType) && |
1904 | (CurTok.Tok == TOK_SCONST || CurTok.Tok == TOK_WCSCONST || |
1905 | (CurTok.Tok == TOK_LCURLY && |
1906 | (NextTok.Tok == TOK_SCONST || NextTok.Tok == TOK_WCSCONST)))) { |
1907 | |
1908 | /* Char array initialized by string constant */ |
1909 | int NeedParen; |
1910 | |
1911 | /* If we initializer is enclosed in brackets, remember this fact and |
1912 | ** skip the opening bracket. |
1913 | */ |
1914 | NeedParen = (CurTok.Tok == TOK_LCURLY); |
1915 | if (NeedParen) { |
1916 | NextToken (); |
1917 | } |
1918 | |
1919 | /* Translate into target charset */ |
1920 | TranslateLiteral (CurTok.SVal); |
1921 | |
1922 | /* If the array is one too small for the string literal, omit the |
1923 | ** trailing zero. |
1924 | */ |
1925 | Count = GetLiteralSize (CurTok.SVal); |
1926 | if (ElementCount != UNSPECIFIED && |
1927 | ElementCount != FLEXIBLE && |
1928 | Count == ElementCount + 1) { |
1929 | /* Omit the trailing zero */ |
1930 | --Count; |
1931 | } |
1932 | |
1933 | /* Output the data */ |
1934 | g_defbytes (GetLiteralStr (CurTok.SVal), Count); |
1935 | |
1936 | /* Skip the string */ |
1937 | NextToken (); |
1938 | |
1939 | /* If the initializer was enclosed in curly braces, we need a closing |
1940 | ** one. |
1941 | */ |
1942 | if (NeedParen) { |
1943 | ConsumeRCurly (); |
1944 | } |
1945 | |
1946 | } else { |
1947 | |
1948 | /* Curly brace */ |
1949 | ConsumeLCurly (); |
1950 | |
1951 | /* Initialize the array members */ |
1952 | Count = 0; |
1953 | while (CurTok.Tok != TOK_RCURLY) { |
1954 | /* Flexible array members may not be initialized within |
1955 | ** an array (because the size of each element may differ |
1956 | ** otherwise). |
1957 | */ |
1958 | ParseInitInternal (ElementType, 0); |
1959 | ++Count; |
1960 | if (CurTok.Tok != TOK_COMMA) |
1961 | break; |
1962 | NextToken (); |
1963 | } |
1964 | |
1965 | /* Closing curly braces */ |
1966 | ConsumeRCurly (); |
1967 | } |
1968 | |
1969 | if (ElementCount == UNSPECIFIED) { |
1970 | /* Number of elements determined by initializer */ |
1971 | SetElementCount (T, Count); |
1972 | ElementCount = Count; |
1973 | } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) { |
1974 | /* In non ANSI mode, allow initialization of flexible array |
1975 | ** members. |
1976 | */ |
1977 | ElementCount = Count; |
1978 | } else if (Count < ElementCount) { |
1979 | g_zerobytes ((ElementCount - Count) * ElementSize); |
1980 | } else if (Count > ElementCount) { |
1981 | Error ("Too many initializers" ); |
1982 | } |
1983 | return ElementCount * ElementSize; |
1984 | } |
1985 | |
1986 | |
1987 | |
1988 | static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers) |
1989 | /* Parse initialization of a struct or union. Return the number of data bytes. */ |
1990 | { |
1991 | SymEntry* Entry; |
1992 | SymTable* Tab; |
1993 | StructInitData SI; |
1994 | |
1995 | |
1996 | /* Consume the opening curly brace */ |
1997 | ConsumeLCurly (); |
1998 | |
1999 | /* Get a pointer to the struct entry from the type */ |
2000 | Entry = GetSymEntry (T); |
2001 | |
2002 | /* Get the size of the struct from the symbol table entry */ |
2003 | SI.Size = Entry->V.S.Size; |
2004 | |
2005 | /* Check if this struct definition has a field table. If it doesn't, it |
2006 | ** is an incomplete definition. |
2007 | */ |
2008 | Tab = Entry->V.S.SymTab; |
2009 | if (Tab == 0) { |
2010 | Error ("Cannot initialize variables with incomplete type" ); |
2011 | /* Try error recovery */ |
2012 | SkipInitializer (1); |
2013 | /* Nothing initialized */ |
2014 | return 0; |
2015 | } |
2016 | |
2017 | /* Get a pointer to the list of symbols */ |
2018 | Entry = Tab->SymHead; |
2019 | |
2020 | /* Initialize fields */ |
2021 | SI.Offs = 0; |
2022 | SI.BitVal = 0; |
2023 | SI.ValBits = 0; |
2024 | while (CurTok.Tok != TOK_RCURLY) { |
2025 | |
2026 | /* */ |
2027 | if (Entry == 0) { |
2028 | Error ("Too many initializers" ); |
2029 | SkipInitializer (1); |
2030 | return SI.Offs; |
2031 | } |
2032 | |
2033 | /* Parse initialization of one field. Bit-fields need a special |
2034 | ** handling. |
2035 | */ |
2036 | if (SymIsBitField (Entry)) { |
2037 | |
2038 | ExprDesc ED; |
2039 | unsigned Val; |
2040 | unsigned Shift; |
2041 | |
2042 | /* Calculate the bitmask from the bit-field data */ |
2043 | unsigned Mask = (1U << Entry->V.B.BitWidth) - 1U; |
2044 | |
2045 | /* Safety ... */ |
2046 | CHECK (Entry->V.B.Offs * CHAR_BITS + Entry->V.B.BitOffs == |
2047 | SI.Offs * CHAR_BITS + SI.ValBits); |
2048 | |
2049 | /* This may be an anonymous bit-field, in which case it doesn't |
2050 | ** have an initializer. |
2051 | */ |
2052 | if (IsAnonName (Entry->Name)) { |
2053 | /* Account for the data and output it if we have a full word */ |
2054 | SI.ValBits += Entry->V.B.BitWidth; |
2055 | CHECK (SI.ValBits <= INT_BITS); |
2056 | if (SI.ValBits == INT_BITS) { |
2057 | OutputBitFieldData (&SI); |
2058 | } |
2059 | goto NextMember; |
2060 | } else { |
2061 | /* Read the data, check for a constant integer, do a range |
2062 | ** check. |
2063 | */ |
2064 | ParseScalarInitInternal (type_uint, &ED); |
2065 | if (!ED_IsConstAbsInt (&ED)) { |
2066 | Error ("Constant initializer expected" ); |
2067 | ED_MakeConstAbsInt (&ED, 1); |
2068 | } |
2069 | if (ED.IVal > (long) Mask) { |
2070 | Warning ("Truncating value in bit-field initializer" ); |
2071 | ED.IVal &= (long) Mask; |
2072 | } |
2073 | Val = (unsigned) ED.IVal; |
2074 | } |
2075 | |
2076 | /* Add the value to the currently stored bit-field value */ |
2077 | Shift = (Entry->V.B.Offs - SI.Offs) * CHAR_BITS + Entry->V.B.BitOffs; |
2078 | SI.BitVal |= (Val << Shift); |
2079 | |
2080 | /* Account for the data and output it if we have a full word */ |
2081 | SI.ValBits += Entry->V.B.BitWidth; |
2082 | CHECK (SI.ValBits <= INT_BITS); |
2083 | if (SI.ValBits == INT_BITS) { |
2084 | OutputBitFieldData (&SI); |
2085 | } |
2086 | |
2087 | } else { |
2088 | |
2089 | /* Standard member. We should never have stuff from a |
2090 | ** bit-field left |
2091 | */ |
2092 | CHECK (SI.ValBits == 0); |
2093 | |
2094 | /* Flexible array members may only be initialized if they are |
2095 | ** the last field (or part of the last struct field). |
2096 | */ |
2097 | SI.Offs += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0); |
2098 | } |
2099 | |
2100 | /* More initializers? */ |
2101 | if (CurTok.Tok != TOK_COMMA) { |
2102 | break; |
2103 | } |
2104 | |
2105 | /* Skip the comma */ |
2106 | NextToken (); |
2107 | |
2108 | NextMember: |
2109 | /* Next member. For unions, only the first one can be initialized */ |
2110 | if (IsTypeUnion (T)) { |
2111 | /* Union */ |
2112 | Entry = 0; |
2113 | } else { |
2114 | /* Struct */ |
2115 | Entry = Entry->NextSym; |
2116 | } |
2117 | } |
2118 | |
2119 | /* Consume the closing curly brace */ |
2120 | ConsumeRCurly (); |
2121 | |
2122 | /* If we have data from a bit-field left, output it now */ |
2123 | OutputBitFieldData (&SI); |
2124 | |
2125 | /* If there are struct fields left, reserve additional storage */ |
2126 | if (SI.Offs < SI.Size) { |
2127 | g_zerobytes (SI.Size - SI.Offs); |
2128 | SI.Offs = SI.Size; |
2129 | } |
2130 | |
2131 | /* Return the actual number of bytes initialized. This number may be |
2132 | ** larger than sizeof (Struct) if flexible array members are present and |
2133 | ** were initialized (possible in non ANSI mode). |
2134 | */ |
2135 | return SI.Offs; |
2136 | } |
2137 | |
2138 | |
2139 | |
2140 | static unsigned ParseVoidInit (Type* T) |
2141 | /* Parse an initialization of a void variable (special cc65 extension). |
2142 | ** Return the number of bytes initialized. |
2143 | */ |
2144 | { |
2145 | ExprDesc Expr; |
2146 | unsigned Size; |
2147 | |
2148 | /* Opening brace */ |
2149 | ConsumeLCurly (); |
2150 | |
2151 | /* Allow an arbitrary list of values */ |
2152 | Size = 0; |
2153 | do { |
2154 | ConstExpr (hie1, &Expr); |
2155 | switch (UnqualifiedType (Expr.Type[0].C)) { |
2156 | |
2157 | case T_SCHAR: |
2158 | case T_UCHAR: |
2159 | if (ED_IsConstAbsInt (&Expr)) { |
2160 | /* Make it byte sized */ |
2161 | Expr.IVal &= 0xFF; |
2162 | } |
2163 | DefineData (&Expr); |
2164 | Size += SIZEOF_CHAR; |
2165 | break; |
2166 | |
2167 | case T_SHORT: |
2168 | case T_USHORT: |
2169 | case T_INT: |
2170 | case T_UINT: |
2171 | case T_PTR: |
2172 | case T_ARRAY: |
2173 | if (ED_IsConstAbsInt (&Expr)) { |
2174 | /* Make it word sized */ |
2175 | Expr.IVal &= 0xFFFF; |
2176 | } |
2177 | DefineData (&Expr); |
2178 | Size += SIZEOF_INT; |
2179 | break; |
2180 | |
2181 | case T_LONG: |
2182 | case T_ULONG: |
2183 | if (ED_IsConstAbsInt (&Expr)) { |
2184 | /* Make it dword sized */ |
2185 | Expr.IVal &= 0xFFFFFFFF; |
2186 | } |
2187 | DefineData (&Expr); |
2188 | Size += SIZEOF_LONG; |
2189 | break; |
2190 | |
2191 | default: |
2192 | Error ("Illegal type in initialization" ); |
2193 | break; |
2194 | |
2195 | } |
2196 | |
2197 | if (CurTok.Tok != TOK_COMMA) { |
2198 | break; |
2199 | } |
2200 | NextToken (); |
2201 | |
2202 | } while (CurTok.Tok != TOK_RCURLY); |
2203 | |
2204 | /* Closing brace */ |
2205 | ConsumeRCurly (); |
2206 | |
2207 | /* Number of bytes determined by initializer */ |
2208 | T->A.U = Size; |
2209 | |
2210 | /* Return the number of bytes initialized */ |
2211 | return Size; |
2212 | } |
2213 | |
2214 | |
2215 | |
2216 | static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers) |
2217 | /* Parse initialization of variables. Return the number of data bytes. */ |
2218 | { |
2219 | switch (UnqualifiedType (T->C)) { |
2220 | |
2221 | case T_SCHAR: |
2222 | case T_UCHAR: |
2223 | case T_SHORT: |
2224 | case T_USHORT: |
2225 | case T_INT: |
2226 | case T_UINT: |
2227 | case T_LONG: |
2228 | case T_ULONG: |
2229 | case T_FLOAT: |
2230 | case T_DOUBLE: |
2231 | return ParseScalarInit (T); |
2232 | |
2233 | case T_PTR: |
2234 | return ParsePointerInit (T); |
2235 | |
2236 | case T_ARRAY: |
2237 | return ParseArrayInit (T, AllowFlexibleMembers); |
2238 | |
2239 | case T_STRUCT: |
2240 | case T_UNION: |
2241 | return ParseStructInit (T, AllowFlexibleMembers); |
2242 | |
2243 | case T_VOID: |
2244 | if (IS_Get (&Standard) == STD_CC65) { |
2245 | /* Special cc65 extension in non-ANSI mode */ |
2246 | return ParseVoidInit (T); |
2247 | } |
2248 | /* FALLTHROUGH */ |
2249 | |
2250 | default: |
2251 | Error ("Illegal type" ); |
2252 | return SIZEOF_CHAR; |
2253 | |
2254 | } |
2255 | } |
2256 | |
2257 | |
2258 | |
2259 | unsigned ParseInit (Type* T) |
2260 | /* Parse initialization of variables. Return the number of data bytes. */ |
2261 | { |
2262 | /* Parse the initialization. Flexible array members can only be initialized |
2263 | ** in cc65 mode. |
2264 | */ |
2265 | unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65); |
2266 | |
2267 | /* The initialization may not generate code on global level, because code |
2268 | ** outside function scope will never get executed. |
2269 | */ |
2270 | if (HaveGlobalCode ()) { |
2271 | Error ("Non constant initializers" ); |
2272 | RemoveGlobalCode (); |
2273 | } |
2274 | |
2275 | /* Return the size needed for the initialization */ |
2276 | return Size; |
2277 | } |
2278 | |