Browse Source

* pas2jni: Filter out advanced methods and properties of RTL classes to prevent overbloated code.

git-svn-id: trunk@43386 -
yury 5 years ago
parent
commit
937683ec8f
1 changed files with 85 additions and 22 deletions
  1. 85 22
      utils/pas2jni/writer.pas

+ 85 - 22
utils/pas2jni/writer.pas

@@ -87,6 +87,8 @@ type
     function GetClassInfo(Index: integer): TClassInfo;
     function GetClassInfo(Index: integer): TClassInfo;
   end;
   end;
 
 
+  TMatchType = (mtNone, mtExact, mtWildcard, mtParams);
+
   { TWriter }
   { TWriter }
 
 
   TWriter = class
   TWriter = class
@@ -105,6 +107,7 @@ type
 
 
     procedure WriteFileComment(st: TTextOutStream);
     procedure WriteFileComment(st: TTextOutStream);
     function FindInStringList(list: TStringList; const s: string): integer;
     function FindInStringList(list: TStringList; const s: string): integer;
+    function FindInStringListEx(list: TStringList; const s: string; AllMatch: boolean; out MatchType: TMatchType): integer;
 
 
     procedure ProcessRules(d: TDef; const Prefix: string = '');
     procedure ProcessRules(d: TDef; const Prefix: string = '');
     function GetUniqueNum: integer;
     function GetUniqueNum: integer;
@@ -193,14 +196,25 @@ const
     'system.fma', 'system.TExtended80Rec'
     'system.fma', 'system.TExtended80Rec'
   );
   );
 
 
-  ExcludeDelphi7: array[1..26] of string = (
+  ExcludeDelphi7: array[1..57] of string = (
     'system.TObject.StringMessageTable', 'system.TObject.GetInterfaceEntryByStr', 'system.TObject.UnitName', 'system.TObject.Equals',
     'system.TObject.StringMessageTable', 'system.TObject.GetInterfaceEntryByStr', 'system.TObject.UnitName', 'system.TObject.Equals',
-    'system.TObject.GetHashCode', 'system.TObject.ToString','system.TObject.QualifiedClassName','classes.TStream.ReadByte', 'classes.TStream.ReadWord',
+    'system.TObject.GetHashCode', 'system.TObject.ToString','system.TObject.QualifiedClassName',
+    'sysutils.TEncoding',
+    'classes.TStream.ReadByte', 'classes.TStream.ReadWord',
     'classes.TStream.ReadDWord', 'classes.TStream.ReadQWord', 'classes.TStream.ReadAnsiString', 'classes.TStream.WriteByte',
     'classes.TStream.ReadDWord', 'classes.TStream.ReadQWord', 'classes.TStream.ReadAnsiString', 'classes.TStream.WriteByte',
     'classes.TStream.WriteWord', 'classes.TStream.WriteDWord', 'classes.TStream.WriteQWord', 'classes.TStream.WriteAnsiString',
     'classes.TStream.WriteWord', 'classes.TStream.WriteDWord', 'classes.TStream.WriteQWord', 'classes.TStream.WriteAnsiString',
+    'classes.TStream.ReadData', 'classes.TStream.ReadBufferData', 'classes.TStream.WriteData', 'classes.TStream.WriteBufferData',
     'classes.TCollection.Exchange', 'classes.TStrings.Equals', 'classes.TStrings.GetNameValue', 'classes.TStrings.ExtractName',
     'classes.TCollection.Exchange', 'classes.TStrings.Equals', 'classes.TStrings.GetNameValue', 'classes.TStrings.ExtractName',
     'classes.TStrings.TextLineBreakStyle', 'classes.TStrings.StrictDelimiter', 'classes.TStrings.GetEnumerator', 'classes.TStringList.OwnsObjects',
     'classes.TStrings.TextLineBreakStyle', 'classes.TStrings.StrictDelimiter', 'classes.TStrings.GetEnumerator', 'classes.TStringList.OwnsObjects',
-    'classes.TList.AddList'
+    'classes.TStrings.Filter', 'classes.TStrings.ForEach', 'classes.TStrings.Reduce', 'classes.TStrings.Map', 'classes.TStrings.AddPair',
+    'classes.TStrings.AddText', 'classes.TStrings.Fill', 'classes.TStrings.LastIndexOf', 'classes.TStrings.Pop', 'classes.TStrings.Reverse',
+    'classes.TStrings.Shift', 'classes.TStrings.Slice', 'classes.TStrings.AlwaysQuote', 'classes.TStrings.LineBreak',
+    'classes.TStrings.MissingNameValueSeparatorAction', 'classes.TStrings.SkipLastLineBreak', 'classes.TStrings.TrailingLineBreak', 'classes.TStrings.WriteBOM',
+    'classes.TStrings.AddStrings#ClearFirst', 'classes.TStrings.IndexOf#aStart', 'classes.TStrings.LoadFromFile#IgnoreEncoding',
+    'classes.TStrings.LoadFromStream#IgnoreEncoding',
+    'classes.TStringList.SortStyle',
+    'classes.TList.AddList', 'classes.TCustomMemoryStream.SizeBoundsSeek', 'classes.TBytesStream',
+    'sortbase'
   );
   );
 
 
   SUnsupportedType = '<unsupported type>';
   SUnsupportedType = '<unsupported type>';
