def.pas 12 KB

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