def.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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, dtRecord, dtProc, dtField, dtProp, dtParam, dtVar,
  23. dtType, dtConst, dtProcType, dtEnum, dtSet);
  24. TDefClass = class of TDef;
  25. { TDef }
  26. TDef = class
  27. private
  28. FAliasName: string;
  29. FRefCnt: integer;
  30. FItems: TObjectList;
  31. FInSetUsed: boolean;
  32. procedure CheckItems;
  33. function GetAliasName: string;
  34. function GetCount: integer;
  35. function GetIsUsed: boolean;
  36. function GetItem(Index: Integer): TDef;
  37. procedure SetItem(Index: Integer; const AValue: TDef);
  38. protected
  39. procedure SetIsUsed(const AValue: boolean); virtual;
  40. function ResolveDef(d: TDef; ExpectedClass: TDefClass = nil): TDef;
  41. procedure AddRef;
  42. procedure DecRef;
  43. procedure SetExtUsed(ExtDef: TDef; AUsed: boolean; var HasRef: boolean);
  44. public
  45. DefType: TDefType;
  46. DefId: integer;
  47. SymId: integer;
  48. Name: string;
  49. Parent: TDef;
  50. Tag: integer;
  51. IsPrivate: boolean;
  52. constructor Create; virtual; overload;
  53. constructor Create(AParent: TDef; AType: TDefType); virtual; overload;
  54. destructor Destroy; override;
  55. function Add(ADef: TDef): integer;
  56. function Insert(Index: integer; ADef: TDef): integer;
  57. function FindDef(ADefId: integer; Recursive: boolean = True): TDef;
  58. procedure ResolveDefs; virtual;
  59. procedure SetNotUsed;
  60. property Items[Index: Integer]: TDef read GetItem write SetItem; default;
  61. property Count: integer read GetCount;
  62. property IsUsed: boolean read GetIsUsed write SetIsUsed;
  63. property RefCnt: integer read FRefCnt;
  64. property AliasName: string read GetAliasName write FAliasName;
  65. end;
  66. { TClassDef }
  67. TClassDef = class(TDef)
  68. private
  69. FHasClassRef: boolean;
  70. protected
  71. procedure SetIsUsed(const AValue: boolean); override;
  72. public
  73. AncestorClass: TClassDef;
  74. HasAbstractMethods: boolean;
  75. HasReplacedItems: boolean;
  76. ImplementsReplacedItems: boolean;
  77. procedure ResolveDefs; override;
  78. end;
  79. TRecordDef = class(TDef)
  80. public
  81. Size: integer;
  82. end;
  83. TBasicType = (btVoid, btByte, btShortInt, btWord, btSmallInt, btLongWord, btLongInt, btInt64,
  84. btSingle, btDouble, btString, btWideString, btBoolean, btChar, btWideChar, btEnum, btPointer,
  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. { TReplDef }
  94. TReplDef = class(TDef)
  95. protected
  96. procedure SetIsUsed(const AValue: boolean); override;
  97. public
  98. IsReplaced: boolean;
  99. IsReplImpl: boolean;
  100. ReplacedItem: TReplDef;
  101. function CanReplaced: boolean; virtual;
  102. function IsReplacedBy(d: TReplDef): boolean; virtual;
  103. procedure CheckReplaced;
  104. end;
  105. TVarOption = (voRead, voWrite, voConst, voVar, voOut);
  106. TVarOptions = set of TVarOption;
  107. { TVarDef }
  108. TVarDef = class(TReplDef)
  109. private
  110. FHasTypeRef: boolean;
  111. protected
  112. procedure SetIsUsed(const AValue: boolean); override;
  113. public
  114. VarOpt: TVarOptions;
  115. VarType: TDef;
  116. constructor Create; override;
  117. procedure ResolveDefs; override;
  118. function IsReplacedBy(d: TReplDef): boolean; override;
  119. function CanReplaced: boolean; override;
  120. end;
  121. TProcType = (ptProcedure, ptFunction, ptConstructor, ptDestructor);
  122. TProcOption = (poOverride, poOverload, poMethodPtr, poPrivate, poProtected);
  123. TProcOptions = set of TProcOption;
  124. { TProcDef }
  125. TProcDef = class(TReplDef)
  126. private
  127. FHasRetTypeRef: boolean;
  128. protected
  129. procedure SetIsUsed(const AValue: boolean); override;
  130. public
  131. ProcType: TProcType;
  132. ReturnType: TDef;
  133. ProcOpt: TProcOptions;
  134. procedure ResolveDefs; override;
  135. function IsReplacedBy(d: TReplDef): boolean; override;
  136. function CanReplaced: boolean; override;
  137. end;
  138. TUnitDef = class(TDef)
  139. public
  140. OS: string;
  141. CPU: string;
  142. IntfCRC: string;
  143. PPUVer: integer;
  144. UsedUnits: array of TUnitDef;
  145. Processed: boolean;
  146. end;
  147. TConstDef = class(TVarDef)
  148. public
  149. Value: string;
  150. end;
  151. { TSetDef }
  152. TSetDef = class(TDef)
  153. private
  154. FHasElTypeRef: boolean;
  155. protected
  156. procedure SetIsUsed(const AValue: boolean); override;
  157. public
  158. Size: integer;
  159. Base: integer;
  160. ElMax: integer;
  161. ElType: TTypeDef;
  162. end;
  163. const
  164. ReplDefs = [dtField, dtProp, dtProc];
  165. implementation
  166. { TReplDef }
  167. procedure TReplDef.SetIsUsed(const AValue: boolean);
  168. var
  169. i: integer;
  170. begin
  171. i:=RefCnt;
  172. inherited SetIsUsed(AValue);
  173. if (i = 0) and (RefCnt > 0) then
  174. CheckReplaced;
  175. end;
  176. function TReplDef.CanReplaced: boolean;
  177. begin
  178. Result:=not (IsPrivate or (Parent = nil) or (Parent.DefType <> dtClass));
  179. end;
  180. function TReplDef.IsReplacedBy(d: TReplDef): boolean;
  181. begin
  182. Result:=d.CanReplaced and (CompareText(Name, d.Name) = 0);
  183. end;
  184. procedure TReplDef.CheckReplaced;
  185. function _Scan(cls: TClassDef): boolean;
  186. var
  187. i: integer;
  188. d: TReplDef;
  189. c: TClassDef;
  190. begin
  191. Result:=False;
  192. c:=cls.AncestorClass;
  193. if c = nil then
  194. exit;
  195. for i:=0 to c.Count - 1 do begin
  196. d:=TReplDef(c[i]);
  197. if (d.DefType in ReplDefs) and IsReplacedBy(d) then begin
  198. d.IsReplaced:=True;
  199. ReplacedItem:=d;
  200. Result:=True;
  201. break;
  202. end;
  203. end;
  204. if not Result then
  205. Result:=_Scan(c);
  206. if Result then begin
  207. cls.ImplementsReplacedItems:=True;
  208. c.HasReplacedItems:=True;
  209. end;
  210. end;
  211. begin
  212. if not CanReplaced then
  213. exit;
  214. if _Scan(TClassDef(Parent)) then
  215. IsReplImpl:=True;
  216. end;
  217. { TSetDef }
  218. procedure TSetDef.SetIsUsed(const AValue: boolean);
  219. begin
  220. inherited SetIsUsed(AValue);
  221. SetExtUsed(ElType, AValue, FHasElTypeRef);
  222. end;
  223. { TTypeDef }
  224. procedure TTypeDef.SetIsUsed(const AValue: boolean);
  225. begin
  226. if BasicType in [btEnum] then
  227. inherited SetIsUsed(AValue)
  228. else
  229. if AValue then
  230. AddRef
  231. else
  232. DecRef;
  233. end;
  234. { TProcDef }
  235. procedure TProcDef.SetIsUsed(const AValue: boolean);
  236. var
  237. i: integer;
  238. begin
  239. if IsPrivate then
  240. exit;
  241. if AValue and (RefCnt = 0) then begin
  242. for i:=0 to Count - 1 do
  243. if TVarDef(Items[i]).VarType = nil then
  244. exit; // If procedure has unsupported parameters, don't use it
  245. end;
  246. inherited SetIsUsed(AValue);
  247. if ReturnType <> Parent then
  248. SetExtUsed(ReturnType, AValue, FHasRetTypeRef);
  249. end;
  250. procedure TProcDef.ResolveDefs;
  251. begin
  252. inherited ResolveDefs;
  253. ReturnType:=ResolveDef(ReturnType);
  254. end;
  255. function TProcDef.IsReplacedBy(d: TReplDef): boolean;
  256. var
  257. i: integer;
  258. p: TProcDef;
  259. begin
  260. Result:=False;
  261. if d.DefType <> dtProc then
  262. exit;
  263. p:=TProcDef(d);
  264. if (ReturnType <> p.ReturnType) and (Count = p.Count) and inherited IsReplacedBy(p) then begin
  265. // Check parameter types
  266. for i:=0 to Count - 1 do
  267. if TVarDef(Items[i]).VarType <> TVarDef(p.Items[i]).VarType then
  268. exit;
  269. Result:=True;
  270. end;
  271. end;
  272. function TProcDef.CanReplaced: boolean;
  273. begin
  274. Result:=inherited CanReplaced and (ProcType = ptFunction);
  275. end;
  276. { TClassDef }
  277. procedure TClassDef.SetIsUsed(const AValue: boolean);
  278. begin
  279. inherited SetIsUsed(AValue);
  280. SetExtUsed(AncestorClass, AValue, FHasClassRef);
  281. end;
  282. procedure TClassDef.ResolveDefs;
  283. begin
  284. inherited ResolveDefs;
  285. AncestorClass:=TClassDef(ResolveDef(AncestorClass, TClassDef));
  286. end;
  287. { TVarDef }
  288. procedure TVarDef.SetIsUsed(const AValue: boolean);
  289. begin
  290. if IsPrivate then
  291. exit;
  292. inherited SetIsUsed(AValue);
  293. SetExtUsed(VarType, AValue, FHasTypeRef);
  294. end;
  295. procedure TVarDef.ResolveDefs;
  296. begin
  297. inherited ResolveDefs;
  298. VarType:=ResolveDef(VarType);
  299. end;
  300. function TVarDef.IsReplacedBy(d: TReplDef): boolean;
  301. begin
  302. Result:=(d.DefType in [dtProp, dtField]) and (VarType <> TVarDef(d).VarType) and inherited IsReplacedBy(d);
  303. end;
  304. function TVarDef.CanReplaced: boolean;
  305. begin
  306. Result:=(voRead in VarOpt) and inherited CanReplaced;
  307. end;
  308. constructor TVarDef.Create;
  309. begin
  310. inherited Create;
  311. VarOpt:=[voRead, voWrite];
  312. end;
  313. { TDef }
  314. procedure TDef.CheckItems;
  315. begin
  316. if FItems = nil then
  317. FItems:=TObjectList.Create(True);
  318. end;
  319. function TDef.GetAliasName: string;
  320. begin
  321. if FAliasName <> '' then
  322. Result:=FAliasName
  323. else
  324. Result:=Name;
  325. end;
  326. function TDef.GetCount: integer;
  327. begin
  328. if FItems = nil then
  329. Result:=0
  330. else begin
  331. CheckItems;
  332. Result:=FItems.Count;
  333. end;
  334. end;
  335. function TDef.GetIsUsed: boolean;
  336. begin
  337. Result:=FRefCnt > 0;
  338. end;
  339. function TDef.GetItem(Index: Integer): TDef;
  340. begin
  341. CheckItems;
  342. Result:=TDef(FItems[Index]);
  343. end;
  344. procedure TDef.SetIsUsed(const AValue: boolean);
  345. var
  346. i: integer;
  347. f: boolean;
  348. begin
  349. if FInSetUsed or (DefType = dtNone) or IsPrivate then
  350. exit;
  351. if AValue then begin
  352. AddRef;
  353. f:=FRefCnt = 1;
  354. end
  355. else begin
  356. if FRefCnt = 0 then
  357. exit;
  358. DecRef;
  359. f:=FRefCnt = 0;
  360. end;
  361. if f then begin
  362. // Update userd mark of children only once
  363. FInSetUsed:=True;
  364. try
  365. for i:=0 to Count - 1 do
  366. Items[i].IsUsed:=AValue;
  367. finally
  368. FInSetUsed:=False;
  369. end;
  370. // Update parent's used mark
  371. if (Parent <> nil) and (Parent.DefType = dtUnit) then
  372. if AValue then
  373. Parent.AddRef
  374. else
  375. Parent.DecRef;
  376. end;
  377. end;
  378. function TDef.ResolveDef(d: TDef; ExpectedClass: TDefClass): TDef;
  379. begin
  380. if (d = nil) or (d.DefType <> dtNone) then begin
  381. Result:=d;
  382. exit;
  383. end;
  384. Result:=d.Parent.FindDef(d.DefId);
  385. if (ExpectedClass <> nil) and (Result <> nil) then
  386. if not (Result is ExpectedClass) then
  387. raise Exception.CreateFmt('Unexpected class. Expected: %s, got: %s', [ExpectedClass.ClassName, Result.ClassName]);
  388. end;
  389. procedure TDef.AddRef;
  390. begin
  391. Inc(FRefCnt);
  392. end;
  393. procedure TDef.DecRef;
  394. begin
  395. if FRefCnt > 0 then
  396. Dec(FRefCnt);
  397. end;
  398. procedure TDef.SetExtUsed(ExtDef: TDef; AUsed: boolean; var HasRef: boolean);
  399. var
  400. OldRefCnt: integer;
  401. begin
  402. if ExtDef = nil then
  403. exit;
  404. if AUsed then begin
  405. if HasRef then
  406. exit;
  407. OldRefCnt:=ExtDef.RefCnt;
  408. ExtDef.IsUsed:=True;
  409. HasRef:=OldRefCnt <> ExtDef.RefCnt;
  410. end
  411. else
  412. if HasRef and not IsUsed then begin
  413. ExtDef.IsUsed:=False;
  414. HasRef:=False;
  415. end;
  416. end;
  417. procedure TDef.SetItem(Index: Integer; const AValue: TDef);
  418. begin
  419. CheckItems;
  420. FItems[Index]:=AValue;
  421. end;
  422. constructor TDef.Create;
  423. begin
  424. DefId:=-1;
  425. DefType:=dtNone;
  426. end;
  427. constructor TDef.Create(AParent: TDef; AType: TDefType);
  428. begin
  429. Create;
  430. if AParent <> nil then
  431. AParent.Add(Self);
  432. DefType:=AType;
  433. end;
  434. destructor TDef.Destroy;
  435. begin
  436. FreeAndNil(FItems);
  437. if (Parent <> nil) and (Parent.FItems <> nil) then begin
  438. Parent.FItems.OwnsObjects:=False;
  439. try
  440. Parent.FItems.Remove(Self);
  441. finally
  442. Parent.FItems.OwnsObjects:=True;
  443. end;
  444. end;
  445. inherited Destroy;
  446. end;
  447. function TDef.Add(ADef: TDef): integer;
  448. begin
  449. Result:=Insert(Count, ADef);
  450. end;
  451. function TDef.Insert(Index: integer; ADef: TDef): integer;
  452. begin
  453. CheckItems;
  454. Result:=Index;
  455. FItems.Insert(Result, ADef);
  456. ADef.Parent:=Self;
  457. end;
  458. function TDef.FindDef(ADefId: integer; Recursive: boolean): TDef;
  459. function _Find(d: TDef): TDef;
  460. var
  461. i: integer;
  462. begin
  463. Result:=nil;
  464. for i:=0 to d.Count - 1 do
  465. with d[i] do begin
  466. if (DefType <> dtNone) and (DefId = ADefId) then begin
  467. Result:=d[i];
  468. break;
  469. end;
  470. if Recursive and (Count > 0) then begin
  471. Result:=_Find(d[i]);
  472. if Result <> nil then
  473. break;
  474. end;
  475. end;
  476. end;
  477. begin
  478. Result:=_Find(Self);
  479. end;
  480. procedure TDef.ResolveDefs;
  481. var
  482. i: integer;
  483. begin
  484. for i:=0 to Count - 1 do
  485. Items[i].ResolveDefs;
  486. end;
  487. procedure TDef.SetNotUsed;
  488. begin
  489. if FRefCnt = 0 then
  490. exit;
  491. FRefCnt:=1;
  492. IsUsed:=False;
  493. end;
  494. end.