]> git.sur5r.net Git - cc65/blob - src/cc65/declare.c
Renamed a C header.
[cc65] / src / cc65 / declare.c
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 */
1334         F->Flags |= (FD_EMPTY | FD_VARIADIC);
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
1354         /* New style function */
1355         ParseAnsiParamList (F);
1356
1357     } else {
1358         /* Old style function */
1359         ParseOldStyleParamList (F);
1360     }
1361
1362     /* Remember the last function parameter. We need it later for several
1363     ** purposes, for example when passing stuff to fastcall functions. Since
1364     ** more symbols are added to the table, it is easier if we remember it
1365     ** now, since it is currently the last entry in the symbol table.
1366     */
1367     F->LastParam = GetSymTab()->SymTail;
1368
1369     /* Assign offsets. If the function has a variable parameter list,
1370     ** there's one additional byte (the arg size).
1371     */
1372     Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
1373     Sym = F->LastParam;
1374     while (Sym) {
1375         unsigned Size = CheckedSizeOf (Sym->Type);
1376         if (SymIsRegVar (Sym)) {
1377             Sym->V.R.SaveOffs = Offs;
1378         } else {
1379             Sym->V.Offs = Offs;
1380         }
1381         Offs += Size;
1382         F->ParamSize += Size;
1383         Sym = Sym->PrevSym;
1384     }
1385
1386     /* Leave the lexical level remembering the symbol tables */
1387     RememberFunctionLevel (F);
1388
1389     /* Did we have a WrappedCall for this function? */
1390     GetWrappedCall((void **) &WrappedCall, &WrappedCallData);
1391     if (WrappedCall) {
1392         F->WrappedCall = WrappedCall;
1393         F->WrappedCallData = WrappedCallData;
1394     }
1395
1396     /* Return the function descriptor */
1397     return F;
1398 }
1399
1400
1401
1402 static void Declarator (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1403 /* Recursively process declarators. Build a type array in reverse order. */
1404 {
1405     /* Read optional function or pointer qualifiers. They modify the
1406     ** identifier or token to the right. For convenience, we allow a calling
1407     ** convention also for pointers here. If it's a pointer-to-function, the
1408     ** qualifier later will be transfered to the function itself. If it's a
1409     ** pointer to something else, it will be flagged as an error.
1410     */
1411     TypeCode Qualifiers = OptionalQualifiers (T_QUAL_ADDRSIZE | T_QUAL_CCONV);
1412
1413     /* Pointer to something */
1414     if (CurTok.Tok == TOK_STAR) {
1415
1416         /* Skip the star */
1417         NextToken ();
1418
1419         /* Allow const, restrict, and volatile qualifiers */
1420         Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE | T_QUAL_RESTRICT);
1421
1422         /* Parse the type that the pointer points to */
1423         Declarator (Spec, D, Mode);
1424
1425         /* Add the type */
1426         AddTypeToDeclaration (D, T_PTR | Qualifiers);
1427         return;
1428     }
1429
1430     if (CurTok.Tok == TOK_LPAREN) {
1431         NextToken ();
1432         Declarator (Spec, D, Mode);
1433         ConsumeRParen ();
1434     } else {
1435         /* Things depend on Mode now:
1436         **  - Mode == DM_NEED_IDENT means:
1437         **      we *must* have a type and a variable identifer.
1438         **  - Mode == DM_NO_IDENT means:
1439         **      we must have a type but no variable identifer
1440         **      (if there is one, it's not read).
1441         **  - Mode == DM_ACCEPT_IDENT means:
1442         **      we *may* have an identifier. If there is an identifier,
1443         **      it is read, but it is no error, if there is none.
1444         */
1445         if (Mode == DM_NO_IDENT) {
1446             D->Ident[0] = '\0';
1447         } else if (CurTok.Tok == TOK_IDENT) {
1448             strcpy (D->Ident, CurTok.Ident);
1449             NextToken ();
1450         } else {
1451             if (Mode == DM_NEED_IDENT) {
1452                 Error ("Identifier expected");
1453             }
1454             D->Ident[0] = '\0';
1455         }
1456     }
1457
1458     while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
1459         if (CurTok.Tok == TOK_LPAREN) {
1460
1461             /* Function declaration */
1462             FuncDesc* F;
1463             SymEntry* PrevEntry;
1464
1465             /* Skip the opening paren */
1466             NextToken ();
1467
1468             /* Parse the function declaration */
1469             F = ParseFuncDecl ();
1470
1471             /* We cannot specify fastcall for variadic functions */
1472             if ((F->Flags & FD_VARIADIC) && (Qualifiers & T_QUAL_FASTCALL)) {
1473                 Error ("Variadic functions cannot be __fastcall__");
1474                 Qualifiers &= ~T_QUAL_FASTCALL;
1475             }
1476
1477             /* Was there a previous entry? If so, copy WrappedCall info from it */
1478             PrevEntry = FindGlobalSym (D->Ident);
1479             if (PrevEntry && PrevEntry->Flags & SC_FUNC) {
1480                 FuncDesc* D = PrevEntry->V.F.Func;
1481                 if (D->WrappedCall && !F->WrappedCall) {
1482                     F->WrappedCall = D->WrappedCall;
1483                     F->WrappedCallData = D->WrappedCallData;
1484                 }
1485             }
1486
1487             /* Add the function type. Be sure to bounds check the type buffer */
1488             NeedTypeSpace (D, 1);
1489             D->Type[D->Index].C = T_FUNC | Qualifiers;
1490             D->Type[D->Index].A.P = F;
1491             ++D->Index;
1492
1493             /* Qualifiers now used */
1494             Qualifiers = T_QUAL_NONE;
1495
1496         } else {
1497             /* Array declaration. */
1498             long Size = UNSPECIFIED;
1499
1500             /* We cannot have any qualifiers for an array */
1501             if (Qualifiers != T_QUAL_NONE) {
1502                 Error ("Invalid qualifiers for array");
1503                 Qualifiers = T_QUAL_NONE;
1504             }
1505
1506             /* Skip the left bracket */
1507             NextToken ();
1508
1509             /* Read the size if it is given */
1510             if (CurTok.Tok != TOK_RBRACK) {
1511                 ExprDesc Expr;
1512                 ConstAbsIntExpr (hie1, &Expr);
1513                 if (Expr.IVal <= 0) {
1514                     if (D->Ident[0] != '\0') {
1515                         Error ("Size of array '%s' is invalid", D->Ident);
1516                     } else {
1517                         Error ("Size of array is invalid");
1518                     }
1519                     Expr.IVal = 1;
1520                 }
1521                 Size = Expr.IVal;
1522             }
1523
1524             /* Skip the right bracket */
1525             ConsumeRBrack ();
1526
1527             /* Add the array type with the size to the type */
1528             NeedTypeSpace (D, 1);
1529             D->Type[D->Index].C = T_ARRAY;
1530             D->Type[D->Index].A.L = Size;
1531             ++D->Index;
1532         }
1533     }
1534
1535     /* If we have remaining qualifiers, flag them as invalid */
1536     if (Qualifiers & T_QUAL_NEAR) {
1537         Error ("Invalid '__near__' qualifier");
1538     }
1539     if (Qualifiers & T_QUAL_FAR) {
1540         Error ("Invalid '__far__' qualifier");
1541     }
1542     if (Qualifiers & T_QUAL_FASTCALL) {
1543         Error ("Invalid '__fastcall__' qualifier");
1544     }
1545     if (Qualifiers & T_QUAL_CDECL) {
1546         Error ("Invalid '__cdecl__' qualifier");
1547     }
1548 }
1549
1550
1551
1552 /*****************************************************************************/
1553 /*                                   code                                    */
1554 /*****************************************************************************/
1555
1556
1557
1558 Type* ParseType (Type* T)
1559 /* Parse a complete type specification */
1560 {
1561     DeclSpec Spec;
1562     Declaration Decl;
1563
1564     /* Get a type without a default */
1565     InitDeclSpec (&Spec);
1566     ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
1567
1568     /* Parse additional declarators */
1569     ParseDecl (&Spec, &Decl, DM_NO_IDENT);
1570
1571     /* Copy the type to the target buffer */
1572     TypeCopy (T, Decl.Type);
1573
1574     /* Return a pointer to the target buffer */
1575     return T;
1576 }
1577
1578
1579
1580 void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1581 /* Parse a variable, type or function declaration */
1582 {
1583     /* Initialize the Declaration struct */
1584     InitDeclaration (D);
1585
1586     /* Get additional declarators and the identifier */
1587     Declarator (Spec, D, Mode);
1588
1589     /* Add the base type. */
1590     NeedTypeSpace (D, TypeLen (Spec->Type) + 1);        /* Bounds check */
1591     TypeCopy (D->Type + D->Index, Spec->Type);
1592
1593     /* Use the storage class from the declspec */
1594     D->StorageClass = Spec->StorageClass;
1595
1596     /* Do several fixes on qualifiers */
1597     FixQualifiers (D->Type);
1598
1599     /* If we have a function, add a special storage class */
1600     if (IsTypeFunc (D->Type)) {
1601         D->StorageClass |= SC_FUNC;
1602     }
1603
1604     /* Parse attributes for this declaration */
1605     ParseAttribute (D);
1606
1607     /* Check several things for function or function pointer types */
1608     if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
1609
1610         /* A function. Check the return type */
1611         Type* RetType = GetFuncReturn (D->Type);
1612
1613         /* Functions may not return functions or arrays */
1614         if (IsTypeFunc (RetType)) {
1615             Error ("Functions are not allowed to return functions");
1616         } else if (IsTypeArray (RetType)) {
1617             Error ("Functions are not allowed to return arrays");
1618         }
1619
1620         /* The return type must not be qualified */
1621         if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
1622
1623             if (GetType (RetType) == T_TYPE_VOID) {
1624                 /* A qualified void type is always an error */
1625                 Error ("function definition has qualified void return type");
1626             } else {
1627                 /* For others, qualifiers are ignored */
1628                 Warning ("type qualifiers ignored on function return type");
1629                 RetType[0].C = UnqualifiedType (RetType[0].C);
1630             }
1631         }
1632
1633         /* Warn about an implicit int return in the function */
1634         if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
1635             RetType[0].C == T_INT && RetType[1].C == T_END) {
1636             /* Function has an implicit int return. Output a warning if we don't
1637             ** have the C89 standard enabled explicitly.
1638             */
1639             if (IS_Get (&Standard) >= STD_C99) {
1640                 Warning ("Implicit 'int' return type is an obsolete feature");
1641             }
1642             GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
1643         }
1644
1645     }
1646
1647     /* For anthing that is not a function or typedef, check for an implicit
1648     ** int declaration.
1649     */
1650     if ((D->StorageClass & SC_FUNC) != SC_FUNC &&
1651         (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
1652         /* If the standard was not set explicitly to C89, print a warning
1653         ** for variables with implicit int type.
1654         */
1655         if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
1656             Warning ("Implicit 'int' is an obsolete feature");
1657         }
1658     }
1659
1660     /* Check the size of the generated type */
1661     if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
1662         unsigned Size = SizeOf (D->Type);
1663         if (Size >= 0x10000) {
1664             if (D->Ident[0] != '\0') {
1665                 Error ("Size of '%s' is invalid (0x%06X)", D->Ident, Size);
1666             } else {
1667                 Error ("Invalid size in declaration (0x%06X)", Size);
1668             }
1669         }
1670     }
1671
1672 }
1673
1674
1675
1676 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
1677 /* Parse a declaration specification */
1678 {
1679     TypeCode Qualifiers;
1680
1681     /* Initialize the DeclSpec struct */
1682     InitDeclSpec (D);
1683
1684     /* There may be qualifiers *before* the storage class specifier */
1685     Qualifiers = OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
1686
1687     /* Now get the storage class specifier for this declaration */
1688     ParseStorageClass (D, DefStorage);
1689
1690     /* Parse the type specifiers passing any initial type qualifiers */
1691     ParseTypeSpec (D, DefType, Qualifiers);
1692 }
1693
1694
1695
1696 void CheckEmptyDecl (const DeclSpec* D)
1697 /* Called after an empty type declaration (that is, a type declaration without
1698 ** a variable). Checks if the declaration does really make sense and issues a
1699 ** warning if not.
1700 */
1701 {
1702     if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1703         Warning ("Useless declaration");
1704     }
1705 }
1706
1707
1708
1709 static void SkipInitializer (unsigned BracesExpected)
1710 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1711 ** smart so we don't have too many following errors.
1712 */
1713 {
1714     while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1715         switch (CurTok.Tok) {
1716             case TOK_RCURLY:    --BracesExpected;   break;
1717             case TOK_LCURLY:    ++BracesExpected;   break;
1718             default:                                break;
1719         }
1720         NextToken ();
1721     }
1722 }
1723
1724
1725
1726 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1727 /* Accept any number of opening curly braces around an initialization, skip
1728 ** them and return the number. If the number of curly braces is less than
1729 ** BracesNeeded, issue a warning.
1730 */
1731 {
1732     unsigned BraceCount = 0;
1733     while (CurTok.Tok == TOK_LCURLY) {
1734         ++BraceCount;
1735         NextToken ();
1736     }
1737     if (BraceCount < BracesNeeded) {
1738         Error ("'{' expected");
1739     }
1740     return BraceCount;
1741 }
1742
1743
1744
1745 static void ClosingCurlyBraces (unsigned BracesExpected)
1746 /* Accept and skip the given number of closing curly braces together with
1747 ** an optional comma. Output an error messages, if the input does not contain
1748 ** the expected number of braces.
1749 */
1750 {
1751     while (BracesExpected) {
1752         if (CurTok.Tok == TOK_RCURLY) {
1753             NextToken ();
1754         } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1755             NextToken ();
1756             NextToken ();
1757         } else {
1758             Error ("'}' expected");
1759             return;
1760         }
1761         --BracesExpected;
1762     }
1763 }
1764
1765
1766
1767 static void DefineData (ExprDesc* Expr)
1768 /* Output a data definition for the given expression */
1769 {
1770     switch (ED_GetLoc (Expr)) {
1771
1772         case E_LOC_ABS:
1773             /* Absolute: numeric address or const */
1774             g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
1775             break;
1776
1777         case E_LOC_GLOBAL:
1778             /* Global variable */
1779             g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
1780             break;
1781
1782         case E_LOC_STATIC:
1783         case E_LOC_LITERAL:
1784             /* Static variable or literal in the literal pool */
1785             g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
1786             break;
1787
1788         case E_LOC_REGISTER:
1789             /* Register variable. Taking the address is usually not
1790             ** allowed.
1791             */
1792             if (IS_Get (&AllowRegVarAddr) == 0) {
1793                 Error ("Cannot take the address of a register variable");
1794             }
1795             g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
1796             break;
1797
1798         case E_LOC_STACK:
1799         case E_LOC_PRIMARY:
1800         case E_LOC_EXPR:
1801             Error ("Non constant initializer");
1802             break;
1803
1804         default:
1805             Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
1806     }
1807 }
1808
1809
1810
1811 static void OutputBitFieldData (StructInitData* SI)
1812 /* Output bit field data */
1813 {
1814     /* Ignore if we have no data */
1815     if (SI->ValBits > 0) {
1816
1817         /* Output the data */
1818         g_defdata (CF_INT | CF_UNSIGNED | CF_CONST, SI->BitVal, 0);
1819
1820         /* Clear the data from SI and account for the size */
1821         SI->BitVal  = 0;
1822         SI->ValBits = 0;
1823         SI->Offs   += SIZEOF_INT;
1824     }
1825 }
1826
1827
1828
1829 static void ParseScalarInitInternal (Type* T, ExprDesc* ED)
1830 /* Parse initializaton for scalar data types. This function will not output the
1831 ** data but return it in ED.
1832 */
1833 {
1834     /* Optional opening brace */
1835     unsigned BraceCount = OpeningCurlyBraces (0);
1836
1837     /* We warn if an initializer for a scalar contains braces, because this is
1838     ** quite unusual and often a sign for some problem in the input.
1839     */
1840     if (BraceCount > 0) {
1841         Warning ("Braces around scalar initializer");
1842     }
1843
1844     /* Get the expression and convert it to the target type */
1845     ConstExpr (hie1, ED);
1846     TypeConversion (ED, T);
1847
1848     /* Close eventually opening braces */
1849     ClosingCurlyBraces (BraceCount);
1850 }
1851
1852
1853
1854 static unsigned ParseScalarInit (Type* T)
1855 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1856 {
1857     ExprDesc ED;
1858
1859     /* Parse initialization */
1860     ParseScalarInitInternal (T, &ED);
1861
1862     /* Output the data */
1863     DefineData (&ED);
1864
1865     /* Done */
1866     return SizeOf (T);
1867 }
1868
1869
1870
1871 static unsigned ParsePointerInit (Type* T)
1872 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1873 {
1874     /* Optional opening brace */
1875     unsigned BraceCount = OpeningCurlyBraces (0);
1876
1877     /* Expression */
1878     ExprDesc ED;
1879     ConstExpr (hie1, &ED);
1880     TypeConversion (&ED, T);
1881
1882     /* Output the data */
1883     DefineData (&ED);
1884
1885     /* Close eventually opening braces */
1886     ClosingCurlyBraces (BraceCount);
1887
1888     /* Done */
1889     return SIZEOF_PTR;
1890 }
1891
1892
1893
1894 static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
1895 /* Parse initializaton for arrays. Return the number of data bytes. */
1896 {
1897     int Count;
1898
1899     /* Get the array data */
1900     Type* ElementType    = GetElementType (T);
1901     unsigned ElementSize = CheckedSizeOf (ElementType);
1902     long ElementCount    = GetElementCount (T);
1903
1904     /* Special handling for a character array initialized by a literal */
1905     if (IsTypeChar (ElementType) &&
1906         (CurTok.Tok == TOK_SCONST || CurTok.Tok == TOK_WCSCONST ||
1907         (CurTok.Tok == TOK_LCURLY &&
1908          (NextTok.Tok == TOK_SCONST || NextTok.Tok == TOK_WCSCONST)))) {
1909
1910         /* Char array initialized by string constant */
1911         int NeedParen;
1912
1913         /* If we initializer is enclosed in brackets, remember this fact and
1914         ** skip the opening bracket.
1915         */
1916         NeedParen = (CurTok.Tok == TOK_LCURLY);
1917         if (NeedParen) {
1918             NextToken ();
1919         }
1920
1921         /* Translate into target charset */
1922         TranslateLiteral (CurTok.SVal);
1923
1924         /* If the array is one too small for the string literal, omit the
1925         ** trailing zero.
1926         */
1927         Count = GetLiteralSize (CurTok.SVal);
1928         if (ElementCount != UNSPECIFIED &&
1929             ElementCount != FLEXIBLE    &&
1930             Count        == ElementCount + 1) {
1931             /* Omit the trailing zero */
1932             --Count;
1933         }
1934
1935         /* Output the data */
1936         g_defbytes (GetLiteralStr (CurTok.SVal), Count);
1937
1938         /* Skip the string */
1939         NextToken ();
1940
1941         /* If the initializer was enclosed in curly braces, we need a closing
1942         ** one.
1943         */
1944         if (NeedParen) {
1945             ConsumeRCurly ();
1946         }
1947
1948     } else {
1949
1950         /* Curly brace */
1951         ConsumeLCurly ();
1952
1953         /* Initialize the array members */
1954         Count = 0;
1955         while (CurTok.Tok != TOK_RCURLY) {
1956             /* Flexible array members may not be initialized within
1957             ** an array (because the size of each element may differ
1958             ** otherwise).
1959             */
1960             ParseInitInternal (ElementType, 0);
1961             ++Count;
1962             if (CurTok.Tok != TOK_COMMA)
1963                 break;
1964             NextToken ();
1965         }
1966
1967         /* Closing curly braces */
1968         ConsumeRCurly ();
1969     }
1970
1971     if (ElementCount == UNSPECIFIED) {
1972         /* Number of elements determined by initializer */
1973         SetElementCount (T, Count);
1974         ElementCount = Count;
1975     } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1976         /* In non ANSI mode, allow initialization of flexible array
1977         ** members.
1978         */
1979         ElementCount = Count;
1980     } else if (Count < ElementCount) {
1981         g_zerobytes ((ElementCount - Count) * ElementSize);
1982     } else if (Count > ElementCount) {
1983         Error ("Too many initializers");
1984     }
1985     return ElementCount * ElementSize;
1986 }
1987
1988
1989
1990 static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
1991 /* Parse initialization of a struct or union. Return the number of data bytes. */
1992 {
1993     SymEntry*       Entry;
1994     SymTable*       Tab;
1995     StructInitData  SI;
1996
1997
1998     /* Consume the opening curly brace */
1999     ConsumeLCurly ();
2000
2001     /* Get a pointer to the struct entry from the type */
2002     Entry = GetSymEntry (T);
2003
2004     /* Get the size of the struct from the symbol table entry */
2005     SI.Size = Entry->V.S.Size;
2006
2007     /* Check if this struct definition has a field table. If it doesn't, it
2008     ** is an incomplete definition.
2009     */
2010     Tab = Entry->V.S.SymTab;
2011     if (Tab == 0) {
2012         Error ("Cannot initialize variables with incomplete type");
2013         /* Try error recovery */
2014         SkipInitializer (1);
2015         /* Nothing initialized */
2016         return 0;
2017     }
2018
2019     /* Get a pointer to the list of symbols */
2020     Entry = Tab->SymHead;
2021
2022     /* Initialize fields */
2023     SI.Offs    = 0;
2024     SI.BitVal  = 0;
2025     SI.ValBits = 0;
2026     while (CurTok.Tok != TOK_RCURLY) {
2027
2028         /* */
2029         if (Entry == 0) {
2030             Error ("Too many initializers");
2031             SkipInitializer (1);
2032             return SI.Offs;
2033         }
2034
2035         /* Parse initialization of one field. Bit-fields need a special
2036         ** handling.
2037         */
2038         if (SymIsBitField (Entry)) {
2039
2040             ExprDesc ED;
2041             unsigned Val;
2042             unsigned Shift;
2043
2044             /* Calculate the bitmask from the bit-field data */
2045             unsigned Mask = (1U << Entry->V.B.BitWidth) - 1U;
2046
2047             /* Safety ... */
2048             CHECK (Entry->V.B.Offs * CHAR_BITS + Entry->V.B.BitOffs ==
2049                    SI.Offs         * CHAR_BITS + SI.ValBits);
2050
2051             /* This may be an anonymous bit-field, in which case it doesn't
2052             ** have an initializer.
2053             */
2054             if (IsAnonName (Entry->Name)) {
2055                 /* Account for the data and output it if we have a full word */
2056                 SI.ValBits += Entry->V.B.BitWidth;
2057                 CHECK (SI.ValBits <= INT_BITS);
2058                 if (SI.ValBits == INT_BITS) {
2059                     OutputBitFieldData (&SI);
2060                 }
2061                 goto NextMember;
2062             } else {
2063                 /* Read the data, check for a constant integer, do a range
2064                 ** check.
2065                 */
2066                 ParseScalarInitInternal (type_uint, &ED);
2067                 if (!ED_IsConstAbsInt (&ED)) {
2068                     Error ("Constant initializer expected");
2069                     ED_MakeConstAbsInt (&ED, 1);
2070                 }
2071                 if (ED.IVal > (long) Mask) {
2072                     Warning ("Truncating value in bit-field initializer");
2073                     ED.IVal &= (long) Mask;
2074                 }
2075                 Val = (unsigned) ED.IVal;
2076             }
2077
2078             /* Add the value to the currently stored bit-field value */
2079             Shift = (Entry->V.B.Offs - SI.Offs) * CHAR_BITS + Entry->V.B.BitOffs;
2080             SI.BitVal |= (Val << Shift);
2081
2082             /* Account for the data and output it if we have a full word */
2083             SI.ValBits += Entry->V.B.BitWidth;
2084             CHECK (SI.ValBits <= INT_BITS);
2085             if (SI.ValBits == INT_BITS) {
2086                 OutputBitFieldData (&SI);
2087             }
2088
2089         } else {
2090
2091             /* Standard member. We should never have stuff from a
2092             ** bit-field left
2093             */
2094             CHECK (SI.ValBits == 0);
2095
2096             /* Flexible array members may only be initialized if they are
2097             ** the last field (or part of the last struct field).
2098             */
2099             SI.Offs += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
2100         }
2101
2102         /* More initializers? */
2103         if (CurTok.Tok != TOK_COMMA) {
2104             break;
2105         }
2106
2107         /* Skip the comma */
2108         NextToken ();
2109
2110 NextMember:
2111         /* Next member. For unions, only the first one can be initialized */
2112         if (IsTypeUnion (T)) {
2113             /* Union */
2114             Entry = 0;
2115         } else {
2116             /* Struct */
2117             Entry = Entry->NextSym;
2118         }
2119     }
2120
2121     /* Consume the closing curly brace */
2122     ConsumeRCurly ();
2123
2124     /* If we have data from a bit-field left, output it now */
2125     OutputBitFieldData (&SI);
2126
2127     /* If there are struct fields left, reserve additional storage */
2128     if (SI.Offs < SI.Size) {
2129         g_zerobytes (SI.Size - SI.Offs);
2130         SI.Offs = SI.Size;
2131     }
2132
2133     /* Return the actual number of bytes initialized. This number may be
2134     ** larger than sizeof (Struct) if flexible array members are present and
2135     ** were initialized (possible in non ANSI mode).
2136     */
2137     return SI.Offs;
2138 }
2139
2140
2141
2142 static unsigned ParseVoidInit (Type* T)
2143 /* Parse an initialization of a void variable (special cc65 extension).
2144 ** Return the number of bytes initialized.
2145 */
2146 {
2147     ExprDesc Expr;
2148     unsigned Size;
2149
2150     /* Opening brace */
2151     ConsumeLCurly ();
2152
2153     /* Allow an arbitrary list of values */
2154     Size = 0;
2155     do {
2156         ConstExpr (hie1, &Expr);
2157         switch (UnqualifiedType (Expr.Type[0].C)) {
2158
2159             case T_SCHAR:
2160             case T_UCHAR:
2161                 if (ED_IsConstAbsInt (&Expr)) {
2162                     /* Make it byte sized */
2163                     Expr.IVal &= 0xFF;
2164                 }
2165                 DefineData (&Expr);
2166                 Size += SIZEOF_CHAR;
2167                 break;
2168
2169             case T_SHORT:
2170             case T_USHORT:
2171             case T_INT:
2172             case T_UINT:
2173             case T_PTR:
2174             case T_ARRAY:
2175                 if (ED_IsConstAbsInt (&Expr)) {
2176                     /* Make it word sized */
2177                     Expr.IVal &= 0xFFFF;
2178                 }
2179                 DefineData (&Expr);
2180                 Size += SIZEOF_INT;
2181                 break;
2182
2183             case T_LONG:
2184             case T_ULONG:
2185                 if (ED_IsConstAbsInt (&Expr)) {
2186                     /* Make it dword sized */
2187                     Expr.IVal &= 0xFFFFFFFF;
2188                 }
2189                 DefineData (&Expr);
2190                 Size += SIZEOF_LONG;
2191                 break;
2192
2193             default:
2194                 Error ("Illegal type in initialization");
2195                 break;
2196
2197         }
2198
2199         if (CurTok.Tok != TOK_COMMA) {
2200             break;
2201         }
2202         NextToken ();
2203
2204     } while (CurTok.Tok != TOK_RCURLY);
2205
2206     /* Closing brace */
2207     ConsumeRCurly ();
2208
2209     /* Number of bytes determined by initializer */
2210     T->A.U = Size;
2211
2212     /* Return the number of bytes initialized */
2213     return Size;
2214 }
2215
2216
2217
2218 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
2219 /* Parse initialization of variables. Return the number of data bytes. */
2220 {
2221     switch (UnqualifiedType (T->C)) {
2222
2223         case T_SCHAR:
2224         case T_UCHAR:
2225         case T_SHORT:
2226         case T_USHORT:
2227         case T_INT:
2228         case T_UINT:
2229         case T_LONG:
2230         case T_ULONG:
2231         case T_FLOAT:
2232         case T_DOUBLE:
2233             return ParseScalarInit (T);
2234
2235         case T_PTR:
2236             return ParsePointerInit (T);
2237
2238         case T_ARRAY:
2239             return ParseArrayInit (T, AllowFlexibleMembers);
2240
2241         case T_STRUCT:
2242         case T_UNION:
2243             return ParseStructInit (T, AllowFlexibleMembers);
2244
2245         case T_VOID:
2246             if (IS_Get (&Standard) == STD_CC65) {
2247                 /* Special cc65 extension in non-ANSI mode */
2248                 return ParseVoidInit (T);
2249             }
2250             /* FALLTHROUGH */
2251
2252         default:
2253             Error ("Illegal type");
2254             return SIZEOF_CHAR;
2255
2256     }
2257 }
2258
2259
2260
2261 unsigned ParseInit (Type* T)
2262 /* Parse initialization of variables. Return the number of data bytes. */
2263 {
2264     /* Parse the initialization. Flexible array members can only be initialized
2265     ** in cc65 mode.
2266     */
2267     unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
2268
2269     /* The initialization may not generate code on global level, because code
2270     ** outside function scope will never get executed.
2271     */
2272     if (HaveGlobalCode ()) {
2273         Error ("Non constant initializers");
2274         RemoveGlobalCode ();
2275     }
2276
2277     /* Return the size needed for the initialization */
2278     return Size;
2279 }