gir2pascal: Created a dedicated section for declaring enumeration types, issue #FP39988.

This section is placed just after the constants section in order to
avoid declaration of function parameters and variables of enumeration
types that are not yet declared.

The issue was hidden in the typed constants implementation because the
constants section is placed at the beginning of the unit. This is not
true anymore and thus we need a dedicated section.
This commit is contained in:
Abou Al Montacir 2023-05-19 20:40:45 +02:00 committed by Maxim Ganetsky
parent c921b72be8
commit 08ee4a27b4

View File

@ -40,6 +40,12 @@ type
function AsString: String; override;
end;
{ TPDeclarationEnumTypes }
TPDeclarationEnumTypes = class(TPDeclarationWithLines)
function AsString: String; override;
end;
{ TPDeclarationVar }
TPDeclarationVar = class(TPDeclarationWithLines)
@ -111,6 +117,7 @@ type
TPInterface = class(TPCommonSections)
private
FConstSection: TPDeclarationConst;
FEnumTypesSection: TPDeclarationEnumTypes;
FFunctionSection: TPDeclarationFunctions;
FUsesSection: TPUses;
public
@ -119,6 +126,7 @@ type
function AsString: String; override;
property UsesSection: TPUses read FUsesSection;
property ConstSection: TPDeclarationConst read FConstSection;
property EnumTypesSection: TPDeclarationEnumTypes read FEnumTypesSection;
property FunctionSection: TPDeclarationFunctions read FFunctionSection;
end;
@ -198,6 +206,7 @@ type
// functions to ensure the type is being written in the correct declaration
function WantTypeSection: TPDeclarationType;
function WantConstSection: TPDeclarationConst;
function WantEnumTypesSection: TPDeclarationEnumTypes;
function WantFunctionSection: TPDeclarationFunctions;
// function WantVarSection: TPDeclarationVar;
@ -318,6 +327,17 @@ begin
Result := ANameSpace+Version;
end;
{ TPDeclarationEnumTypes }
function TPDeclarationEnumTypes.AsString: String;
begin
if Lines.Count > 0 then begin
Result := IndentText('type') + Lines.Text;
end else begin
Result := '';
end;
end;
{ TPascalUnitGroup }
function TPascalUnitGroup.GetUnitForType(AType: TPascalUnitType): TPascalUnit;
@ -550,6 +570,7 @@ begin
FUsesSection := AUses;
FUsesSection.Units.Add('CTypes');
FConstSection := TPDeclarationConst.Create;
FEnumTypesSection := TPDeclarationEnumTypes.Create;
FFunctionSection := TPDeclarationFunctions.Create(ADynamicFunctions);
end;
@ -567,6 +588,7 @@ begin
Result := IndentText('interface')+
FUsesSection.AsString+
FConstSection.AsString+
FEnumTypesSection.AsString +
FDeclarations.AsString+
FFunctionSection.AsString;
end;
@ -872,6 +894,11 @@ begin
Result := InterfaceSection.ConstSection;
end;
function TPascalUnit.WantEnumTypesSection: TPDeclarationEnumTypes;
begin
Result := InterfaceSection.EnumTypesSection;
end;
function TPascalUnit.WantFunctionSection: TPDeclarationFunctions;
begin
Result := InterfaceSection.FunctionSection;
@ -1014,7 +1041,7 @@ begin
// forces forward declarations to be written
ProcessType(AItem);
TypeName := AItem.TranslatedName;
Section := WantTypeSection;
Section := WantEnumTypesSection;
Section.Lines.Add(IndentText(TypeName + ' = (', 2, 0));
Section.Lines.Add(IndentText(TypeName + 'MinValue = -$7FFFFFFF,', 4, 0));
AItem.Members.Sort(@CompareEnumValues)