2019-07-29 14:33:28 +02:00
|
|
|
unit DelphiUnit;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2019-07-29 16:01:27 +02:00
|
|
|
classes,
|
|
|
|
system.json,
|
|
|
|
System.SysUtils,
|
|
|
|
System.Rtti,
|
|
|
|
System.TypInfo,
|
|
|
|
System.Generics.Collections
|
|
|
|
;
|
2019-07-29 14:33:28 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TUnitTypeDefinition = class;
|
|
|
|
|
|
|
|
TUnitFieldDefinition = class
|
|
|
|
private
|
|
|
|
FFieldName: string;
|
|
|
|
FFieldType: string;
|
|
|
|
FAttributes: TStringList;
|
|
|
|
FDescription: string;
|
|
|
|
public
|
|
|
|
property FieldName: string read FFieldName write FFieldName;
|
|
|
|
property FieldType: string read FFieldType write FFieldType;
|
|
|
|
property Description: string read FDescription write FDescription;
|
|
|
|
procedure AddAttribute(const inAttribute: string);
|
|
|
|
function GenerateInterface: string;
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TUnitParameter = class
|
|
|
|
private
|
|
|
|
FFlags: TParamFlags;
|
|
|
|
FType: TUnitTypeDefinition;
|
|
|
|
FParamName: string;
|
|
|
|
public
|
|
|
|
property ParamName: string read FParamName write FParamName;
|
|
|
|
property Flags: TParamFlags read FFlags write FFlags;
|
|
|
|
property ParamType: TUnitTypeDefinition read FType write FType;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TUnitMethod = class
|
|
|
|
private
|
|
|
|
FAttributes: TStringList;
|
|
|
|
FMethodKind: TMethodKind;
|
|
|
|
FVisibility: TMemberVisibility;
|
|
|
|
FName: string;
|
|
|
|
FIsStatic: Boolean;
|
|
|
|
FIsClassMethod: Boolean;
|
|
|
|
FIsConstructor: Boolean;
|
|
|
|
FIsDestructor: Boolean;
|
|
|
|
FReturnType: TUnitTypeDefinition;
|
|
|
|
FParams: TObjectList<TUnitParameter>;
|
|
|
|
FVars: TObjectList<TUnitParameter>;
|
|
|
|
FContent: TStringList;
|
|
|
|
public
|
|
|
|
property Content: TStringList read FContent write FContent;
|
|
|
|
property MethodKind: TMethodKind read FMethodKind write FMethodKind;
|
|
|
|
property Visibility: TMemberVisibility read FVisibility write FVisibility;
|
|
|
|
property Name: string read FName write FName;
|
|
|
|
property IsConstructor: Boolean read FIsConstructor write FIsConstructor;
|
|
|
|
property IsDestructor: Boolean read FIsDestructor write FIsDestructor;
|
|
|
|
property IsClassMethod: Boolean read FIsClassMethod write FIsClassMethod;
|
|
|
|
// Static: No 'Self' parameter
|
|
|
|
property IsStatic: Boolean read FIsStatic write FIsStatic;
|
|
|
|
property ReturnType: TUnitTypeDefinition read FReturnType write FReturnType;
|
|
|
|
function GetParameters: TArray<TUnitParameter>;
|
|
|
|
procedure AddParameter(param: TUnitParameter);
|
|
|
|
procedure AddLocalVariable(inVar: TUnitParameter);
|
|
|
|
procedure AddAttribute(const inAttribute: string);
|
|
|
|
function GenerateInterface: string;
|
|
|
|
function GenerateImplementation(inOnType: TUnitTypeDefinition): string;
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TUnitTypeDefinition = class
|
|
|
|
private
|
|
|
|
FTypeName: string;
|
|
|
|
FTypeInheritedFrom: string;
|
|
|
|
FAttributes: TStringList;
|
|
|
|
public
|
|
|
|
Fields: TObjectList<TUnitFieldDefinition>;
|
|
|
|
FMethods: TObjectList<TUnitMethod>;
|
|
|
|
property TypeName: string read FTypeName write FTypeName;
|
|
|
|
property TypeInherited: string read FTypeInheritedFrom write FTypeInheritedFrom;
|
|
|
|
function GetMethods(): TArray<TUnitMethod>;
|
|
|
|
procedure AddAttribute(const inAttribute: string);
|
|
|
|
function GenerateInterface: string;
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TDelphiUnit = class
|
|
|
|
private
|
|
|
|
FInterfaceUses: TStringList;
|
|
|
|
FImplementationUses: TStringList;
|
|
|
|
FUnitName: string;
|
|
|
|
public
|
|
|
|
TypeDefinitions: TObjectList<TUnitTypeDefinition>;
|
|
|
|
function GenerateInterfaceSectionStart: string; virtual;
|
|
|
|
function GenerateInterfaceUses: string; virtual;
|
|
|
|
function GenerateImplementationSectionStart: string; virtual;
|
|
|
|
function GenerateImplementationUses: string; virtual;
|
|
|
|
public
|
|
|
|
property UnitFile: string read FUnitName write FUnitName;
|
|
|
|
procedure AddInterfaceUnit(const inFilename: string); virtual;
|
|
|
|
procedure AddImplementationUnit(const inFilename: string); virtual;
|
|
|
|
procedure AddType(inTypeInfo: TUnitTypeDefinition);
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TDelphiUnit }
|
|
|
|
|
|
|
|
procedure TDelphiUnit.AddImplementationUnit(const inFilename: string);
|
|
|
|
var
|
|
|
|
IntIndex : Integer;
|
|
|
|
begin
|
|
|
|
IntIndex := FInterfaceUses.IndexOf(inFilename);
|
|
|
|
if IntIndex < 0 then
|
|
|
|
FImplementationUses.Add(inFilename);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TDelphiUnit.AddInterfaceUnit(const inFilename: string);
|
|
|
|
var
|
|
|
|
ImpIndex : Integer;
|
|
|
|
begin
|
|
|
|
ImpIndex := FImplementationUses.IndexOf(inFilename);
|
|
|
|
if ImpIndex >= 0 then
|
|
|
|
FImplementationUses.Delete(ImpIndex);
|
|
|
|
|
|
|
|
FInterfaceUses.Add(inFilename);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TDelphiUnit.AddType(inTypeInfo: TUnitTypeDefinition);
|
|
|
|
begin
|
|
|
|
TypeDefinitions.Add(inTypeInfo);
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TDelphiUnit.Create;
|
|
|
|
begin
|
|
|
|
FInterfaceUses := TStringList.Create;
|
|
|
|
FInterfaceUses.Duplicates := dupIgnore;
|
|
|
|
FImplementationUses := TStringList.Create;
|
|
|
|
FImplementationUses.Duplicates := dupIgnore;
|
|
|
|
TypeDefinitions := TObjectList<TUnitTypeDefinition>.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TDelphiUnit.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FInterfaceUses);
|
|
|
|
FreeAndNil(FImplementationUses);
|
|
|
|
FreeAndNil(TypeDefinitions);
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TDelphiUnit.GenerateImplementationSectionStart: string;
|
|
|
|
var
|
2019-07-29 16:01:27 +02:00
|
|
|
LImplementationSection: TStringList;
|
2019-07-29 14:33:28 +02:00
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LImplementationSection := TStringList.Create;
|
2019-07-29 14:33:28 +02:00
|
|
|
try
|
2019-07-29 16:01:27 +02:00
|
|
|
LImplementationSection.Add('');
|
|
|
|
LImplementationSection.Add('implementation');
|
|
|
|
LImplementationSection.Add('');
|
|
|
|
Result := LImplementationSection.Text;
|
2019-07-29 14:33:28 +02:00
|
|
|
finally
|
2019-07-29 16:01:27 +02:00
|
|
|
FreeAndNil(LImplementationSection);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TDelphiUnit.GenerateImplementationUses: string;
|
|
|
|
var
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL: TStringList;
|
2019-07-29 14:33:28 +02:00
|
|
|
i: Integer;
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL := TStringList.Create;
|
2019-07-29 14:33:28 +02:00
|
|
|
try
|
|
|
|
if FImplementationUses.Count > 0 then
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add('uses');
|
2019-07-29 14:33:28 +02:00
|
|
|
for i := 0 to FImplementationUses.Count - 1 do
|
|
|
|
begin
|
|
|
|
if i = 0 then
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add(' ' + FImplementationUses[i])
|
2019-07-29 14:33:28 +02:00
|
|
|
else
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add(' , ' + FImplementationUses[i]);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add(' ;');
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add('');
|
|
|
|
Result := LUsesSL.Text;
|
2019-07-29 14:33:28 +02:00
|
|
|
finally
|
2019-07-29 16:01:27 +02:00
|
|
|
FreeAndNil(LUsesSL);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TDelphiUnit.GenerateInterfaceSectionStart: string;
|
|
|
|
var
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSection: TStringList;
|
2019-07-29 14:33:28 +02:00
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSection := TStringList.Create;
|
2019-07-29 14:33:28 +02:00
|
|
|
try
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSection.Add('unit ' + UnitFile + ';');
|
|
|
|
LInterfaceSection.Add('');
|
|
|
|
LInterfaceSection.Add('interface');
|
|
|
|
LInterfaceSection.Add('');
|
|
|
|
Result := LInterfaceSection.Text;
|
2019-07-29 14:33:28 +02:00
|
|
|
finally
|
2019-07-29 16:01:27 +02:00
|
|
|
FreeAndNil(LInterfaceSection);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TDelphiUnit.GenerateInterfaceUses: string;
|
|
|
|
var
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL: TStringList;
|
2019-07-29 14:33:28 +02:00
|
|
|
i: Integer;
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL := TStringList.Create;
|
2019-07-29 14:33:28 +02:00
|
|
|
try
|
|
|
|
if FInterfaceUses.Count > 0 then
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add('uses');
|
2019-07-29 14:33:28 +02:00
|
|
|
for i := 0 to FInterfaceUses.Count - 1 do
|
|
|
|
begin
|
|
|
|
if i = 0 then
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add(' ' + FInterfaceUses[i])
|
2019-07-29 14:33:28 +02:00
|
|
|
else
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add(' , ' + FInterfaceUses[i]);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add(' ;');
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
2019-07-29 16:01:27 +02:00
|
|
|
LUsesSL.Add('');
|
|
|
|
Result := LUsesSL.Text;
|
2019-07-29 14:33:28 +02:00
|
|
|
finally
|
2019-07-29 16:01:27 +02:00
|
|
|
FreeAndNil(LUsesSL);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TTypeDefinition }
|
|
|
|
|
|
|
|
procedure TUnitTypeDefinition.AddAttribute(const inAttribute: string);
|
|
|
|
begin
|
|
|
|
FAttributes.Add(inAttribute);
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TUnitTypeDefinition.Create;
|
|
|
|
begin
|
|
|
|
FAttributes := TStringList.Create;
|
|
|
|
Fields := TObjectList<TUnitFieldDefinition>.Create;
|
|
|
|
FMethods := TObjectList<TUnitMethod>.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TUnitTypeDefinition.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FAttributes);
|
|
|
|
FreeAndNil(Fields);
|
|
|
|
FreeAndNil(FMethods);
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TUnitTypeDefinition.GenerateInterface: string;
|
|
|
|
var
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL: TStringList;
|
2019-07-29 14:33:28 +02:00
|
|
|
i: Integer;
|
|
|
|
j: Integer;
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL := TStringList.Create;
|
2019-07-29 14:33:28 +02:00
|
|
|
try
|
|
|
|
for i := 0 to FAttributes.Count - 1 do
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL.Add(FAttributes[i]);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
if TypeInherited.Length > 0 then
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL.Add(' ' + TypeName + ' = class(' + TypeInherited + ')')
|
2019-07-29 14:33:28 +02:00
|
|
|
else
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL.Add(' ' + TypeName + ' = class');
|
2019-07-29 14:33:28 +02:00
|
|
|
|
|
|
|
for j := 0 to Fields.Count - 1 do
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL.Add(Fields[j].GenerateInterface);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
for j := 0 to FMethods.Count - 1 do
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL.Add(TrimRight(FMethods[j].GenerateInterface));
|
|
|
|
LInterfaceSL.Add('');
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LInterfaceSL.Add(' end;');
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
Result := LInterfaceSL.Text;
|
2019-07-29 14:33:28 +02:00
|
|
|
finally
|
2019-07-29 16:01:27 +02:00
|
|
|
FreeAndNil(LInterfaceSL);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TUnitTypeDefinition.GetMethods: TArray<TUnitMethod>;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
SetLength(Result, FMethods.Count);
|
|
|
|
for i := 0 to FMethods.Count - 1 do
|
|
|
|
begin
|
|
|
|
Result[i] := FMethods[i];
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TFieldDefinition }
|
|
|
|
|
|
|
|
procedure TUnitFieldDefinition.AddAttribute(const inAttribute: string);
|
|
|
|
begin
|
|
|
|
FAttributes.Add(inAttribute);
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TUnitFieldDefinition.Create;
|
|
|
|
begin
|
|
|
|
FAttributes := TStringList.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TUnitFieldDefinition.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FAttributes);
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TUnitFieldDefinition.GenerateInterface: string;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
SL: TStringList;
|
|
|
|
begin
|
|
|
|
SL := TStringList.Create;
|
|
|
|
try
|
|
|
|
for i := 0 to FAttributes.Count - 1 do
|
|
|
|
begin
|
|
|
|
SL.Add(' ' + FAttributes[i]);
|
|
|
|
end;
|
|
|
|
if Description.Length > 0 then
|
|
|
|
SL.Add(' [MVCDoc(' + QuotedStr(Description) + ')]');
|
|
|
|
SL.Add(' ' + FFieldName + ' : ' + FFieldType + ';');
|
|
|
|
|
|
|
|
Result := SL.Text;
|
|
|
|
finally
|
|
|
|
FreeAndNil(SL);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TUnitMethod }
|
|
|
|
|
|
|
|
procedure TUnitMethod.AddAttribute(const inAttribute: string);
|
|
|
|
begin
|
|
|
|
FAttributes.Add(inAttribute);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TUnitMethod.AddLocalVariable(inVar: TUnitParameter);
|
|
|
|
begin
|
|
|
|
FVars.Add(inVar);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TUnitMethod.AddParameter(param: TUnitParameter);
|
|
|
|
begin
|
|
|
|
FParams.Add(param);
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TUnitMethod.Create;
|
|
|
|
begin
|
|
|
|
FParams := TObjectList<TUnitParameter>.Create;
|
|
|
|
FAttributes := TStringList.Create;
|
|
|
|
FVars := TObjectList<TUnitParameter>.Create;
|
|
|
|
FContent := TStringList.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TUnitMethod.Destroy;
|
|
|
|
begin
|
|
|
|
FreeAndNil(FParams);
|
|
|
|
FreeAndNil(FAttributes);
|
|
|
|
FreeAndNil(FVars);
|
|
|
|
FreeAndNil(FContent);
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TUnitMethod.GenerateImplementation(inOnType: TUnitTypeDefinition): string;
|
|
|
|
var
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString: string;
|
|
|
|
LHasReturn: Boolean;
|
|
|
|
LParam: TUnitParameter;
|
|
|
|
LParamFlagString: string;
|
|
|
|
LParamString: string;
|
|
|
|
LClassNameProcIn: string;
|
2019-07-29 14:33:28 +02:00
|
|
|
i: Integer;
|
2019-07-29 16:01:27 +02:00
|
|
|
LFuncSL: TStringList;
|
2019-07-29 14:33:28 +02:00
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LHasReturn := False;
|
|
|
|
LClassNameProcIn := '';
|
2019-07-29 14:33:28 +02:00
|
|
|
case MethodKind of
|
|
|
|
mkProcedure:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'procedure';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkFunction:
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'function';
|
|
|
|
LHasReturn := True;
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
mkDestructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'destructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkConstructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'constructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkClassFunction:
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class function';
|
|
|
|
LHasReturn := True;
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
mkClassProcedure:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class procedure';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkClassConstructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class constructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkClassDestructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class destructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
else
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'unknown'; // invalid ... will cause a compile error
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
if Assigned(inOnType) then
|
2019-07-29 16:01:27 +02:00
|
|
|
LClassNameProcIn := inOnType.TypeName + '.';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LParamString := '(';
|
|
|
|
for LParam in GetParameters do
|
2019-07-29 14:33:28 +02:00
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LParamFlagString := '';
|
|
|
|
if pfConst in LParam.Flags then
|
|
|
|
LParamFlagString := 'const'
|
|
|
|
else if pfVar in LParam.Flags then
|
|
|
|
LParamFlagString := 'var';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
if LParamFlagString.Length > 0 then
|
|
|
|
LParamFlagString := LParamFlagString + ' ';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LParamString := LParamString + LParamFlagString + LParam.ParamName + ': ' + LParam.FType.FTypeName + ';';
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
if LParamString.EndsWith(';') then
|
|
|
|
LParamString := LParamString.Remove(LParamString.Length - 1);
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LParamString := LParamString + ')';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
if LParamString = '()' then
|
|
|
|
LParamString := '';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
if LHasReturn then
|
|
|
|
Result := LProcTypeString + ' ' + LClassNameProcIn + FName + LParamString + ': ' + ReturnType.FTypeName + ';'
|
2019-07-29 14:33:28 +02:00
|
|
|
else
|
2019-07-29 16:01:27 +02:00
|
|
|
Result := LProcTypeString + ' ' + LClassNameProcIn + FName + LParamString + ';';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LFuncSL := TStringList.Create;
|
2019-07-29 14:33:28 +02:00
|
|
|
try
|
2019-07-29 16:01:27 +02:00
|
|
|
LFuncSL.Text := Result;
|
2019-07-29 14:33:28 +02:00
|
|
|
if FVars.Count > 0 then
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LFuncSL.Add('var');
|
2019-07-29 14:33:28 +02:00
|
|
|
for i := 0 to FVars.Count - 1 do
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LFuncSL.Add(' ' + FVars[i].ParamName + ' : ' + FVars[i].ParamType.TypeName + ';');
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
end;
|
2019-07-29 16:01:27 +02:00
|
|
|
LFuncSL.Add('begin');
|
|
|
|
LFuncSL.Add(Content.Text);
|
|
|
|
LFuncSL.Add('end;');
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
Result := LFuncSL.Text;
|
2019-07-29 14:33:28 +02:00
|
|
|
finally
|
2019-07-29 16:01:27 +02:00
|
|
|
FreeAndNil(LFuncSL);
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TUnitMethod.GenerateInterface: string;
|
|
|
|
var
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString: string;
|
|
|
|
LHasReturn: Boolean;
|
|
|
|
LParam: TUnitParameter;
|
|
|
|
LParamFlagString: string;
|
|
|
|
LParamString: string;
|
|
|
|
LAttributeString: string;
|
2019-07-29 14:33:28 +02:00
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LHasReturn := False;
|
2019-07-29 14:33:28 +02:00
|
|
|
case MethodKind of
|
|
|
|
mkProcedure:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'procedure';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkFunction:
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'function';
|
|
|
|
LHasReturn := True;
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
mkDestructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'destructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkConstructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'constructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkClassFunction:
|
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class function';
|
|
|
|
LHasReturn := True;
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
mkClassProcedure:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class procedure';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkClassConstructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class constructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
mkClassDestructor:
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'class destructor';
|
2019-07-29 14:33:28 +02:00
|
|
|
else
|
2019-07-29 16:01:27 +02:00
|
|
|
LProcTypeString := 'unknown'; // invalid ... will cause a compile error
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LParamString := '(';
|
|
|
|
for LParam in GetParameters do
|
2019-07-29 14:33:28 +02:00
|
|
|
begin
|
2019-07-29 16:01:27 +02:00
|
|
|
LParamFlagString := '';
|
|
|
|
if pfConst in LParam.Flags then
|
|
|
|
LParamFlagString := 'const'
|
|
|
|
else if pfVar in LParam.Flags then
|
|
|
|
LParamFlagString := 'var';
|
|
|
|
|
|
|
|
if LParamFlagString.Length > 0 then
|
|
|
|
LParamFlagString := LParamFlagString + ' ';
|
|
|
|
LParamString := LParamString + LParamFlagString + LParam.ParamName + ': ' + LParam.FType.FTypeName + ';';
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
if LParamString.EndsWith(';') then
|
|
|
|
LParamString := LParamString.Remove(LParamString.Length - 1);
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LParamString := LParamString + ')';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
if LParamString = '()' then
|
|
|
|
LParamString := '';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
if LHasReturn then
|
|
|
|
Result := ' ' + LProcTypeString + ' ' + FName + LParamString + ': ' + ReturnType.FTypeName + ';'
|
2019-07-29 14:33:28 +02:00
|
|
|
else
|
2019-07-29 16:01:27 +02:00
|
|
|
Result := ' ' + LProcTypeString + ' ' + FName + LParamString + ';';
|
2019-07-29 14:33:28 +02:00
|
|
|
|
2019-07-29 16:01:27 +02:00
|
|
|
LAttributeString := FAttributes.Text;
|
|
|
|
Result := LAttributeString + Result;
|
2019-07-29 14:33:28 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TUnitMethod.GetParameters: TArray<TUnitParameter>;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
setLength(Result, FParams.Count);
|
|
|
|
for i := 0 to FParams.Count - 1 do
|
|
|
|
begin
|
|
|
|
Result[i] := FParams[i];
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|