def.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. function IsSameType(t1, t2: TDef): boolean;
  167. begin
  168. Result:=t1 = t2;
  169. if Result then
  170. exit;
  171. if (t1 = nil) or (t2 = nil) or (t1.DefType <> t2.DefType) then
  172. exit;
  173. if t1.DefType <> dtType then
  174. exit;
  175. Result:=TTypeDef(t1).BasicType = TTypeDef(t2).BasicType;
  176. end;
  177. { TReplDef }
  178. procedure TReplDef.SetIsUsed(const AValue: boolean);
  179. var
  180. i: integer;
  181. begin
  182. i:=RefCnt;
  183. inherited SetIsUsed(AValue);
  184. if (i = 0) and (RefCnt > 0) then
  185. CheckReplaced;
  186. end;
  187. function TReplDef.CanReplaced: boolean;
  188. begin
  189. Result:=not (IsPrivate or (Parent = nil) or (Parent.DefType <> dtClass));
  190. end;
  191. function TReplDef.IsReplacedBy(d: TReplDef): boolean;
  192. begin
  193. Result:=d.CanReplaced and (CompareText(Name, d.Name) = 0);
  194. end;
  195. procedure TReplDef.CheckReplaced;
  196. function _Scan(cls: TClassDef): boolean;
  197. var
  198. i: integer;
  199. d: TReplDef;
  200. c: TClassDef;
  201. begin
  202. Result:=False;
  203. c:=cls.AncestorClass;
  204. if c = nil then
  205. exit;
  206. for i:=0 to c.Count - 1 do begin
  207. d:=TReplDef(c[i]);
  208. if (d.DefType in ReplDefs) and IsReplacedBy(d) then begin
  209. d.IsReplaced:=True;
  210. ReplacedItem:=d;
  211. Result:=True;
  212. break;
  213. end;
  214. end;
  215. if not Result then
  216. Result:=_Scan(c);
  217. if Result then begin
  218. cls.ImplementsReplacedItems:=True;
  219. c.HasReplacedItems:=True;
  220. end;
  221. end;
  222. begin
  223. if not CanReplaced then
  224. exit;
  225. if _Scan(TClassDef(Parent)) then
  226. IsReplImpl:=True;
  227. end;
  228. { TSetDef }
  229. procedure TSetDef.SetIsUsed(const AValue: boolean);
  230. begin
  231. inherited SetIsUsed(AValue);
  232. SetExtUsed(ElType, AValue, FHasElTypeRef);
  233. end;
  234. { TTypeDef }
  235. procedure TTypeDef.SetIsUsed(const AValue: boolean);
  236. begin
  237. if BasicType in [btEnum] then
  238. inherited SetIsUsed(AValue)
  239. else
  240. if AValue then
  241. AddRef
  242. else
  243. DecRef;
  244. end;
  245. { TProcDef }
  246. procedure TProcDef.SetIsUsed(const AValue: boolean);
  247. var
  248. i: integer;
  249. begin
  250. if IsPrivate then
  251. exit;
  252. if AValue and (RefCnt = 0) then begin
  253. for i:=0 to Count - 1 do
  254. if TVarDef(Items[i]).VarType = nil then
  255. exit; // If procedure has unsupported parameters, don't use it
  256. end;
  257. inherited SetIsUsed(AValue);
  258. if ReturnType <> Parent then
  259. SetExtUsed(ReturnType, AValue, FHasRetTypeRef);
  260. end;
  261. procedure TProcDef.ResolveDefs;
  262. begin
  263. inherited ResolveDefs;
  264. ReturnType:=ResolveDef(ReturnType);
  265. end;
  266. function TProcDef.IsReplacedBy(d: TReplDef): boolean;
  267. var
  268. i: integer;
  269. p: TProcDef;
  270. begin
  271. Result:=False;
  272. if d.DefType <> dtProc then
  273. exit;
  274. p:=TProcDef(d);
  275. if (Count = p.Count) and inherited IsReplacedBy(p) then begin
  276. // Check parameter types
  277. for i:=0 to Count - 1 do
  278. if not IsSameType(TVarDef(Items[i]).VarType, TVarDef(p.Items[i]).VarType) then
  279. exit;
  280. Result:=True;
  281. end;
  282. end;
  283. function TProcDef.CanReplaced: boolean;
  284. begin
  285. Result:=inherited CanReplaced and (ProcType = ptFunction);
  286. end;
  287. { TClassDef }
  288. procedure TClassDef.SetIsUsed(const AValue: boolean);
  289. begin
  290. inherited SetIsUsed(AValue);
  291. SetExtUsed(AncestorClass, AValue, FHasClassRef);
  292. end;
  293. procedure TClassDef.ResolveDefs;
  294. begin
  295. inherited ResolveDefs;
  296. AncestorClass:=TClassDef(ResolveDef(AncestorClass, TClassDef));
  297. end;
  298. { TVarDef }
  299. procedure TVarDef.SetIsUsed(const AValue: boolean);
  300. begin
  301. if IsPrivate then
  302. exit;
  303. inherited SetIsUsed(AValue);
  304. SetExtUsed(VarType, AValue, FHasTypeRef);
  305. end;
  306. procedure TVarDef.ResolveDefs;
  307. begin
  308. inherited ResolveDefs;
  309. VarType:=ResolveDef(VarType);
  310. end;
  311. function TVarDef.IsReplacedBy(d: TReplDef): boolean;
  312. begin
  313. Result:=(d.DefType in [dtProp, dtField]) and not IsSameType(VarType, TVarDef(d).VarType) and inherited IsReplacedBy(d);
  314. end;
  315. function TVarDef.CanReplaced: boolean;
  316. begin
  317. Result:=(voRead in VarOpt) and inherited CanReplaced;
  318. end;
  319. constructor TVarDef.Create;
  320. begin
  321. inherited Create;
  322. VarOpt:=[voRead, voWrite];
  323. end;
  324. { TDef }
  325. procedure TDef.CheckItems;
  326. begin
  327. if FItems = nil then
  328. FItems:=TObjectList.Create(True);
  329. end;
  330. function TDef.GetAliasName: string;
  331. begin
  332. if FAliasName <> '' then
  333. Result:=FAliasName
  334. else
  335. Result:=Name;
  336. end;
  337. function TDef.GetCount: integer;
  338. begin
  339. if FItems = nil then
  340. Result:=0
  341. else begin
  342. CheckItems;
  343. Result:=FItems.Count;
  344. end;
  345. end;
  346. function TDef.GetIsUsed: boolean;
  347. begin
  348. Result:=FRefCnt > 0;
  349. end;
  350. function TDef.GetItem(Index: Integer): TDef;
  351. begin
  352. CheckItems;
  353. Result:=TDef(FItems[Index]);
  354. end;
  355. procedure TDef.SetIsUsed(const AValue: boolean);
  356. var
  357. i: integer;
  358. f: boolean;
  359. begin
  360. if FInSetUsed or (DefType = dtNone) or IsPrivate then
  361. exit;
  362. if AValue then begin
  363. AddRef;
  364. f:=FRefCnt = 1;
  365. end
  366. else begin
  367. if FRefCnt = 0 then
  368. exit;
  369. DecRef;
  370. f:=FRefCnt = 0;
  371. end;
  372. if f then begin
  373. // Update userd mark of children only once
  374. FInSetUsed:=True;
  375. try
  376. for i:=0 to Count - 1 do
  377. Items[i].IsUsed:=AValue;
  378. finally
  379. FInSetUsed:=False;
  380. end;
  381. // Update parent's used mark
  382. if (Parent <> nil) and (Parent.DefType = dtUnit) then
  383. if AValue then
  384. Parent.AddRef
  385. else
  386. Parent.DecRef;
  387. end;
  388. end;
  389. function TDef.ResolveDef(d: TDef; ExpectedClass: TDefClass): TDef;
  390. begin
  391. if (d = nil) or (d.DefType <> dtNone) then begin
  392. Result:=d;
  393. exit;
  394. end;
  395. Result:=d.Parent.FindDef(d.DefId);
  396. if (ExpectedClass <> nil) and (Result <> nil) then
  397. if not (Result is ExpectedClass) then
  398. raise Exception.CreateFmt('Unexpected class. Expected: %s, got: %s', [ExpectedClass.ClassName, Result.ClassName]);
  399. end;
  400. procedure TDef.AddRef;
  401. begin
  402. Inc(FRefCnt);
  403. end;
  404. procedure TDef.DecRef;
  405. begin
  406. if FRefCnt > 0 then
  407. Dec(FRefCnt);
  408. end;
  409. procedure TDef.SetExtUsed(ExtDef: TDef; AUsed: boolean; var HasRef: boolean);
  410. var
  411. OldRefCnt: integer;
  412. begin
  413. if ExtDef = nil then
  414. exit;
  415. if AUsed then begin
  416. if HasRef then
  417. exit;
  418. OldRefCnt:=ExtDef.RefCnt;
  419. ExtDef.IsUsed:=True;
  420. HasRef:=OldRefCnt <> ExtDef.RefCnt;
  421. end
  422. else
  423. if HasRef and not IsUsed then begin
  424. ExtDef.IsUsed:=False;
  425. HasRef:=False;
  426. end;
  427. end;
  428. procedure TDef.SetItem(Index: Integer; const AValue: TDef);
  429. begin
  430. CheckItems;
  431. FItems[Index]:=AValue;
  432. end;
  433. constructor TDef.Create;
  434. begin
  435. DefId:=-1;
  436. DefType:=dtNone;
  437. end;
  438. constructor TDef.Create(AParent: TDef; AType: TDefType);
  439. begin
  440. Create;
  441. if AParent <> nil then
  442. AParent.Add(Self);
  443. DefType:=AType;
  444. end;
  445. destructor TDef.Destroy;
  446. begin
  447. FreeAndNil(FItems);
  448. if (Parent <> nil) and (Parent.FItems <> nil) then begin
  449. Parent.FItems.OwnsObjects:=False;
  450. try
  451. Parent.FItems.Remove(Self);
  452. finally
  453. Parent.FItems.OwnsObjects:=True;
  454. end;
  455. end;
  456. inherited Destroy;
  457. end;
  458. function TDef.Add(ADef: TDef): integer;
  459. begin
  460. Result:=Insert(Count, ADef);
  461. end;
  462. function TDef.Insert(Index: integer; ADef: TDef): integer;
  463. begin
  464. CheckItems;
  465. Result:=Index;
  466. FItems.Insert(Result, ADef);
  467. ADef.Parent:=Self;
  468. end;
  469. function TDef.FindDef(ADefId: integer; Recursive: boolean): TDef;
  470. function _Find(d: TDef): TDef;
  471. var
  472. i: integer;
  473. begin
  474. Result:=nil;
  475. for i:=0 to d.Count - 1 do
  476. with d[i] do begin
  477. if (DefType <> dtNone) and (DefId = ADefId) then begin
  478. Result:=d[i];
  479. break;
  480. end;
  481. if Recursive and (Count > 0) then begin
  482. Result:=_Find(d[i]);
  483. if Result <> nil then
  484. break;
  485. end;
  486. end;
  487. end;
  488. begin
  489. Result:=_Find(Self);
  490. end;
  491. procedure TDef.ResolveDefs;
  492. var
  493. i: integer;
  494. begin
  495. for i:=0 to Count - 1 do
  496. Items[i].ResolveDefs;
  497. end;
  498. procedure TDef.SetNotUsed;
  499. begin
  500. if FRefCnt = 0 then
  501. exit;
  502. FRefCnt:=1;
  503. IsUsed:=False;
  504. end;
  505. end.