@@ -375,11 +389,23 @@ begin
 end;
 end;
 
 
 function TWriter.FindInStringList(list: TStringList; const s: string): integer;
 function TWriter.FindInStringList(list: TStringList; const s: string): integer;
+var
+  mt: TMatchType;
+begin
+  Result:=FindInStringListEx(list, s, False, mt);
+end;
+
+function TWriter.FindInStringListEx(list: TStringList; const s: string; AllMatch: boolean; out MatchType: TMatchType): integer;
 var
 var
   len, cnt: integer;
   len, cnt: integer;
   ss: string;
   ss: string;
 begin
 begin
-  if list.Find(s, Result) or (Result < 0) then
+  MatchType:=mtNone;
+  if list.Find(s, Result) then begin
+    MatchType:=mtExact;
+    exit;
+  end;
+  if Result < 0 then
     exit;
     exit;
   if Result < list.Count then begin
   if Result < list.Count then begin
     cnt:=3;
     cnt:=3;
@@ -392,22 +418,36 @@ begin
     while cnt > 0 do begin
     while cnt > 0 do begin
       ss:=list[Result];
       ss:=list[Result];
       len:=Length(ss);
       len:=Length(ss);
-      if (len > 1) and (ss[len] = '*') then begin
-        Dec(len);
-        if AnsiCompareText(Copy(s, 1, len), Copy(ss, 1, len)) = 0 then
+      if len > 1 then begin
+        if ss[len] = '*' then begin
+          Dec(len);
+          MatchType:=mtWildcard;
+        end
+        else
+          if AllMatch then begin
+            len:=Pos('#', ss) - 1;
+            MatchType:=mtParams;
+          end
+          else
+            len:=0;
+
+        if (len > 0) and (AnsiCompareText(Copy(s, 1, len), Copy(ss, 1, len)) = 0) then
           exit;
           exit;
       end;
       end;
       Inc(Result);
       Inc(Result);
       Dec(cnt);
       Dec(cnt);
     end;
     end;
   end;
   end;
+  MatchType:=mtNone;
   Result:=-1;
   Result:=-1;
 end;
 end;
 
 
 procedure TWriter.ProcessRules(d: TDef; const Prefix: string);
 procedure TWriter.ProcessRules(d: TDef; const Prefix: string);
 var
 var
   i: integer;
   i: integer;
-  s: string;
+  s, c: string;
+  b: boolean;
+  mt: TMatchType;
 begin
 begin
   if d.DefType = dtClass then
   if d.DefType = dtClass then
     with TClassDef(d) do
     with TClassDef(d) do
@@ -415,15 +455,31 @@ begin
         SetNotUsed;
         SetNotUsed;
         exit;
         exit;
       end;
       end;
-  s:=Prefix + d.Name;
-  if FindInStringList(ExcludeList, s) >= 0 then
-    d.SetNotUsed
+  s:=Prefix + d.AliasName;
+  if FindInStringListEx(ExcludeList, s, (d.DefType = dtProc), mt) >= 0 then begin
+    if mt <> mtParams then begin
+      if d.DefType = dtParam then
+        d.Parent.SetNotUsed
+      else
+        d.SetNotUsed;
+    end;
+  end
   else
   else
     if FindInStringList(IncludeList, s) >= 0 then
     if FindInStringList(IncludeList, s) >= 0 then
       d.IsUsed:=True;
       d.IsUsed:=True;
-  if not (d.DefType in [dtUnit, dtClass]) then
+  b:=not (d.DefType in [dtUnit, dtClass]);
+  // Check exclusion rules for parameters of overloaded procs
+  if (d.DefType = dtProc) and (mt = mtParams) then begin
+    b:=False;
+    c:='#';
+  end
+  else
+    c:='.';
+
+  if b then
     exit;
     exit;
