From f567d6042917c6b288e3ad610a57f09f0a8798eb Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Mon, 18 Oct 2021 21:48:18 -0500 Subject: [PATCH] Allow bit-fields in unions. All versions of standard C allow this, but ORCA/C previously did not. --- Parser.pas | 13 ++++++++----- Scanner.pas | 2 +- cc.notes | 6 ++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Parser.pas b/Parser.pas index 0377ed3..e2e151c 100644 --- a/Parser.pas +++ b/Parser.pas @@ -2795,10 +2795,12 @@ var fl := variable; end; {if} end; {if} + if kind = unionType then begin + disp := 0; + bitdisp := 0; + end; {if} if token.kind = colonch then {handle a bit field} begin - if kind = unionType then - Error(56); NextToken; Expression(arrayExpression,[commach,semicolonch]); if (expressionValue >= maxBitField) or (expressionValue < 0) then @@ -2823,6 +2825,9 @@ var else tPtr := typeSpec; bitdisp := bitdisp+long(expressionValue).lsw; + if kind = unionType then + if ((bitDisp+7) div 8) > maxDisp then + maxDisp := ((bitDisp+7) div 8); if (tPtr^.kind <> scalarType) or not (tPtr^.baseType in [cgByte,cgUByte,cgWord,cgUWord,cgLong,cgULong]) @@ -2836,9 +2841,7 @@ var if bitdisp <> 0 then begin disp := disp+((bitDisp+7) div 8); bitdisp := 0; - end {if} - else if kind = unionType then - disp := 0; + end; {if} variable^.disp := disp; variable^.bitdisp := bitdisp; variable^.bitsize := 0; diff --git a/Scanner.pas b/Scanner.pas index d59c02a..314ed9b 100644 --- a/Scanner.pas +++ b/Scanner.pas @@ -627,7 +627,7 @@ if list or (numErr <> 0) then begin 53: msg := @'the structure or union has already been defined'; 54: msg := @'bit fields must be less than 32 bits wide'; 55: msg := @'a value cannot be zero bits wide'; - 56: msg := @'bit fields in unions are not supported by ORCA/C'; + {56: msg := @'bit fields in unions are not supported by ORCA/C';} 57: msg := @'compiler error'; 58: msg := @'implementation restriction: too many local labels'; 59: msg := @'file name expected'; diff --git a/cc.notes b/cc.notes index befd5a2..bbd0df7 100644 --- a/cc.notes +++ b/cc.notes @@ -206,6 +206,10 @@ p. 277 The type "long double" now specifies a number in the SANE extended format. +p. 286 + +Unions may now contain bit fields. + p. 289 Unions can be initialized by a brace-enclosed expression giving the initializer value for the first element of the union, or by an expression of the appropriate union type. The non-standard construct of initializing the first element of a union with a non-brace-enclosed initializer is no longer supported. The supported alternative is simply to enclose the initializer in braces, e.g. @@ -1370,6 +1374,8 @@ int foo(int[42]); 167. If an array with automatic storage duration was initialized from a string literal containing an embedded null byte (e.g. char s[] = "a\0b"), characters after the null byte would not be properly initialized. +168. Unions may now contain bit-fields, as specified in the C standards. + -- Bugs from C 2.1.0 that have been fixed ----------------------------------- 1. In some situations, fread() reread the first 1K or so of the file.