mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-11-14 21:40:00 +01:00
* pas2jni: Basic support for arrays.
git-svn-id: trunk@32578 -
This commit is contained in:
parent
03067f240c
commit
0e056ece03
@ -30,7 +30,7 @@ uses
|
|||||||
|
|
||||||
type
|
type
|
||||||
TDefType = (dtNone, dtUnit, dtClass, dtProc, dtField, dtProp, dtParam, dtVar,
|
TDefType = (dtNone, dtUnit, dtClass, dtProc, dtField, dtProp, dtParam, dtVar,
|
||||||
dtType, dtConst, dtProcType, dtEnum, dtSet, dtPointer);
|
dtType, dtConst, dtProcType, dtEnum, dtSet, dtPointer, dtArray);
|
||||||
|
|
||||||
TDefClass = class of TDef;
|
TDefClass = class of TDef;
|
||||||
{ TDef }
|
{ TDef }
|
||||||
@ -205,6 +205,20 @@ type
|
|||||||
ElType: TTypeDef;
|
ElType: TTypeDef;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TArrayDef }
|
||||||
|
|
||||||
|
TArrayDef = class(TDef)
|
||||||
|
private
|
||||||
|
FHasElTypeRef: boolean;
|
||||||
|
FHasRTypeRef: boolean;
|
||||||
|
protected
|
||||||
|
procedure SetIsUsed(const AValue: boolean); override;
|
||||||
|
public
|
||||||
|
ElType: TDef;
|
||||||
|
RangeType: TDef;
|
||||||
|
RangeLow, RangeHigh: integer;
|
||||||
|
end;
|
||||||
|
|
||||||
const
|
const
|
||||||
ReplDefs = [dtField, dtProp, dtProc];
|
ReplDefs = [dtField, dtProp, dtProc];
|
||||||
|
|
||||||
@ -222,6 +236,15 @@ begin
|
|||||||
Result:=TTypeDef(t1).BasicType = TTypeDef(t2).BasicType;
|
Result:=TTypeDef(t1).BasicType = TTypeDef(t2).BasicType;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TArrayDef }
|
||||||
|
|
||||||
|
procedure TArrayDef.SetIsUsed(const AValue: boolean);
|
||||||
|
begin
|
||||||
|
inherited SetIsUsed(AValue);
|
||||||
|
SetExtUsed(ElType, AValue, FHasElTypeRef);
|
||||||
|
SetExtUsed(RangeType, AValue, FHasRTypeRef);
|
||||||
|
end;
|
||||||
|
|
||||||
{ TPointerDef }
|
{ TPointerDef }
|
||||||
|
|
||||||
procedure TPointerDef.SetIsUsed(const AValue: boolean);
|
procedure TPointerDef.SetIsUsed(const AValue: boolean);
|
||||||
|
|||||||
@ -382,10 +382,13 @@ var
|
|||||||
else
|
else
|
||||||
if jt = 'const' then
|
if jt = 'const' then
|
||||||
d:=TConstDef.Create(CurDef, dtConst)
|
d:=TConstDef.Create(CurDef, dtConst)
|
||||||
|
else
|
||||||
|
if jt = 'array' then
|
||||||
|
d:=TArrayDef.Create(CurDef, dtArray)
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (CurObjName = '') and (d.DefType <> dtEnum) then begin
|
if (CurObjName = '') and not (d.DefType in [dtEnum, dtArray]) then begin
|
||||||
d.Free;
|
d.Free;
|
||||||
continue;
|
continue;
|
||||||
end;
|
end;
|
||||||
@ -520,6 +523,14 @@ var
|
|||||||
with TPointerDef(d) do begin
|
with TPointerDef(d) do begin
|
||||||
PtrType:=_GetRef(it.Get('Ptr', TJSONObject(nil)));;
|
PtrType:=_GetRef(it.Get('Ptr', TJSONObject(nil)));;
|
||||||
end;
|
end;
|
||||||
|
dtArray:
|
||||||
|
with TArrayDef(d) do begin
|
||||||
|
_ReadDefs(d, it, 'Types');
|
||||||
|
RangeLow:=it.Get('Low', -1);
|
||||||
|
RangeHigh:=it.Get('High', -1);
|
||||||
|
RangeType:=_GetRef(it.Get('RangeType', TJSONObject(nil)));
|
||||||
|
ElType:=_GetRef(it.Get('ElType', TJSONObject(nil)));
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|||||||
@ -63,6 +63,7 @@ type
|
|||||||
FPkgDir: string;
|
FPkgDir: string;
|
||||||
FUniqueCnt: integer;
|
FUniqueCnt: integer;
|
||||||
FThisUnit: TUnitDef;
|
FThisUnit: TUnitDef;
|
||||||
|
FIntegerType: TDef;
|
||||||
|
|
||||||
function DoCheckItem(const ItemName: string): TCheckItemResult;
|
function DoCheckItem(const ItemName: string): TCheckItemResult;
|
||||||
|
|
||||||
@ -844,11 +845,13 @@ begin
|
|||||||
s:='__objvar.';
|
s:='__objvar.';
|
||||||
end;
|
end;
|
||||||
s:=s + Variable.Name;
|
s:=s + Variable.Name;
|
||||||
if Variable.Count > 0 then begin
|
j:=Count;
|
||||||
ASSERT(Count >= 1);
|
if ProcType = ptProcedure then
|
||||||
i:=Variable.Count;
|
Dec(j);
|
||||||
|
if j > 0 then begin
|
||||||
|
i:=j;
|
||||||
ss:='';
|
ss:='';
|
||||||
for j:=0 to Variable.Count - 1 do begin
|
for j:=0 to j - 1 do begin
|
||||||
if ss <> '' then
|
if ss <> '' then
|
||||||
ss:=ss + ', ';
|
ss:=ss + ', ';
|
||||||
ss:=ss + JniToPasType(TVarDef(Items[j]).VarType, Items[j].Name, False);
|
ss:=ss + JniToPasType(TVarDef(Items[j]).VarType, Items[j].Name, False);
|
||||||
@ -968,11 +971,36 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TWriter.WriteVar(d: TVarDef; AParent: TDef);
|
procedure TWriter.WriteVar(d: TVarDef; AParent: TDef);
|
||||||
|
|
||||||
|
function _WriteArrayIndex(pd: TProcDef): TDef;
|
||||||
|
var
|
||||||
|
ad: TArrayDef;
|
||||||
|
i: integer;
|
||||||
|
begin
|
||||||
|
ad:=TArrayDef(d.VarType);
|
||||||
|
i:=1;
|
||||||
|
repeat
|
||||||
|
with TVarDef.Create(pd, dtParam) do begin
|
||||||
|
Name:='Index';
|
||||||
|
if i > 1 then
|
||||||
|
Name:=Name + IntToStr(i);
|
||||||
|
VarType:=ad.RangeType;
|
||||||
|
if (VarType.DefType = dtType) and (TTypeDef(VarType).BasicType in [btByte, btShortInt, btSmallInt]) then
|
||||||
|
VarType:=FIntegerType;
|
||||||
|
VarOpt:=[voRead];
|
||||||
|
end;
|
||||||
|
Result:=ad.ElType;
|
||||||
|
ad:=TArrayDef(Result);
|
||||||
|
Inc(i);
|
||||||
|
until Result.DefType <> dtArray;
|
||||||
|
end;
|
||||||
|
|
||||||
var
|
var
|
||||||
pd: TProcDef;
|
pd: TProcDef;
|
||||||
vd: TVarDef;
|
vd: TVarDef;
|
||||||
t: TTypeDef;
|
t: TTypeDef;
|
||||||
s: string;
|
vt: TDef;
|
||||||
|
s, ss: string;
|
||||||
i: integer;
|
i: integer;
|
||||||
begin
|
begin
|
||||||
if not d.IsUsed then
|
if not d.IsUsed then
|
||||||
@ -989,7 +1017,11 @@ begin
|
|||||||
s:=Trim(s + ' ' + d.Name);
|
s:=Trim(s + ' ' + d.Name);
|
||||||
if d.Count > 0 then
|
if d.Count > 0 then
|
||||||
s:=s + '[]';
|
s:=s + '[]';
|
||||||
Fjs.WriteLn(Format('// %s: %s', [s, d.VarType.Name]));
|
ss:=d.VarType.Name;
|
||||||
|
if ss = '' then
|
||||||
|
if d.VarType.DefType = dtArray then
|
||||||
|
ss:='array';
|
||||||
|
Fjs.WriteLn(Format('// %s: %s', [s, ss]));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
if voRead in d.VarOpt then begin
|
if voRead in d.VarOpt then begin
|
||||||
@ -999,6 +1031,10 @@ begin
|
|||||||
pd.Parent:=d.Parent;
|
pd.Parent:=d.Parent;
|
||||||
pd.ProcType:=ptFunction;
|
pd.ProcType:=ptFunction;
|
||||||
pd.Name:='get' + d.Name;
|
pd.Name:='get' + d.Name;
|
||||||
|
if (d.VarType <> nil) and (d.VarType.DefType = dtArray) then
|
||||||
|
// Array var
|
||||||
|
pd.ReturnType:=_WriteArrayIndex(pd)
|
||||||
|
else begin
|
||||||
pd.ReturnType:=d.VarType;
|
pd.ReturnType:=d.VarType;
|
||||||
if d.DefType = dtProp then begin
|
if d.DefType = dtProp then begin
|
||||||
for i:=0 to d.Count - 1 do begin
|
for i:=0 to d.Count - 1 do begin
|
||||||
@ -1010,6 +1046,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
WriteProc(pd, d, AParent);
|
WriteProc(pd, d, AParent);
|
||||||
finally
|
finally
|
||||||
pd.Free;
|
pd.Free;
|
||||||
@ -1023,8 +1060,11 @@ begin
|
|||||||
pd.Parent:=d.Parent;
|
pd.Parent:=d.Parent;
|
||||||
pd.ProcType:=ptProcedure;
|
pd.ProcType:=ptProcedure;
|
||||||
pd.Name:='set' + d.Name;
|
pd.Name:='set' + d.Name;
|
||||||
|
vt:=d.VarType;;
|
||||||
s:='Value';
|
if (d.VarType <> nil) and (d.VarType.DefType = dtArray) then
|
||||||
|
// Array var
|
||||||
|
vt:=_WriteArrayIndex(pd)
|
||||||
|
else
|
||||||
if d.DefType = dtProp then begin
|
if d.DefType = dtProp then begin
|
||||||
for i:=0 to d.Count - 1 do begin
|
for i:=0 to d.Count - 1 do begin
|
||||||
vd:=TVarDef(d.Items[i]);
|
vd:=TVarDef(d.Items[i]);
|
||||||
@ -1034,7 +1074,9 @@ begin
|
|||||||
VarOpt:=[voRead];
|
VarOpt:=[voRead];
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
s:='Value';
|
||||||
// Check if the name of value parameter is unique
|
// Check if the name of value parameter is unique
|
||||||
i:=0;
|
i:=0;
|
||||||
while i < d.Count do begin
|
while i < d.Count do begin
|
||||||
@ -1045,12 +1087,11 @@ begin
|
|||||||
end;
|
end;
|
||||||
Inc(i);
|
Inc(i);
|
||||||
end;
|
end;
|
||||||
end;
|
|
||||||
|
|
||||||
with TVarDef.Create(pd, dtParam) do begin
|
with TVarDef.Create(pd, dtParam) do begin
|
||||||
Name:='_' + s;
|
Name:='_' + s;
|
||||||
AliasName:=s;
|
AliasName:=s;
|
||||||
VarType:=d.VarType;
|
VarType:=vt;
|
||||||
VarOpt:=[voRead];
|
VarOpt:=[voRead];
|
||||||
end;
|
end;
|
||||||
t:=TTypeDef.Create(nil, dtType);
|
t:=TTypeDef.Create(nil, dtType);
|
||||||
@ -1391,6 +1432,15 @@ begin
|
|||||||
Fjs.WriteLn('public class ' + u.Name + ' {');
|
Fjs.WriteLn('public class ' + u.Name + ' {');
|
||||||
Fjs.IncI;
|
Fjs.IncI;
|
||||||
if u.Name = 'system' then begin
|
if u.Name = 'system' then begin
|
||||||
|
|
||||||
|
for i:=0 to u.Count - 1 do begin
|
||||||
|
d:=u[i];
|
||||||
|
if (d.DefType = dtType) and (TTypeDef(d).BasicType = btLongInt) then begin
|
||||||
|
FIntegerType:=d;
|
||||||
|
break;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
Fjs.WriteLn('static private boolean _JniLibLoaded = false;');
|
Fjs.WriteLn('static private boolean _JniLibLoaded = false;');
|
||||||
Fjs.WriteLn('public static void InitJni() {');
|
Fjs.WriteLn('public static void InitJni() {');
|
||||||
Fjs.WriteLn('if (!_JniLibLoaded) {', 1);
|
Fjs.WriteLn('if (!_JniLibLoaded) {', 1);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user