-  s:=s + '.';
+
+  s:=s + c;
   for i:=0 to d.Count - 1 do
   for i:=0 to d.Count - 1 do
     ProcessRules(d[i], s);
     ProcessRules(d[i], s);
 end;
 end;
@@ -2317,6 +2373,14 @@ begin
 end;
 end;
 
 
 function TWriter.JniToPasType(d: TDef; const v: string; CheckNil: boolean): string;
 function TWriter.JniToPasType(d: TDef; const v: string; CheckNil: boolean): string;
+
+  function _GetFullName(d: TDef): string;
+  begin
+    Result:=Format('%s.%s', [d.Parent.Name, d.Name]);
+    if Result = 'types.TDuplicates' then
+      Result:='classes.TDuplicates';  // Hack for Delphi 7 compatibility
+  end;
+
 var
 var
   n: string;
   n: string;
 begin
 begin
@@ -2361,7 +2425,7 @@ begin
     dtProcType:
     dtProcType:
       Result:=Format('%sGetHandler(_env, %s, %s)', [GetClassPrefix(d), Result, GetTypeInfoVar(d)]);
       Result:=Format('%sGetHandler(_env, %s, %s)', [GetClassPrefix(d), Result, GetTypeInfoVar(d)]);
     dtEnum:
     dtEnum:
-      Result:=Format('%s.%s(_GetIntObjValue(_env, %s, %s))', [d.Parent.Name, d.Name, Result, GetTypeInfoVar(d)]);
+      Result:=Format('%s(_GetIntObjValue(_env, %s, %s))', [_GetFullName(d), Result, GetTypeInfoVar(d)]);
     dtSet:
     dtSet:
       Result:=Format('%s.%s(%s(_GetIntObjValue(_env, %s, %s)))', [d.Parent.Name, d.Name, GetPasIntType(TSetDef(d).Size), Result, GetTypeInfoVar(d)]);
       Result:=Format('%s.%s(%s(_GetIntObjValue(_env, %s, %s)))', [d.Parent.Name, d.Name, GetPasIntType(TSetDef(d).Size), Result, GetTypeInfoVar(d)]);
     dtPointer:
     dtPointer:
@@ -2780,8 +2844,6 @@ begin
 end;
 end;
 }
 }
 constructor TWriter.Create;
 constructor TWriter.Create;
-var
-  i: integer;
 begin
 begin
   Units:=TStringList.Create;
   Units:=TStringList.Create;
   FClasses:=TClassList.Create;
   FClasses:=TClassList.Create;
@@ -2790,12 +2852,6 @@ begin
   IncludeList.Duplicates:=dupIgnore;
   IncludeList.Duplicates:=dupIgnore;
   ExcludeList:=TStringList.Create;
   ExcludeList:=TStringList.Create;
   ExcludeList.Duplicates:=dupIgnore;
   ExcludeList.Duplicates:=dupIgnore;
-
-  for i:=Low(ExcludeStd) to High(ExcludeStd) do
-    ExcludeList.Add(ExcludeStd[i]);
-  for i:=Low(ExcludeDelphi7) to High(ExcludeDelphi7) do
-    ExcludeList.Add(ExcludeDelphi7[i]);
-
   FThisUnit:=TUnitDef.Create(nil, dtUnit);
   FThisUnit:=TUnitDef.Create(nil, dtUnit);
   FRecords:=TObjectList.Create(False);
   FRecords:=TObjectList.Create(False);
   FRealClasses:=TObjectList.Create(False);
   FRealClasses:=TObjectList.Create(False);
@@ -2880,6 +2936,13 @@ begin
     ExcludeList[i]:=Trim(ExcludeList[i]);
     ExcludeList[i]:=Trim(ExcludeList[i]);
   ExcludeList.Sorted:=True;
   ExcludeList.Sorted:=True;
 
 
+  for i:=Low(ExcludeStd) to High(ExcludeStd) do
+    if IncludeList.IndexOf(ExcludeStd[i]) < 0 then
+      ExcludeList.Add(ExcludeStd[i]);
+  for i:=Low(ExcludeDelphi7) to High(ExcludeDelphi7) do
+    if IncludeList.IndexOf(ExcludeDelphi7[i]) < 0 then
+      ExcludeList.Add(ExcludeDelphi7[i]);
+
   FThisUnit.Name:=LibName;
   FThisUnit.Name:=LibName;
   FThisUnit.AliasName:='system';
   FThisUnit.AliasName:='system';