fixed external functions and variables naming (underscores)

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@726 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
skalogryz
2009-02-28 10:11:19 +00:00
parent 079f53052f
commit 6f30ade18f
2 changed files with 66 additions and 46 deletions

View File

@ -48,6 +48,11 @@ type
end; end;
TConvertSettings = class(TObject) TConvertSettings = class(TObject)
private
fCallConv : AnsiString;
fExternPrefix : AnsiString;
protected
procedure SetCallConv(const ACallConv: String);
public public
IgnoreIncludes : TStringList; IgnoreIncludes : TStringList;
DefineReplace : TReplaceList; DefineReplace : TReplaceList;
@ -64,10 +69,17 @@ type
CustomTypes : TStringList; CustomTypes : TStringList;
ObjcIDReplace : AnsiString; // = 'objc.id';
fExternVarPrefix : AnsiString; // always '_'?
constructor Create; constructor Create;
destructor Destroy; override; destructor Destroy; override;
procedure AssignNewTypeName(const AName, TypeDefStr: AnsiString; var NewTypeName: AnsiString); procedure AssignNewTypeName(const AName, TypeDefStr: AnsiString; var NewTypeName: AnsiString);
function GetCallConv(withSemiColon: Boolean = true): AnsiString;
property CallConv: AnsiString read fCallConv write SetCallConv;
property ExternFuncPrefix: AnsiString read fExternPrefix; // external function name prefix
property ExternVarPrefix: AnsiString read fExternVarPrefix; // external function var prefix
end; end;
var var
@ -98,9 +110,6 @@ function GetObjCVarType(const TypeName: AnsiString):TObjcConvertVarType; //): Bo
implementation implementation
var
ObjcIDReplace : AnsiString = 'objc.id';
function GetterSetterName(const PropName: AnsiString; etterName: AnsiString; isSetter: Boolean): AnsiString; function GetterSetterName(const PropName: AnsiString; etterName: AnsiString; isSetter: Boolean): AnsiString;
begin begin
if etterName = '' then begin if etterName = '' then begin
@ -309,7 +318,7 @@ begin
end; end;
end; end;
'i': 'i':
if l = 'id' then Result := ObjCIDReplace if l = 'id' then Result := ConvertSettings.ObjCIDReplace
else if l = 'int' then Result := 'Integer'; else if l = 'int' then Result := 'Integer';
'b': 'b':
if l = 'bool' then Result := 'LongBool'; if l = 'bool' then Result := 'LongBool';
@ -836,9 +845,11 @@ procedure WriteOutVariableToHeader(v: TVariable; const SpacePrefix: String; Vars
var var
tp : TTypeDef; tp : TTypeDef;
s : AnsiString; s : AnsiString;
vartype : AnsiString;
begin begin
tp := TTypeDef(v._Type); tp := TTypeDef(v._Type);
s := Format('%s : %s; external name ''%s''; ', [v._Name, ObjCToDelphiType(tp._Name, tp._IsPointer), v._Name] ); vartype := ObjCToDelphiType(tp._Name, tp._IsPointer);
s := Format('%s : %s; external name ''%s%s''; ', [v._Name, vartype, ConvertSettings.ExternVarPrefix, v._Name] );
Vars.Add(SpacePrefix + s); Vars.Add(SpacePrefix + s);
end; end;
@ -886,9 +897,9 @@ begin
end; end;
restype := ObjCToDelphiType(fntype, isptr); restype := ObjCToDelphiType(fntype, isptr);
s:= GetProcFuncHead(f._Name, '', CParamsListToPascalStr(f._ParamsList), restype) + ' cdecl'; s:= GetProcFuncHead(f._Name, '', CParamsListToPascalStr(f._ParamsList), restype) + ' ' + ConvertSettings.GetCallConv(true);
st.Add( s); st.Add(s);
s := Format(' external name ''_%s'';', [f._Name]); s := Format(' external name ''%s%s'';', [ConvertSettings.ExternFuncPrefix, f._Name]);
st.Add(s); st.Add(s);
end; end;
@ -984,7 +995,7 @@ begin
fntype := '{todo: not implemented... see .h file for type}'; fntype := '{todo: not implemented... see .h file for type}';
end; end;
restype := ObjCToDelphiType(fntype, isptr); restype := ObjCToDelphiType(fntype, isptr);
Result := GetProcFuncHead('', '', CParamsListToPascalStr(AFuncType._ParamsList), restype) + ' cdecl'; Result := GetProcFuncHead('', '', CParamsListToPascalStr(AFuncType._ParamsList), restype) + ' '+ConvertSettings.GetCallConv(true);
//Result := Copy(Result, 1, length(Result) - 1); //Result := Copy(Result, 1, length(Result) - 1);
//Result := Result + '; cdecl'; //Result := Result + '; cdecl';
end; end;
@ -1258,7 +1269,7 @@ begin
subs.Add(s + '('+cl._SuperClass+')'); subs.Add(s + '('+cl._SuperClass+')');
protidx := subs.Count; protidx := subs.Count;
subs.Add(' public'); subs.Add(' public');
subs.Add(' class function getClass: '+ObjCIDReplace+'; override;'); subs.Add(' class function getClass: '+ConvertSettings.ObjCIDReplace+'; override;');
end else begin end else begin
subs.Add(s + '{from category '+ cl._Category +'}'); subs.Add(s + '{from category '+ cl._Category +'}');
protidx := subs.Count; protidx := subs.Count;
@ -1394,24 +1405,28 @@ var
s : AnsiString; s : AnsiString;
ms : AnsiString; ms : AnsiString;
restype : AnsiString; restype : AnsiString;
funchdr : AnsiString;
callcnv : AnsiString;
begin begin
//typeName := MtdPrefix + mtd._Name + MtdPostFix; //typeName := MtdPrefix + mtd._Name + MtdPostFix;
typeName := 'TmsgSendWrapper'; typeName := 'TmsgSendWrapper';
subs.Add('type'); subs.Add('type');
ms := GetMethodParams(mtd, false); ms := GetMethodParams(mtd, false);
if ms = '' then ms := 'param1: '+ObjCIDReplace+'; param2: SEL' if ms = '' then ms := 'param1: '+ConvertSettings.ObjCIDReplace+'; param2: SEL'
else ms := 'param1: '+ObjCIDReplace+'; param2: SEL' + ';' + ms; else ms := 'param1: '+ConvertSettings.ObjCIDReplace+'; param2: SEL' + ';' + ms;
if isResultStruct then begin if isResultStruct then begin
restype := ''; restype := '';
ms := 'result_param: Pointer; ' + ms; ms := 'result_param: Pointer; ' + ms;
end else begin end else begin
restype := GetMethodResultType(mtd); restype := GetMethodResultType(mtd);
if IsMethodConstructor(mtd.Owner as TClassDef, mtd) then restype := ObjCIDReplace; if IsMethodConstructor(mtd.Owner as TClassDef, mtd) then restype := ConvertSettings.ObjCIDReplace;
end; end;
s := Format(' %s = %s cdecl;',[typeName, GetProcFuncHead('', '', ms, restype, '' )]); funchdr := GetProcFuncHead('', '', ms, restype, '' );
callcnv := ConvertSettings.GetCallConv(false);
s := Format(' %s = %s%s;',[typeName, funchdr, callcnv]);
subs.Add(s); subs.Add(s);
end; end;
@ -1618,7 +1633,7 @@ begin
end; end;
subs.Add(''); subs.Add('');
subs.Add('class ' + GetProcFuncHead('getClass', cl._ClassName, '', ObjCIdReplace)); subs.Add('class ' + GetProcFuncHead('getClass', cl._ClassName, '', ConvertSettings.ObjCIdReplace));
subs.Add('begin'); subs.Add('begin');
subs.Add( subs.Add(
Format(' Result := objc_getClass(Str%s_%s);', [cl._ClassName, cl._ClassName])); Format(' Result := objc_getClass(Str%s_%s);', [cl._ClassName, cl._ClassName]));
@ -1807,14 +1822,14 @@ begin
if res._Type is TTypeDef then begin if res._Type is TTypeDef then begin
td := TTypeDef(res._Type); td := TTypeDef(res._Type);
res.tagComment := td._Name; res.tagComment := td._Name;
td._Name := ObjcIDReplace; //Format('objc.id', [td._Name] ); td._Name := ConvertSettings.ObjcIDReplace; //Format('objc.id', [td._Name] );
end; end;
end else if (obj is TObjCParameterDef) then begin end else if (obj is TObjCParameterDef) then begin
prm := TObjCParameterDef(obj); prm := TObjCParameterDef(obj);
if ConvertSettings.ObjCClassTypes.IndexOf( ObjCResultToDelphiType(prm._Type) ) >= 0 then begin if ConvertSettings.ObjCClassTypes.IndexOf( ObjCResultToDelphiType(prm._Type) ) >= 0 then begin
if prm._Type._Type is TTypeDef then begin if prm._Type._Type is TTypeDef then begin
TTypeDef(prm._Type._Type)._Name := ObjCIDReplace; //Format('objc.id {%s}', [TTypeDef(prm._Type._Type)._Name] ); TTypeDef(prm._Type._Type)._Name := ConvertSettings.ObjCIDReplace; //Format('objc.id {%s}', [TTypeDef(prm._Type._Type)._Name] );
end; end;
end; end;
@ -1824,7 +1839,7 @@ begin
end else if (obj is TStructField) then begin end else if (obj is TStructField) then begin
// should _TypeName to be removed? // should _TypeName to be removed?
if ConvertSettings.ObjCClassTypes.IndexOf(TStructField(obj)._TypeName) >= 0 then begin if ConvertSettings.ObjCClassTypes.IndexOf(TStructField(obj)._TypeName) >= 0 then begin
TStructField(obj)._TypeName := ObjCIDReplace TStructField(obj)._TypeName := ConvertSettings.ObjCIDReplace
end; end;
end else if (obj is TClassesForward) then begin end else if (obj is TClassesForward) then begin
for j := 0 to TClassesForward(obj)._Classes.Count - 1 do for j := 0 to TClassesForward(obj)._Classes.Count - 1 do
@ -1959,17 +1974,21 @@ begin
TypeDefReplace := TReplaceList.Create; // replaces for default types TypeDefReplace := TReplaceList.Create; // replaces for default types
PtrTypeReplace := TReplaceList.Create; // replaces for C types pointers PtrTypeReplace := TReplaceList.Create; // replaces for C types pointers
ConvertPrefix := TStringList.Create; ConvertPrefix := TStringList.Create;
FloatTypes := TStringList.Create; FloatTypes := TStringList.Create;
FloatTypes.CaseSensitive := false; FloatTypes.CaseSensitive := false;
StructTypes := TStringList.Create; StructTypes := TStringList.Create;
StructTypes.CaseSensitive := false; StructTypes.CaseSensitive := false;
ObjCClassTypes := TStringList.Create; ObjCClassTypes := TStringList.Create;
ObjCClassTypes.CaseSensitive := false; ObjCClassTypes.CaseSensitive := false;
CustomTypes := TStringList.Create; CustomTypes := TStringList.Create;
ObjcIDReplace := 'objc.id';
CallConv := 'cdecl';
fExternVarPrefix := '_';
end; end;
destructor TConvertSettings.Destroy; destructor TConvertSettings.Destroy;
@ -1992,20 +2011,9 @@ procedure InitConvertSettings;
begin begin
with ConvertSettings.IgnoreIncludes do begin with ConvertSettings.IgnoreIncludes do begin
// must not be $included, because they are used // must not be $included, because they are used
// Add('Foundation/');
// Add('Foundation/NSObject.h');
// Add('NSObjCRuntime.h');
// Add('Foundation/NSObject.h');
// Add('Foundation/Foundation.h');
end; end;
with ConvertSettings do begin with ConvertSettings do begin
{DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2'] := 'MAC_OS_X_VERSION_10_2';
DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3'] := 'MAC_OS_X_VERSION_10_3';
DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4'] := 'MAC_OS_X_VERSION_10_4';
DefineReplace['MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5'] := 'MAC_OS_X_VERSION_10_5';
DefineReplace['__LP64__'] := 'LP64';}
TypeDefReplace['unsigned char'] := 'byte'; TypeDefReplace['unsigned char'] := 'byte';
TypeDefReplace['uint8_t'] := 'byte'; TypeDefReplace['uint8_t'] := 'byte';
PtrTypeReplace['uint8_t'] := 'PByte'; PtrTypeReplace['uint8_t'] := 'PByte';
@ -2046,7 +2054,7 @@ begin
TypeDefReplace['unsigned long long'] := 'Int64'; TypeDefReplace['unsigned long long'] := 'Int64';
TypeDefReplace['int64_t'] := 'Int64'; TypeDefReplace['int64_t'] := 'Int64';
TypeDefReplace['uint64_t'] := 'Int64'; TypeDefReplace['uint64_t'] := 'Int64';
PtrTypeReplace['long long'] := 'PInt64'; PtrTypeReplace['long long'] := 'PInt64';
PtrTypeReplace['singned long long'] := 'PInt64'; PtrTypeReplace['singned long long'] := 'PInt64';
PtrTypeReplace['unsigned long long'] := 'PInt64'; PtrTypeReplace['unsigned long long'] := 'PInt64';
@ -2076,19 +2084,25 @@ begin
PtrTypeReplace['unsigned']:='PLongWord'; PtrTypeReplace['unsigned']:='PLongWord';
StructTypes.Add('Int64'); StructTypes.Add('Int64');
{ StructTypes.Add('NSAffineTransformStruct');
FloatTypes.Add('NSTimeInterval');
FloatTypes.Add('CFFloat');}
{IgnoreTokens.Add('DEPRECATED_IN_MAC_OS_X_VERSION_10_5_AND_LATER');
IgnoreTokens.Add('DEPRECATED_IN_MAC_OS_X_VERSION_10_4_AND_LATER');
IgnoreTokens.Add('AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER');
IgnoreTokens.Add('AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER');
IgnoreTokens.Add('AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER');
IgnoreTokens.Add('AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER');}
end; end;
end; end;
function TConvertSettings.GetCallConv(withSemiColon: Boolean): AnsiString;
begin
Result := CallConv;
if (Result <> '') and withSemiColon then
Result := Result + ';';
end;
procedure TConvertSettings.SetCallConv(const ACallConv: String);
begin
fCallConv := ACallConv;
if AnsiLowerCase(fCallConv) = 'cdecl' then
fExternPrefix := ''
else
fExternPrefix := '_';
end;
{ TReplaceList } { TReplaceList }
constructor TReplaceList.Create; constructor TReplaceList.Create;

View File

@ -417,7 +417,8 @@ begin
prm := AnsiLowerCase(prm); prm := AnsiLowerCase(prm);
if prm = 'noout' then doOutput:=false if prm = 'noout' then doOutput:=false
else if prm = 'all' then doparseAll:=true else if prm = 'all' then doparseAll:=true
else if (prm = 'id') and (vlm <> '') then ObjcIDReplace:=vlm else if (prm = 'id') and (vlm <> '') then ConvertSettings.ObjcIDReplace:=vlm
else if (prm = 'call') then ConvertSettings.CallConv:=vlm
else if prm = 'ini' then begin else if prm = 'ini' then begin
ReadIniFile(Settings, vlm); ReadIniFile(Settings, vlm);
end else end else
@ -451,7 +452,12 @@ begin
writeln(''); writeln('');
writeln(' hidden keys (they''re temporary, and will be removed in future versions)'); writeln(' hidden keys (they''re temporary, and will be removed in future versions)');
writeln(' -id=IDENTIFIER the identifier to replace objective-c id type name'); writeln(' -id=IDENTIFIER the identifier to replace objective-c id type name');
writeln(' default = objc.id') writeln(' default = objc.id');
writeln(' -call=IDENTIFIER specifies the function''s calling convention.');
writeln(' default is cdecl. Please note, that calling convention');
writeln(' also effect external functions name. Thus, using ');
writeln(' if calling convention is not cdecl, the external name');
writeln(' will be prefixed with underscore');
end; end;
var var