def.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. {
  2. pas2jni - JNI bridge generator for Pascal.
  3. Copyright (c) 2013 by Yury Sidorov.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit def;
  17. {$mode objfpc}{$H+}
  18. interface
  19. uses
  20. Classes, SysUtils, contnrs;
  21. type
  22. TDefType = (dtNone, dtUnit, dtClass, dtProc, dtField, dtProp, dtParam, dtVar,
  23. dtType, dtConst, dtProcType, dtEnum, dtSet, dtPointer, dtArray,
  24. dtJniObject, dtJniEnv);
  25. TDefClass = class of TDef;
  26. { TDef }
  27. TDef = class
  28. private
  29. FAliasName: string;
  30. FRefCnt: integer;
  31. FItems: TObjectList;
  32. FInSetUsed: boolean;
  33. procedure CheckItems;
  34. function GetAliasName: string;
  35. function GetCount: integer;
  36. function GetIsUsed: boolean;
  37. function GetItem(Index: Integer): TDef;
  38. procedure SetItem(Index: Integer; const AValue: TDef);
  39. protected
  40. procedure SetIsUsed(const AValue: boolean); virtual;
  41. function ResolveDef(d: TDef; ExpectedClass: TDefClass = nil): TDef;
  42. procedure AddRef;
  43. procedure DecRef;
  44. procedure SetExtUsed(ExtDef: TDef; AUsed: boolean; var HasRef: boolean);
  45. public
  46. DefType: TDefType;
  47. DefId: integer;
  48. SymId: integer;
  49. Name: string;
  50. Parent: TDef;
  51. Tag: integer;
  52. IsPrivate: boolean;
  53. constructor Create; virtual; overload;
  54. constructor Create(AParent: TDef; AType: TDefType); virtual; overload;
  55. destructor Destroy; override;
  56. function Add(ADef: TDef): integer;
  57. function Insert(Index: integer; ADef: TDef): integer;
  58. function FindDef(ADefId: integer; Recursive: boolean = True): TDef;
  59. procedure ResolveDefs; virtual;
  60. procedure SetNotUsed;
  61. property Items[Index: Integer]: TDef read GetItem write SetItem; default;
  62. property Count: integer read GetCount;
  63. property IsUsed: boolean read GetIsUsed write SetIsUsed;
  64. property RefCnt: integer read FRefCnt;
  65. property AliasName: string read GetAliasName write FAliasName;
  66. end;
  67. TClassType = (ctClass, ctInterface, ctObject, ctRecord);
  68. { TClassDef }
  69. TClassDef = class(TDef)
  70. private
  71. FHasClassRef: boolean;
  72. protected
  73. procedure SetIsUsed(const AValue: boolean); override;
  74. public
  75. CType: TClassType;
  76. AncestorClass: TClassDef;
  77. HasAbstractMethods: boolean;
  78. HasReplacedItems: boolean;
  79. ImplementsReplacedItems: boolean;
  80. Size: integer;
  81. procedure ResolveDefs; override;
  82. end;
  83. TBasicType = (btVoid, btByte, btShortInt, btWord, btSmallInt, btLongWord, btLongInt, btInt64,
  84. btSingle, btDouble, btString, btWideString, btBoolean, btChar, btWideChar, btEnum,
  85. btGuid);
  86. { TTypeDef }
  87. TTypeDef = class(TDef)
  88. protected
  89. procedure SetIsUsed(const AValue: boolean); override;
  90. public
  91. BasicType: TBasicType;
  92. end;
  93. { TPointerDef }
  94. TPointerDef = class(TDef)
  95. private
  96. FHasPtrRef: boolean;
  97. protected
  98. procedure SetIsUsed(const AValue: boolean); override;
  99. public
  100. PtrType: TDef;
  101. procedure ResolveDefs; override;
  102. function IsObjPtr: boolean;
  103. end;
  104. { TReplDef }
  105. TReplDef = class(TDef)
  106. protected
  107. procedure SetIsUsed(const AValue: boolean); override;
  108. public
  109. IsReplaced: boolean;
  110. IsReplImpl: boolean;
  111. ReplacedItem: TReplDef;
  112. function CanReplaced: boolean; virtual;
  113. function IsReplacedBy(d: TReplDef): boolean; virtual;
  114. procedure CheckReplaced;
  115. end;
  116. TVarOption = (voRead, voWrite, voConst, voVar, voOut);
  117. TVarOptions = set of TVarOption;
  118. { TVarDef }
  119. TVarDef = class(TReplDef)
  120. private
  121. FHasTypeRef: boolean;
  122. protected
  123. procedure SetIsUsed(const AValue: boolean); override;
  124. public
  125. VarOpt: TVarOptions;
  126. VarType: TDef;
  127. constructor Create; override;
  128. procedure ResolveDefs; override;
  129. function IsReplacedBy(d: TReplDef): boolean; override;
  130. function CanReplaced: boolean; override;
  131. end;
  132. TProcType = (ptProcedure, ptFunction, ptConstructor, ptDestructor);
  133. TProcOption = (poOverride, poOverload, poMethodPtr, poPrivate, poProtected);
  134. TProcOptions = set of TProcOption;
  135. { TProcDef }
  136. TProcDef = class(TReplDef)
  137. private
  138. FHasRetTypeRef: boolean;
  139. protected
  140. procedure SetIsUsed(const AValue: boolean); override;
  141. public
  142. ProcType: TProcType;
  143. ReturnType: TDef;
  144. ProcOpt: TProcOptions;
  145. procedure ResolveDefs; override;
  146. function IsReplacedBy(d: TReplDef): boolean; override;
  147. function CanReplaced: boolean; override;
  148. end;
  149. TUnitDef = class(TDef)
  150. public
  151. OS: string;
  152. CPU: string;
  153. IntfCRC: string;
  154. PPUVer: integer;
  155. UsedUnits: array of TUnitDef;
  156. Processed: boolean;
  157. end;
  158. TConstDef = class(TVarDef)
  159. public
  160. Value: string;
  161. end;
  162. { TSetDef }
  163. TSetDef = class(TDef)
  164. private
  165. FHasElTypeRef: boolean;
  166. protected
  167. procedure SetIsUsed(const AValue: boolean); override;
  168. public
  169. Size: integer;
  170. Base: integer;
  171. ElMax: integer;
  172. ElType: TTypeDef;
  173. end;
  174. { TArrayDef }
  175. TArrayDef = class(TDef)
  176. private
  177. FHasElTypeRef: boolean;
  178. FHasRTypeRef: boolean;
  179. protected
  180. procedure SetIsUsed(const AValue: boolean); override;
  181. public
  182. ElType: TDef;
  183. RangeType: TDef;
  184. RangeLow, RangeHigh: integer;
  185. end;
  186. const
  187. ReplDefs = [dtField, dtProp, dtProc];
  188. implementation
  189. function IsSameType(t1, t2: TDef): boolean;
  190. begin
  191. Result:=t1 = t2;
  192. if Result then
  193. exit;
  194. if (t1 = nil) or (t2 = nil) or (t1.DefType <> t2.DefType) then
  195. exit;
  196. if t1.DefType <> dtType then
  197. exit;
  198. Result:=TTypeDef(t1).BasicType = TTypeDef(t2).BasicType;
  199. end;
  200. { TArrayDef }
  201. procedure TArrayDef.SetIsUsed(const AValue: boolean);
  202. begin
  203. inherited SetIsUsed(AValue);
  204. SetExtUsed(ElType, AValue, FHasElTypeRef);
  205. SetExtUsed(RangeType, AValue, FHasRTypeRef);
  206. end;
  207. { TPointerDef }
  208. procedure TPointerDef.SetIsUsed(const AValue: boolean);
  209. begin
  210. if IsObjPtr then begin
  211. inherited SetIsUsed(AValue);
  212. SetExtUsed(PtrType, AValue, FHasPtrRef);
  213. end
  214. else
  215. if AValue then
  216. AddRef
  217. else
  218. DecRef;
  219. end;
  220. procedure TPointerDef.ResolveDefs;
  221. begin
  222. inherited ResolveDefs;
  223. PtrType:=ResolveDef(PtrType);
  224. end;
  225. function TPointerDef.IsObjPtr: boolean;
  226. begin
  227. Result:=(PtrType <> nil) and (PtrType.DefType in [dtClass]);
  228. end;
  229. { TReplDef }
  230. procedure TReplDef.SetIsUsed(const AValue: boolean);
  231. var
  232. i: integer;
  233. begin
  234. i:=RefCnt;
  235. inherited SetIsUsed(AValue);
  236. if (i = 0) and (RefCnt > 0) then
  237. CheckReplaced;
  238. end;
  239. function TReplDef.CanReplaced: boolean;
  240. begin
  241. Result:=not (IsPrivate or (Parent = nil) or (Parent.DefType <> dtClass));
  242. end;
  243. function TReplDef.IsReplacedBy(d: TReplDef): boolean;
  244. begin
  245. Result:=d.CanReplaced and (CompareText(Name, d.Name) = 0);
  246. end;
  247. procedure TReplDef.CheckReplaced;
  248. function _Scan(cls: TClassDef): boolean;
  249. var
  250. i: integer;
  251. d: TReplDef;
  252. c: TClassDef;
  253. begin
  254. Result:=False;
  255. c:=cls.AncestorClass;
  256. if c = nil then
  257. exit;
  258. for i:=0 to c.Count - 1 do begin
  259. d:=TReplDef(c[i]);
  260. if (d.DefType in ReplDefs) and IsReplacedBy(d) then begin
  261. d.IsReplaced:=True;
  262. ReplacedItem:=d;
  263. Result:=True;
  264. break;
  265. end;
  266. end;
  267. if not Result then
  268. Result:=_Scan(c);
  269. if Result then begin
  270. cls.ImplementsReplacedItems:=True;
  271. c.HasReplacedItems:=True;
  272. end;
  273. end;
  274. begin
  275. if not CanReplaced then
  276. exit;
  277. if _Scan(TClassDef(Parent)) then
  278. IsReplImpl:=True;
  279. end;
  280. { TSetDef }
  281. procedure TSetDef.SetIsUsed(const AValue: boolean);
  282. begin
  283. inherited SetIsUsed(AValue);
  284. SetExtUsed(ElType, AValue, FHasElTypeRef);
  285. end;
  286. { TTypeDef }
  287. procedure TTypeDef.SetIsUsed(const AValue: boolean);
  288. begin
  289. if BasicType in [btEnum] then
  290. inherited SetIsUsed(AValue)
  291. else
  292. if AValue then
  293. AddRef
  294. else
  295. DecRef;
  296. end;
  297. { TProcDef }
  298. procedure TProcDef.SetIsUsed(const AValue: boolean);
  299. var
  300. i: integer;
  301. begin
  302. if IsPrivate then
  303. exit;
  304. if AValue and (RefCnt = 0) then begin
  305. for i:=0 to Count - 1 do
  306. if (Items[i].DefType = dtParam) and (TVarDef(Items[i]).VarType = nil) then
  307. exit; // If procedure has unsupported parameters, don't use it
  308. end;
  309. inherited SetIsUsed(AValue);
  310. if ReturnType <> Parent then
  311. SetExtUsed(ReturnType, AValue, FHasRetTypeRef);
  312. end;
  313. procedure TProcDef.ResolveDefs;
  314. begin
  315. inherited ResolveDefs;
  316. ReturnType:=ResolveDef(ReturnType);
  317. end;
  318. function TProcDef.IsReplacedBy(d: TReplDef): boolean;
  319. var
  320. i: integer;
  321. p: TProcDef;
  322. begin
  323. Result:=False;
  324. if d.DefType <> dtProc then
  325. exit;
  326. p:=TProcDef(d);
  327. if (Count = p.Count) and inherited IsReplacedBy(p) then begin
  328. // Check parameter types
  329. for i:=0 to Count - 1 do
  330. if not IsSameType(TVarDef(Items[i]).VarType, TVarDef(p.Items[i]).VarType) then
  331. exit;
  332. Result:=True;
  333. end;
  334. end;
  335. function TProcDef.CanReplaced: boolean;
  336. begin
  337. Result:=inherited CanReplaced and (ProcType = ptFunction);
  338. end;
  339. { TClassDef }
  340. procedure TClassDef.SetIsUsed(const AValue: boolean);
  341. begin
  342. inherited SetIsUsed(AValue);
  343. SetExtUsed(AncestorClass, AValue, FHasClassRef);
  344. end;
  345. procedure TClassDef.ResolveDefs;
  346. begin
  347. inherited ResolveDefs;
  348. AncestorClass:=TClassDef(ResolveDef(AncestorClass, TClassDef));
  349. end;
  350. { TVarDef }
  351. procedure TVarDef.SetIsUsed(const AValue: boolean);
  352. begin
  353. if IsPrivate then
  354. exit;
  355. inherited SetIsUsed(AValue);
  356. SetExtUsed(VarType, AValue, FHasTypeRef);
  357. end;
  358. procedure TVarDef.ResolveDefs;
  359. begin
  360. inherited ResolveDefs;
  361. VarType:=ResolveDef(VarType);
  362. end;
  363. function TVarDef.IsReplacedBy(d: TReplDef): boolean;
  364. begin
  365. Result:=(d.DefType in [dtProp, dtField]) and not IsSameType(VarType, TVarDef(d).VarType) and inherited IsReplacedBy(d);
  366. end;
  367. function TVarDef.CanReplaced: boolean;
  368. begin
  369. Result:=(voRead in VarOpt) and inherited CanReplaced;
  370. end;
  371. constructor TVarDef.Create;
  372. begin
  373. inherited Create;
  374. VarOpt:=[voRead, voWrite];
  375. end;
  376. { TDef }
  377. procedure TDef.CheckItems;
  378. begin
  379. if FItems = nil then
  380. FItems:=TObjectList.Create(True);
  381. end;
  382. function TDef.GetAliasName: string;
  383. begin
  384. if FAliasName <> '' then
  385. Result:=FAliasName
  386. else
  387. Result:=Name;
  388. end;
  389. function TDef.GetCount: integer;
  390. begin
  391. if FItems = nil then
  392. Result:=0
  393. else begin
  394. CheckItems;
  395. Result:=FItems.Count;
  396. end;
  397. end;
  398. function TDef.GetIsUsed: boolean;
  399. begin
  400. Result:=FRefCnt > 0;
  401. end;
  402. function TDef.GetItem(Index: Integer): TDef;
  403. begin
  404. CheckItems;
  405. Result:=TDef(FItems[Index]);
  406. end;
  407. procedure TDef.SetIsUsed(const AValue: boolean);
  408. var
  409. i: integer;
  410. f: boolean;
  411. begin
  412. if FInSetUsed or (DefType = dtNone) or IsPrivate then
  413. exit;
  414. if AValue then begin
  415. AddRef;
  416. f:=FRefCnt = 1;
  417. end
  418. else begin
  419. if FRefCnt = 0 then
  420. exit;
  421. DecRef;
  422. f:=FRefCnt = 0;
  423. end;
  424. if f then begin
  425. // Update used mark of children only once
  426. FInSetUsed:=True;
  427. try
  428. for i:=0 to Count - 1 do
  429. Items[i].IsUsed:=AValue;
  430. finally
  431. FInSetUsed:=False;
  432. end;
  433. // Update parent's used mark
  434. if (Parent <> nil) and (Parent.DefType = dtUnit) then
  435. if AValue then
  436. Parent.AddRef
  437. else
  438. Parent.DecRef;
  439. end;
  440. end;
  441. function TDef.ResolveDef(d: TDef; ExpectedClass: TDefClass): TDef;
  442. begin
  443. if (d = nil) or (d.DefType <> dtNone) then
  444. Result:=d
  445. else begin
  446. Result:=d.Parent.FindDef(d.DefId);
  447. if (ExpectedClass <> nil) and (Result <> nil) then
  448. if not (Result is ExpectedClass) then
  449. raise Exception.CreateFmt('Unexpected class. Expected: %s, got: %s', [ExpectedClass.ClassName, Result.ClassName]);
  450. end;
  451. end;
  452. procedure TDef.AddRef;
  453. begin
  454. Inc(FRefCnt);
  455. end;
  456. procedure TDef.DecRef;
  457. begin
  458. if FRefCnt > 0 then
  459. Dec(FRefCnt);
  460. end;
  461. procedure TDef.SetExtUsed(ExtDef: TDef; AUsed: boolean; var HasRef: boolean);
  462. var
  463. OldRefCnt: integer;
  464. begin
  465. if ExtDef = nil then
  466. exit;
  467. if AUsed then begin
  468. if HasRef then
  469. exit;
  470. OldRefCnt:=ExtDef.RefCnt;
  471. ExtDef.IsUsed:=True;
  472. HasRef:=OldRefCnt <> ExtDef.RefCnt;
  473. end
  474. else
  475. if HasRef and not IsUsed then begin
  476. ExtDef.IsUsed:=False;
  477. HasRef:=False;
  478. end;
  479. end;
  480. procedure TDef.SetItem(Index: Integer; const AValue: TDef);
  481. begin
  482. CheckItems;
  483. FItems[Index]:=AValue;
  484. end;
  485. constructor TDef.Create;
  486. begin
  487. DefId:=-1;
  488. DefType:=dtNone;
  489. end;
  490. constructor TDef.Create(AParent: TDef; AType: TDefType);
  491. begin
  492. Create;
  493. if AParent <> nil then
  494. AParent.Add(Self);
  495. DefType:=AType;
  496. end;
  497. destructor TDef.Destroy;
  498. begin
  499. FreeAndNil(FItems);
  500. if (Parent <> nil) and (Parent.FItems <> nil) then begin
  501. Parent.FItems.OwnsObjects:=False;
  502. try
  503. Parent.FItems.Remove(Self);
  504. finally
  505. Parent.FItems.OwnsObjects:=True;
  506. end;
  507. end;
  508. inherited Destroy;
  509. end;
  510. function TDef.Add(ADef: TDef): integer;
  511. begin
  512. Result:=Insert(Count, ADef);
  513. end;
  514. function TDef.Insert(Index: integer; ADef: TDef): integer;
  515. begin
  516. CheckItems;
  517. Result:=Index;
  518. FItems.Insert(Result, ADef);
  519. ADef.Parent:=Self;
  520. end;
  521. function TDef.FindDef(ADefId: integer; Recursive: boolean): TDef;
  522. function _Find(d: TDef): TDef;
  523. var
  524. i: integer;
  525. begin
  526. Result:=nil;
  527. for i:=0 to d.Count - 1 do
  528. with d[i] do begin
  529. if (DefType <> dtNone) and (DefId = ADefId) then begin
  530. Result:=d[i];
  531. break;
  532. end;
  533. if Recursive and (Count > 0) then begin
  534. Result:=_Find(d[i]);
  535. if Result <> nil then
  536. break;
  537. end;
  538. end;
  539. end;
  540. begin
  541. Result:=_Find(Self);
  542. end;
  543. procedure TDef.ResolveDefs;
  544. var
  545. i: integer;
  546. begin
  547. for i:=0 to Count - 1 do
  548. Items[i].ResolveDefs;
  549. end;
  550. procedure TDef.SetNotUsed;
  551. begin
  552. if FRefCnt = 0 then
  553. exit;
  554. FRefCnt:=1;
  555. IsUsed:=False;
  556. end;
  557. end.