mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-13 12:09:21 +02:00
gir2pas: Enabled generating Pascal sets to C bitfield enums by CLO.
Based on command line option (CLO) `-e/--declare-enums-as` that selects the way how to define C enums and bit fields. Added support for 5th options in addition 4 already implemented. 1. `IntConst`: No type, just use integer non typed constants. This was already implemented, but not easily selected at CLI. 2. `TypedIntConst`: Use non strict type aliases and typed constants. This was already implemented, selected by default in the code. 3. `IntAliasConst`: Use strict type aliases and non typed constants. This was newly added. 4. `Enum`: Use Pascal enumeration type for both C enums and bit fields. This was newly added. 5. `Set`: Use Pascal enumeration type for both C enums and Pascal sets for bit fields. This is now implemented.
This commit is contained in:
parent
bb05d66329
commit
cd0756f17b
@ -1028,6 +1028,8 @@ var
|
|||||||
ProperUnit: TPascalUnit;
|
ProperUnit: TPascalUnit;
|
||||||
IntType: String;
|
IntType: String;
|
||||||
Value: String;
|
Value: String;
|
||||||
|
UIntValue: QWord;
|
||||||
|
MSB: Integer;
|
||||||
begin
|
begin
|
||||||
ProperUnit := FGroup.GetUnitForType(utTypes);
|
ProperUnit := FGroup.GetUnitForType(utTypes);
|
||||||
if ProperUnit <> Self then begin
|
if ProperUnit <> Self then begin
|
||||||
@ -1037,8 +1039,14 @@ begin
|
|||||||
ResolveTypeTranslation(AItem);
|
ResolveTypeTranslation(AItem);
|
||||||
|
|
||||||
ConstSection := WantConstSection;
|
ConstSection := WantConstSection;
|
||||||
if goEnumAsSet in FOptions then begin
|
if (goEnumAsSet in FOptions) and (AItem is TgirBitField) then begin
|
||||||
raise Exception.Create('Not yet supported!');
|
// forces forward declarations to be written
|
||||||
|
ProcessType(AItem);
|
||||||
|
TypeName := AItem.TranslatedName + 'Idx';
|
||||||
|
Section := WantEnumTypesSection;
|
||||||
|
Section.Lines.Add(IndentText(TypeName + ' = (', 2, 0));
|
||||||
|
Section.Lines.Add(IndentText(TypeName + 'MinValue = 0,', 4, 0));
|
||||||
|
AItem.Members.Sort(@CompareEnumValues)
|
||||||
end else if goEnumAsEnum in FOptions then begin
|
end else if goEnumAsEnum in FOptions then begin
|
||||||
// forces forward declarations to be written
|
// forces forward declarations to be written
|
||||||
ProcessType(AItem);
|
ProcessType(AItem);
|
||||||
@ -1091,7 +1099,13 @@ begin
|
|||||||
if CName = 'ATK_HYPERLINK_IS_INLINE' then
|
if CName = 'ATK_HYPERLINK_IS_INLINE' then
|
||||||
CName :='ATK_HYPERLINK_IS_INLINE_';
|
CName :='ATK_HYPERLINK_IS_INLINE_';
|
||||||
Value := AItem.Members.Member[i]^.Value;
|
Value := AItem.Members.Member[i]^.Value;
|
||||||
if goEnumAsSet in FOptions then begin
|
if (goEnumAsSet in FOptions) and (AItem is TgirBitField) then begin
|
||||||
|
UIntValue := UInt64(StrToInt(Value));
|
||||||
|
MSB := BsrQWord(UIntValue);
|
||||||
|
if UIntValue > 1 shl MSB then
|
||||||
|
Continue;
|
||||||
|
Value := IntToStr(MSB);
|
||||||
|
Entry := IndentText(CName + ' = ' + Value + ',', 4, 0);;
|
||||||
end else if goEnumAsEnum in FOptions then begin
|
end else if goEnumAsEnum in FOptions then begin
|
||||||
Entry := IndentText(CName + ' = ' + Value + ',', 4, 0);
|
Entry := IndentText(CName + ' = ' + Value + ',', 4, 0);
|
||||||
end else if goEnumAsIntAliasConst in FOptions then begin
|
end else if goEnumAsIntAliasConst in FOptions then begin
|
||||||
@ -1103,67 +1117,34 @@ begin
|
|||||||
end;
|
end;
|
||||||
Section.Lines.Add(Entry);
|
Section.Lines.Add(Entry);
|
||||||
end;
|
end;
|
||||||
|
if (goEnumAsSet in FOptions) and (AItem is TgirBitField) then begin
|
||||||
|
Value := '31';
|
||||||
|
end else begin
|
||||||
|
Value := '$7FFFFFFF';
|
||||||
|
end;
|
||||||
if goEnumAsEnum in FOptions then begin
|
if goEnumAsEnum in FOptions then begin
|
||||||
Section.Lines.Add(IndentText(TypeName + 'MaxValue = $7FFFFFFF', 4, 0));
|
Section.Lines.Add(IndentText(TypeName + 'MaxValue = ' + Value, 4, 0));
|
||||||
Section.Lines.Add(IndentText(');', 2, 0));
|
Section.Lines.Add(IndentText(');', 2, 0));
|
||||||
end;
|
end;
|
||||||
AItem.Writing:=msWritten;
|
AItem.Writing:=msWritten;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TPascalUnit.HandleBitfield(AItem: TgirBitField);
|
procedure TPascalUnit.HandleBitfield(AItem: TgirBitField);
|
||||||
{
|
|
||||||
const
|
|
||||||
TemplateLongWord =
|
|
||||||
'%s = packed object(TBitObject32)'+LineEnding+
|
|
||||||
'%s'+LineEnding+
|
|
||||||
'end';
|
|
||||||
var
|
var
|
||||||
Intf: TPDeclarationType;
|
Section: TPDeclarationWithLines;
|
||||||
CodeText: TPCodeText;
|
TypeName: String;
|
||||||
Code: TStringList;
|
|
||||||
PName: String;
|
|
||||||
Entry: String;
|
|
||||||
i: Integer;
|
|
||||||
VarType: String;
|
|
||||||
}
|
|
||||||
begin
|
begin
|
||||||
HandleEnum(AItem);
|
if goEnumAsSet in FOptions then begin
|
||||||
(*
|
Include(FOptions, goEnumAsEnum);
|
||||||
Intf := WantTypeSection;
|
|
||||||
CodeText := TPCodeText.Create;
|
|
||||||
ImplementationSection.Declarations.Add(CodeText);
|
|
||||||
Code := TStringList.Create;
|
|
||||||
|
|
||||||
PName:=MakePascalTypeFromCType(AItem.CType);
|
|
||||||
|
|
||||||
{case AItem.Bits of
|
|
||||||
//1..8: VarType:='Byte';
|
|
||||||
//9..16: VarType:='Word';
|
|
||||||
//0:;
|
|
||||||
//17..32: VarType:='LongWord';
|
|
||||||
//33..64: VarType:='QWord';
|
|
||||||
else
|
|
||||||
WriteLn('Bitfield <> 16bits');
|
|
||||||
Halt;
|
|
||||||
end;
|
end;
|
||||||
}
|
|
||||||
HandleEnum(AItem);
|
HandleEnum(AItem);
|
||||||
|
if goEnumAsSet in FOptions then begin
|
||||||
|
Section := WantEnumTypesSection;
|
||||||
|
TypeName := AItem.TranslatedName;
|
||||||
|
end;
|
||||||
|
Section.Lines.Add(IndentText(TypeName + ' = Set of ' + TypeName + 'Idx;', 2, 0));
|
||||||
|
Section.Lines.Add('');
|
||||||
|
|
||||||
VarType:='DWord';
|
|
||||||
|
|
||||||
Intf.Lines.Add(IndentText(PName+ ' = packed object(TBitObject32)',2,0));
|
|
||||||
Intf.Lines.Add(IndentText('public',2,0));
|
|
||||||
for i := 0 to AItem.Members.Count-1 do
|
|
||||||
begin
|
|
||||||
Entry := 'property '+ SanitizeName(AItem.Members.Member[i]^.Name) +': '+VarType+' index '+AItem.Members.Member[i]^.Value+' read GetBit write SetBit;';
|
|
||||||
Intf.Lines.Add(IndentText(Entry, 4,0));
|
|
||||||
end;
|
|
||||||
Intf.Lines.Add(IndentText('end;',2,0));
|
|
||||||
Intf.Lines.Add('');
|
|
||||||
|
|
||||||
CodeText.Content:=Code.Text;
|
|
||||||
Code.Free;
|
|
||||||
*)
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TPascalUnit.HandleRecord(AItem: TgirRecord);
|
procedure TPascalUnit.HandleRecord(AItem: TgirRecord);
|
||||||
|
Loading…
Reference in New Issue
Block a user