tcgenerics.pas 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457
  1. unit TCGenerics;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry,
  6. TCModules, FPPas2Js, PScanner, PasResolveEval;
  7. type
  8. { TTestGenerics }
  9. TTestGenerics = class(TCustomTestModule)
  10. Published
  11. // generic record
  12. Procedure TestGen_RecordEmpty;
  13. Procedure TestGen_Record_ClassProc;
  14. Procedure TestGen_Record_ClassVarRecord_Program;
  15. Procedure TestGen_Record_ClassVarRecord_UnitImpl;
  16. Procedure TestGen_Record_RTTI_UnitImpl;
  17. // ToDo: delay RTTI with anonymous array a:array of T, array[1..2] of T
  18. // ToDo: type alias type as parameter, TBird = type word;
  19. // generic class
  20. Procedure TestGen_ClassEmpty;
  21. Procedure TestGen_Class_EmptyMethod;
  22. Procedure TestGen_Class_TList;
  23. Procedure TestGen_Class_TCustomList; // ToDo: with Self do Result:=Method()
  24. Procedure TestGen_ClassAncestor;
  25. Procedure TestGen_Class_TypeInfo;
  26. Procedure TestGen_Class_TypeOverload; // ToDo TBird, TBird<T>, TBird<S,T>
  27. Procedure TestGen_Class_ClassProperty;
  28. Procedure TestGen_Class_ClassProc;
  29. //Procedure TestGen_Record_ReferGenClass_DelphiFail; TBird<T> = class x:TBird; end;
  30. Procedure TestGen_Class_ClassConstructor;
  31. Procedure TestGen_Class_TypeCastSpecializesWarn;
  32. Procedure TestGen_Class_TypeCastSpecializesJSValueNoWarn;
  33. procedure TestGen_Class_OverloadsInUnit;
  34. procedure TestGen_ClassForward_CircleRTTI;
  35. procedure TestGen_Class_Nested_RTTI;
  36. Procedure TestGen_Class_ClassVarRecord_UnitImpl;
  37. // generic external class
  38. procedure TestGen_ExtClass_VarArgsOfType;
  39. procedure TestGen_ExtClass_Array;
  40. procedure TestGen_ExtClass_GenJSValueAssign;
  41. procedure TestGen_ExtClass_AliasMemberType;
  42. Procedure TestGen_ExtClass_RTTI;
  43. procedure TestGen_ExtClass_UnitImplRec;
  44. // class interfaces
  45. procedure TestGen_ClassInterface_Corba;
  46. procedure TestGen_ClassInterface_InterfacedObject;
  47. procedure TestGen_ClassInterface_COM_RTTI;
  48. procedure TestGen_ClassInterface_Helper;
  49. // statements
  50. Procedure TestGen_InlineSpec_Constructor;
  51. Procedure TestGen_CallUnitImplProc;
  52. Procedure TestGen_IntAssignTemplVar;
  53. Procedure TestGen_TypeCastDotField;
  54. // generic helper
  55. procedure TestGen_HelperForArray;
  56. // generic functions
  57. procedure TestGenProc_Function_ObjFPC;
  58. procedure TestGenProc_Function_Delphi;
  59. procedure TestGenProc_Overload;
  60. procedure TestGenProc_Forward;
  61. procedure TestGenProc_Infer_OverloadForward;
  62. procedure TestGenProc_TypeInfo;
  63. procedure TestGenProc_Infer_Widen;
  64. procedure TestGenProc_Infer_PassAsArg;
  65. // ToDo: FuncName:= instead of Result:=
  66. // generic methods
  67. procedure TestGenMethod_ObjFPC;
  68. // generic array
  69. procedure TestGen_Array_OtherUnit;
  70. procedure TestGen_ArrayOfUnitImplRec;
  71. // generic procedure type
  72. procedure TestGen_ProcType_ProcLocal;
  73. procedure TestGen_ProcType_Local_RTTI_Fail;
  74. procedure TestGen_ProcType_ParamUnitImpl;
  75. end;
  76. implementation
  77. { TTestGenerics }
  78. procedure TTestGenerics.TestGen_RecordEmpty;
  79. begin
  80. StartProgram(false);
  81. Add([
  82. 'type',
  83. ' generic TRecA<T> = record',
  84. ' end;',
  85. 'var a,b: specialize TRecA<word>;',
  86. 'begin',
  87. ' if a=b then ;']);
  88. ConvertProgram;
  89. CheckSource('TestGen_RecordEmpty',
  90. LinesToStr([ // statements
  91. 'rtl.recNewT(this, "TRecA$G1", function () {',
  92. ' this.$eq = function (b) {',
  93. ' return true;',
  94. ' };',
  95. ' this.$assign = function (s) {',
  96. ' return this;',
  97. ' };',
  98. '});',
  99. 'this.a = this.TRecA$G1.$new();',
  100. 'this.b = this.TRecA$G1.$new();',
  101. '']),
  102. LinesToStr([ // $mod.$main
  103. 'if ($mod.a.$eq($mod.b)) ;'
  104. ]));
  105. end;
  106. procedure TTestGenerics.TestGen_Record_ClassProc;
  107. begin
  108. StartProgram(false);
  109. Add([
  110. '{$modeswitch AdvancedRecords}',
  111. 'type',
  112. ' generic TPoint<T> = record',
  113. ' class var x: T;',
  114. ' class procedure Fly; static;',
  115. ' end;',
  116. 'class procedure Tpoint.Fly;',
  117. 'begin',
  118. ' x:=x+3;',
  119. ' tpoint.x:=tpoint.x+4;',
  120. ' Fly;',
  121. ' tpoint.Fly;',
  122. 'end;',
  123. 'var p: specialize TPoint<word>;',
  124. 'begin',
  125. ' p.x:=p.x+10;',
  126. ' p.Fly;',
  127. ' p.Fly();',
  128. '']);
  129. ConvertProgram;
  130. CheckSource('TestGen_Record_ClassProc',
  131. LinesToStr([ // statements
  132. 'rtl.recNewT(this, "TPoint$G1", function () {',
  133. ' this.x = 0;',
  134. ' this.$eq = function (b) {',
  135. ' return true;',
  136. ' };',
  137. ' this.$assign = function (s) {',
  138. ' return this;',
  139. ' };',
  140. ' this.Fly = function () {',
  141. ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 3;',
  142. ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 4;',
  143. ' $mod.TPoint$G1.Fly();',
  144. ' $mod.TPoint$G1.Fly();',
  145. ' };',
  146. '}, true);',
  147. 'this.p = this.TPoint$G1.$new();',
  148. '']),
  149. LinesToStr([ // $mod.$main
  150. '$mod.TPoint$G1.x = $mod.p.x + 10;',
  151. '$mod.p.Fly();',
  152. '$mod.p.Fly();',
  153. '']));
  154. end;
  155. procedure TTestGenerics.TestGen_Record_ClassVarRecord_Program;
  156. begin
  157. StartProgram(false);
  158. Add([
  159. '{$modeswitch AdvancedRecords}',
  160. 'type',
  161. ' generic TAnt<T> = record',
  162. ' class var x: T;',
  163. ' end;',
  164. ' TBird = record',
  165. ' b: word;',
  166. ' end;',
  167. 'var f: specialize TAnt<TBird>;',
  168. 'begin',
  169. ' f.x.b:=f.x.b+10;',
  170. '']);
  171. ConvertProgram;
  172. CheckSource('TestGen_Record_ClassVarRecord_Program',
  173. LinesToStr([ // statements
  174. 'rtl.recNewT(this, "TBird", function () {',
  175. ' this.b = 0;',
  176. ' this.$eq = function (b) {',
  177. ' return this.b === b.b;',
  178. ' };',
  179. ' this.$assign = function (s) {',
  180. ' this.b = s.b;',
  181. ' return this;',
  182. ' };',
  183. '});',
  184. 'rtl.recNewT(this, "TAnt$G1", function () {',
  185. ' this.x = $mod.TBird.$new();',
  186. ' this.$eq = function (b) {',
  187. ' return true;',
  188. ' };',
  189. ' this.$assign = function (s) {',
  190. ' return this;',
  191. ' };',
  192. '}, true);',
  193. 'this.f = this.TAnt$G1.$new();',
  194. '']),
  195. LinesToStr([ // $mod.$main
  196. '$mod.f.x.b = $mod.f.x.b + 10;',
  197. '']));
  198. end;
  199. procedure TTestGenerics.TestGen_Record_ClassVarRecord_UnitImpl;
  200. begin
  201. StartProgram(true,[supTObject]);
  202. AddModuleWithIntfImplSrc('UnitA.pas',
  203. LinesToStr([
  204. '{$modeswitch AdvancedRecords}',
  205. 'type',
  206. ' generic TAnt<T> = record',
  207. ' class var x: T;',
  208. ' class var a: array[1..2] of T;',
  209. ' end;',
  210. '']),
  211. LinesToStr([
  212. 'type',
  213. ' TBird = record',
  214. ' b: word;',
  215. ' end;',
  216. 'var f: specialize TAnt<TBird>;',
  217. 'begin',
  218. ' f.x.b:=f.x.b+10;',
  219. '']));
  220. Add([
  221. 'uses UnitA;',
  222. 'begin',
  223. 'end.']);
  224. ConvertProgram;
  225. CheckUnit('UnitA.pas',
  226. LinesToStr([ // statements
  227. 'rtl.module("UnitA", ["system"], function () {',
  228. ' var $mod = this;',
  229. ' var $impl = $mod.$impl;',
  230. ' rtl.recNewT(this, "TAnt$G1", function () {',
  231. ' this.$initSpec = function () {',
  232. ' this.x = $impl.TBird.$new();',
  233. ' this.a = rtl.arraySetLength(null, $impl.TBird, 2);',
  234. ' };',
  235. ' this.$eq = function (b) {',
  236. ' return true;',
  237. ' };',
  238. ' this.$assign = function (s) {',
  239. ' return this;',
  240. ' };',
  241. ' }, true);',
  242. ' $mod.$implcode = function () {',
  243. ' rtl.recNewT($impl, "TBird", function () {',
  244. ' this.b = 0;',
  245. ' this.$eq = function (b) {',
  246. ' return this.b === b.b;',
  247. ' };',
  248. ' this.$assign = function (s) {',
  249. ' this.b = s.b;',
  250. ' return this;',
  251. ' };',
  252. ' });',
  253. ' $impl.f = $mod.TAnt$G1.$new();',
  254. ' };',
  255. ' $mod.$init = function () {',
  256. ' $impl.f.x.b = $impl.f.x.b + 10;',
  257. ' };',
  258. '}, []);']));
  259. CheckSource('TestGen_Record_ClassVarRecord_UnitImpl',
  260. LinesToStr([ // statements
  261. 'pas.UnitA.TAnt$G1.$initSpec();',
  262. '']),
  263. LinesToStr([ // $mod.$main
  264. '']));
  265. end;
  266. procedure TTestGenerics.TestGen_Record_RTTI_UnitImpl;
  267. begin
  268. WithTypeInfo:=true;
  269. StartUnit(true);
  270. Add([
  271. 'interface',
  272. '{$modeswitch AdvancedRecords}',
  273. 'type',
  274. ' generic TAnt<T> = record',
  275. ' class var x: T;',
  276. //' class var a,b: array of T;',
  277. ' end;',
  278. 'implementation',
  279. 'type',
  280. ' TBird = record',
  281. ' b: word;',
  282. ' end;',
  283. 'var f: specialize TAnt<TBird>;',
  284. ' p: pointer;',
  285. 'begin',
  286. ' p:=typeinfo(f);',
  287. '']);
  288. ConvertUnit;
  289. CheckSource('TestGen_Record_RTTI_UnitImpl',
  290. LinesToStr([ // statements
  291. 'var $impl = $mod.$impl;',
  292. 'rtl.recNewT(this, "TAnt$G1", function () {',
  293. ' var $r = $mod.$rtti.$Record("TAnt<Test1.TBird>", {});',
  294. ' this.$initSpec = function () {',
  295. ' this.x = $impl.TBird.$new();',
  296. ' $r.addField("x", $mod.$rtti["TBird"]);',
  297. ' };',
  298. ' this.$eq = function (b) {',
  299. ' return true;',
  300. ' };',
  301. ' this.$assign = function (s) {',
  302. ' return this;',
  303. ' };',
  304. '}, true);',
  305. '']),
  306. LinesToStr([ // $mod.$init
  307. '$impl.p = $mod.$rtti["TAnt<Test1.TBird>"];',
  308. '']),
  309. LinesToStr([ // statements
  310. 'rtl.recNewT($impl, "TBird", function () {',
  311. ' this.b = 0;',
  312. ' this.$eq = function (b) {',
  313. ' return this.b === b.b;',
  314. ' };',
  315. ' this.$assign = function (s) {',
  316. ' this.b = s.b;',
  317. ' return this;',
  318. ' };',
  319. ' var $r = $mod.$rtti.$Record("TBird", {});',
  320. ' $r.addField("b", rtl.word);',
  321. '});',
  322. '$impl.f = $mod.TAnt$G1.$new();',
  323. '$impl.p = null;',
  324. '']));
  325. end;
  326. procedure TTestGenerics.TestGen_ClassEmpty;
  327. begin
  328. StartProgram(false);
  329. Add([
  330. 'type',
  331. ' TObject = class end;',
  332. ' generic TBird<T> = class',
  333. ' end;',
  334. 'var a,b: specialize TBird<word>;',
  335. 'begin',
  336. ' if a=b then ;']);
  337. ConvertProgram;
  338. CheckSource('TestGen_ClassEmpty',
  339. LinesToStr([ // statements
  340. 'rtl.createClass(this, "TObject", null, function () {',
  341. ' this.$init = function () {',
  342. ' };',
  343. ' this.$final = function () {',
  344. ' };',
  345. '});',
  346. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  347. '}, "TBird<System.Word>");',
  348. 'this.a = null;',
  349. 'this.b = null;',
  350. '']),
  351. LinesToStr([ // $mod.$main
  352. 'if ($mod.a === $mod.b) ;'
  353. ]));
  354. end;
  355. procedure TTestGenerics.TestGen_Class_EmptyMethod;
  356. begin
  357. StartProgram(false);
  358. Add([
  359. 'type',
  360. ' TObject = class end;',
  361. ' generic TBird<T> = class',
  362. ' function Fly(w: T): T;',
  363. ' end;',
  364. 'function TBird.Fly(w: T): T;',
  365. 'begin',
  366. 'end;',
  367. 'var a: specialize TBird<word>;',
  368. 'begin',
  369. ' if a.Fly(3)=4 then ;']);
  370. ConvertProgram;
  371. CheckSource('TestGen_Class_EmptyMethod',
  372. LinesToStr([ // statements
  373. 'rtl.createClass(this, "TObject", null, function () {',
  374. ' this.$init = function () {',
  375. ' };',
  376. ' this.$final = function () {',
  377. ' };',
  378. '});',
  379. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  380. ' this.Fly = function (w) {',
  381. ' var Result = 0;',
  382. ' return Result;',
  383. ' };',
  384. '}, "TBird<System.Word>");',
  385. 'this.a = null;',
  386. '']),
  387. LinesToStr([ // $mod.$main
  388. ' if ($mod.a.Fly(3) === 4) ;'
  389. ]));
  390. end;
  391. procedure TTestGenerics.TestGen_Class_TList;
  392. begin
  393. StartProgram(false);
  394. Add([
  395. '{$mode objfpc}',
  396. 'type',
  397. ' TObject = class end;',
  398. ' generic TList<T> = class',
  399. ' strict private',
  400. ' FItems: array of T;',
  401. ' function GetItems(Index: longint): T;',
  402. ' procedure SetItems(Index: longint; Value: T);',
  403. ' public',
  404. ' procedure Alter(w: T);',
  405. ' property Items[Index: longint]: T read GetItems write SetItems; default;',
  406. ' end;',
  407. ' TWordList = specialize TList<word>;',
  408. 'function TList.GetItems(Index: longint): T;',
  409. 'begin',
  410. ' Result:=FItems[Index];',
  411. 'end;',
  412. 'procedure TList.SetItems(Index: longint; Value: T);',
  413. 'begin',
  414. ' FItems[Index]:=Value;',
  415. 'end;',
  416. 'procedure TList.Alter(w: T);',
  417. 'begin',
  418. ' SetLength(FItems,length(FItems)+1);',
  419. ' Insert(w,FItems,2);',
  420. ' Delete(FItems,2,3);',
  421. 'end;',
  422. 'var l: TWordList;',
  423. ' w: word;',
  424. 'begin',
  425. ' l[1]:=w;',
  426. ' w:=l[2];',
  427. '']);
  428. ConvertProgram;
  429. CheckSource('TestGen_Class_TList',
  430. LinesToStr([ // statements
  431. 'rtl.createClass(this, "TObject", null, function () {',
  432. ' this.$init = function () {',
  433. ' };',
  434. ' this.$final = function () {',
  435. ' };',
  436. '});',
  437. 'rtl.createClass(this, "TList$G1", this.TObject, function () {',
  438. ' this.$init = function () {',
  439. ' $mod.TObject.$init.call(this);',
  440. ' this.FItems = [];',
  441. ' };',
  442. ' this.$final = function () {',
  443. ' this.FItems = undefined;',
  444. ' $mod.TObject.$final.call(this);',
  445. ' };',
  446. ' this.GetItems = function (Index) {',
  447. ' var Result = 0;',
  448. ' Result = this.FItems[Index];',
  449. ' return Result;',
  450. ' };',
  451. ' this.SetItems = function (Index, Value) {',
  452. ' this.FItems[Index] = Value;',
  453. ' };',
  454. ' this.Alter = function (w) {',
  455. ' this.FItems = rtl.arraySetLength(this.FItems, 0, rtl.length(this.FItems) + 1);',
  456. ' this.FItems.splice(2, 0, w);',
  457. ' this.FItems.splice(2, 3);',
  458. ' };',
  459. '}, "TList<System.Word>");',
  460. 'this.l = null;',
  461. 'this.w = 0;',
  462. '']),
  463. LinesToStr([ // $mod.$main
  464. '$mod.l.SetItems(1, $mod.w);',
  465. '$mod.w = $mod.l.GetItems(2);',
  466. '']));
  467. end;
  468. procedure TTestGenerics.TestGen_Class_TCustomList;
  469. begin
  470. StartProgram(false);
  471. Add([
  472. '{$mode delphi}',
  473. 'type',
  474. ' TObject = class end;',
  475. ' TCustomList<T> = class',
  476. ' public',
  477. ' function PrepareAddingItem: word; virtual;',
  478. ' end;',
  479. ' TList<T> = class(TCustomList<T>)',
  480. ' public',
  481. ' function Add: word;',
  482. ' end;',
  483. ' TWordList = TList<word>;',
  484. 'function TCustomList<T>.PrepareAddingItem: word;',
  485. 'begin',
  486. 'end;',
  487. 'function TList<T>.Add: word;',
  488. 'begin',
  489. ' Result:=PrepareAddingItem;',
  490. ' Result:=Self.PrepareAddingItem;',
  491. //' with Self do Result:=PrepareAddingItem;',
  492. 'end;',
  493. 'var l: TWordList;',
  494. 'begin',
  495. '']);
  496. ConvertProgram;
  497. CheckSource('TestGen_Class_TCustomList',
  498. LinesToStr([ // statements
  499. 'rtl.createClass(this, "TObject", null, function () {',
  500. ' this.$init = function () {',
  501. ' };',
  502. ' this.$final = function () {',
  503. ' };',
  504. '});',
  505. 'rtl.createClass(this, "TCustomList$G2", this.TObject, function () {',
  506. ' this.PrepareAddingItem = function () {',
  507. ' var Result = 0;',
  508. ' return Result;',
  509. ' };',
  510. '}, "TCustomList<System.Word>");',
  511. 'rtl.createClass(this, "TList$G1", this.TCustomList$G2, function () {',
  512. ' this.Add = function () {',
  513. ' var Result = 0;',
  514. ' Result = this.PrepareAddingItem();',
  515. ' Result = this.PrepareAddingItem();',
  516. ' return Result;',
  517. ' };',
  518. '}, "TList<System.Word>");',
  519. 'this.l = null;',
  520. '']),
  521. LinesToStr([ // $mod.$main
  522. '']));
  523. end;
  524. procedure TTestGenerics.TestGen_ClassAncestor;
  525. begin
  526. StartProgram(false);
  527. Add([
  528. 'type',
  529. ' TObject = class end;',
  530. ' generic TBird<T> = class',
  531. ' end;',
  532. ' generic TEagle<T> = class(specialize TBird<T>)',
  533. ' end;',
  534. 'var a: specialize TEagle<word>;',
  535. 'begin',
  536. '']);
  537. ConvertProgram;
  538. CheckSource('TestGen_ClassAncestor',
  539. LinesToStr([ // statements
  540. 'rtl.createClass(this, "TObject", null, function () {',
  541. ' this.$init = function () {',
  542. ' };',
  543. ' this.$final = function () {',
  544. ' };',
  545. '});',
  546. 'rtl.createClass(this, "TBird$G2", this.TObject, function () {',
  547. '}, "TBird<System.Word>");',
  548. 'rtl.createClass(this, "TEagle$G1", this.TBird$G2, function () {',
  549. '}, "TEagle<System.Word>");',
  550. 'this.a = null;',
  551. '']),
  552. LinesToStr([ // $mod.$main
  553. '']));
  554. end;
  555. procedure TTestGenerics.TestGen_Class_TypeInfo;
  556. begin
  557. WithTypeInfo:=true;
  558. StartProgram(false);
  559. Add([
  560. 'type',
  561. ' TObject = class end;',
  562. ' generic TBird<T> = class',
  563. ' published',
  564. ' m: T;',
  565. ' end;',
  566. ' TEagle = specialize TBird<word>;',
  567. 'var',
  568. ' b: specialize TBird<word>;',
  569. ' p: pointer;',
  570. 'begin',
  571. ' p:=typeinfo(TEagle);',
  572. ' p:=typeinfo(b);',
  573. '']);
  574. ConvertProgram;
  575. CheckSource('TestGen_Class_TypeInfo',
  576. LinesToStr([ // statements
  577. '$mod.$rtti.$Class("TBird<System.Word>");',
  578. 'rtl.createClass(this, "TObject", null, function () {',
  579. ' this.$init = function () {',
  580. ' };',
  581. ' this.$final = function () {',
  582. ' };',
  583. '});',
  584. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  585. ' this.$init = function () {',
  586. ' $mod.TObject.$init.call(this);',
  587. ' this.m = 0;',
  588. ' };',
  589. ' var $r = this.$rtti;',
  590. ' $r.addField("m", rtl.word);',
  591. '}, "TBird<System.Word>");',
  592. 'this.b = null;',
  593. 'this.p = null;',
  594. '']),
  595. LinesToStr([ // $mod.$main
  596. '$mod.p = $mod.$rtti["TBird<System.Word>"];',
  597. '$mod.p = $mod.b.$rtti;',
  598. '']));
  599. end;
  600. procedure TTestGenerics.TestGen_Class_TypeOverload;
  601. begin
  602. exit;// ToDo
  603. StartProgram(false);
  604. Add([
  605. '{$mode delphi}',
  606. 'type',
  607. ' TObject = class end;',
  608. ' TBird = word;',
  609. ' TBird<T> = class',
  610. ' m: T;',
  611. ' end;',
  612. ' TEagle = TBird<word>;',
  613. 'var',
  614. ' b: TBird<word>;',
  615. ' e: TEagle;',
  616. 'begin',
  617. '']);
  618. ConvertProgram;
  619. CheckSource('TestGen_Class_TypeOverload',
  620. LinesToStr([ // statements
  621. 'rtl.createClass(this, "TObject", null, function () {',
  622. ' this.$init = function () {',
  623. ' };',
  624. ' this.$final = function () {',
  625. ' };',
  626. '});',
  627. '']),
  628. LinesToStr([ // $mod.$main
  629. '']));
  630. end;
  631. procedure TTestGenerics.TestGen_Class_ClassProperty;
  632. begin
  633. StartProgram(false);
  634. Add([
  635. '{$mode delphi}',
  636. 'type',
  637. ' TObject = class end;',
  638. ' TBird<T> = class',
  639. ' private',
  640. ' class var fSize: T;',
  641. ' public',
  642. ' class property Size: T read fSize write fSize;',
  643. ' end;',
  644. ' TEagle = TBird<word>;',
  645. 'begin',
  646. ' TBird<word>.Size:=3+TBird<word>.Size;',
  647. '']);
  648. ConvertProgram;
  649. CheckSource('TestGen_Class_ClassProperty',
  650. LinesToStr([ // statements
  651. 'rtl.createClass(this, "TObject", null, function () {',
  652. ' this.$init = function () {',
  653. ' };',
  654. ' this.$final = function () {',
  655. ' };',
  656. '});',
  657. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  658. ' this.fSize = 0;',
  659. '}, "TBird<System.Word>");',
  660. '']),
  661. LinesToStr([ // $mod.$main
  662. '$mod.TBird$G1.fSize = 3 + $mod.TBird$G1.fSize;',
  663. '']));
  664. end;
  665. procedure TTestGenerics.TestGen_Class_ClassProc;
  666. begin
  667. StartProgram(false);
  668. Add([
  669. 'type',
  670. ' TObject = class end;',
  671. ' generic TPoint<T> = class',
  672. ' class var x: T;',
  673. ' class procedure Fly; static;',
  674. ' class procedure Run;',
  675. ' end;',
  676. 'class procedure Tpoint.Fly;',
  677. 'begin',
  678. ' x:=x+3;',
  679. ' tpoint.x:=tpoint.x+4;',
  680. ' Fly;',
  681. ' tpoint.Fly;',
  682. ' Run;',
  683. ' tpoint.Run;',
  684. 'end;',
  685. 'class procedure TPoint.Run;',
  686. 'begin',
  687. ' x:=x+5;',
  688. ' tpoint.x:=tpoint.x+6;',
  689. ' Fly;',
  690. ' tpoint.Fly;',
  691. ' Run;',
  692. ' tpoint.Run;',
  693. 'end;',
  694. 'var p: specialize TPoint<word>;',
  695. 'begin',
  696. '']);
  697. ConvertProgram;
  698. CheckSource('TestGen_Class_ClassProc',
  699. LinesToStr([ // statements
  700. 'rtl.createClass(this, "TObject", null, function () {',
  701. ' this.$init = function () {',
  702. ' };',
  703. ' this.$final = function () {',
  704. ' };',
  705. '});',
  706. 'rtl.createClass(this, "TPoint$G1", this.TObject, function () {',
  707. ' this.x = 0;',
  708. ' this.Fly = function () {',
  709. ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 3;',
  710. ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 4;',
  711. ' $mod.TPoint$G1.Fly();',
  712. ' $mod.TPoint$G1.Fly();',
  713. ' $mod.TPoint$G1.Run();',
  714. ' $mod.TPoint$G1.Run();',
  715. ' };',
  716. ' this.Run = function () {',
  717. ' $mod.TPoint$G1.x = this.x + 5;',
  718. ' $mod.TPoint$G1.x = $mod.TPoint$G1.x + 6;',
  719. ' this.Fly();',
  720. ' $mod.TPoint$G1.Fly();',
  721. ' this.Run();',
  722. ' $mod.TPoint$G1.Run();',
  723. ' };',
  724. '}, "TPoint<System.Word>");',
  725. 'this.p = null;',
  726. '']),
  727. LinesToStr([ // $mod.$main
  728. '']));
  729. end;
  730. procedure TTestGenerics.TestGen_Class_ClassConstructor;
  731. begin
  732. StartProgram(false);
  733. Add([
  734. 'type',
  735. ' TObject = class end;',
  736. ' generic TPoint<T> = class',
  737. ' class var x: T;',
  738. ' class procedure Fly; static;',
  739. ' class constructor Init;',
  740. ' end;',
  741. 'var count: word;',
  742. 'class procedure Tpoint.Fly;',
  743. 'begin',
  744. 'end;',
  745. 'class constructor tpoint.init;',
  746. 'begin',
  747. ' count:=count+1;',
  748. ' x:=3;',
  749. ' tpoint.x:=4;',
  750. ' fly;',
  751. ' tpoint.fly;',
  752. 'end;',
  753. 'var',
  754. ' r: specialize TPoint<word>;',
  755. ' s: specialize TPoint<smallint>;',
  756. 'begin',
  757. ' r.x:=10;',
  758. '']);
  759. ConvertProgram;
  760. CheckSource('TestGen_Class_ClassConstructor',
  761. LinesToStr([ // statements
  762. 'rtl.createClass(this, "TObject", null, function () {',
  763. ' this.$init = function () {',
  764. ' };',
  765. ' this.$final = function () {',
  766. ' };',
  767. '});',
  768. 'this.count = 0;',
  769. 'rtl.createClass(this, "TPoint$G1", this.TObject, function () {',
  770. ' this.x = 0;',
  771. ' this.Fly = function () {',
  772. ' };',
  773. '}, "TPoint<System.Word>");',
  774. 'this.r = null;',
  775. 'rtl.createClass(this, "TPoint$G2", this.TObject, function () {',
  776. ' this.x = 0;',
  777. ' this.Fly = function () {',
  778. ' };',
  779. '}, "TPoint<System.SmallInt>");',
  780. 'this.s = null;',
  781. '']),
  782. LinesToStr([ // $mod.$main
  783. '(function () {',
  784. ' $mod.count = $mod.count + 1;',
  785. ' $mod.TPoint$G1.x = 3;',
  786. ' $mod.TPoint$G1.x = 4;',
  787. ' $mod.TPoint$G1.Fly();',
  788. ' $mod.TPoint$G1.Fly();',
  789. '})();',
  790. '(function () {',
  791. ' $mod.count = $mod.count + 1;',
  792. ' $mod.TPoint$G2.x = 3;',
  793. ' $mod.TPoint$G2.x = 4;',
  794. ' $mod.TPoint$G2.Fly();',
  795. ' $mod.TPoint$G2.Fly();',
  796. '})();',
  797. '$mod.TPoint$G1.x = 10;',
  798. '']));
  799. end;
  800. procedure TTestGenerics.TestGen_Class_TypeCastSpecializesWarn;
  801. begin
  802. StartProgram(false);
  803. Add([
  804. '{$mode delphi}',
  805. 'type',
  806. ' TObject = class end;',
  807. ' TBird<T> = class F: T; end;',
  808. ' TBirdWord = TBird<Word>;',
  809. ' TBirdChar = TBird<Char>;',
  810. 'var',
  811. ' w: TBirdWord;',
  812. ' c: TBirdChar;',
  813. 'begin',
  814. ' w:=TBirdWord(c);',
  815. '']);
  816. ConvertProgram;
  817. CheckSource('TestGen_Class_TypeCastSpecializesWarn',
  818. LinesToStr([ // statements
  819. 'rtl.createClass(this, "TObject", null, function () {',
  820. ' this.$init = function () {',
  821. ' };',
  822. ' this.$final = function () {',
  823. ' };',
  824. '});',
  825. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  826. ' this.$init = function () {',
  827. ' $mod.TObject.$init.call(this);',
  828. ' this.F = 0;',
  829. ' };',
  830. '}, "TBird<System.Word>");',
  831. 'rtl.createClass(this, "TBird$G2", this.TObject, function () {',
  832. ' this.$init = function () {',
  833. ' $mod.TObject.$init.call(this);',
  834. ' this.F = "";',
  835. ' };',
  836. '}, "TBird<System.Char>");',
  837. 'this.w = null;',
  838. 'this.c = null;',
  839. '']),
  840. LinesToStr([ // $mod.$main
  841. '$mod.w = $mod.c;',
  842. '']));
  843. CheckHint(mtWarning,nClassTypesAreNotRelatedXY,'Class types "TBird<System.Char>" and "TBird<System.Word>" are not related');
  844. CheckResolverUnexpectedHints();
  845. end;
  846. procedure TTestGenerics.TestGen_Class_TypeCastSpecializesJSValueNoWarn;
  847. begin
  848. StartProgram(false);
  849. Add([
  850. '{$mode delphi}',
  851. 'type',
  852. ' TObject = class end;',
  853. ' TBird<T> = class F: T; end;',
  854. ' TBirdWord = TBird<Word>;',
  855. ' TBirdAny = TBird<JSValue>;',
  856. 'var',
  857. ' w: TBirdWord;',
  858. ' a: TBirdAny;',
  859. 'begin',
  860. ' w:=TBirdWord(a);',
  861. ' a:=TBirdAny(w);',
  862. '']);
  863. ConvertProgram;
  864. CheckSource('TestGen_Class_TypeCastSpecializesJSValueNoWarn',
  865. LinesToStr([ // statements
  866. 'rtl.createClass(this, "TObject", null, function () {',
  867. ' this.$init = function () {',
  868. ' };',
  869. ' this.$final = function () {',
  870. ' };',
  871. '});',
  872. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  873. ' this.$init = function () {',
  874. ' $mod.TObject.$init.call(this);',
  875. ' this.F = 0;',
  876. ' };',
  877. '}, "TBird<System.Word>");',
  878. 'rtl.createClass(this, "TBird$G2", this.TObject, function () {',
  879. ' this.$init = function () {',
  880. ' $mod.TObject.$init.call(this);',
  881. ' this.F = undefined;',
  882. ' };',
  883. '}, "TBird<System.JSValue>");',
  884. 'this.w = null;',
  885. 'this.a = null;',
  886. '']),
  887. LinesToStr([ // $mod.$main
  888. '$mod.w = $mod.a;',
  889. '$mod.a = $mod.w;',
  890. '']));
  891. CheckResolverUnexpectedHints();
  892. end;
  893. procedure TTestGenerics.TestGen_Class_OverloadsInUnit;
  894. begin
  895. StartProgram(true,[supTObject]);
  896. AddModuleWithIntfImplSrc('UnitA.pas',
  897. LinesToStr([
  898. 'type',
  899. ' generic TBird<T> = class',
  900. ' const c = 13;',
  901. ' constructor Create(w: T);',
  902. ' constructor Create(b: boolean);',
  903. ' end;',
  904. '']),
  905. LinesToStr([
  906. 'constructor TBird.Create(w: T);',
  907. 'const c = 14;',
  908. 'begin',
  909. 'end;',
  910. 'constructor TBird.Create(b: boolean);',
  911. 'const c = 15;',
  912. 'begin',
  913. 'end;',
  914. '']));
  915. Add([
  916. 'uses UnitA;',
  917. 'type',
  918. ' TWordBird = specialize TBird<word>;',
  919. ' TDoubleBird = specialize TBird<double>;',
  920. 'var',
  921. ' wb: TWordBird;',
  922. ' db: TDoubleBird;',
  923. 'begin',
  924. ' wb:=TWordBird.Create(3);',
  925. ' wb:=TWordBird.Create(true);',
  926. ' db:=TDoubleBird.Create(1.3);',
  927. ' db:=TDoubleBird.Create(true);',
  928. '']);
  929. ConvertProgram;
  930. CheckUnit('UnitA.pas',
  931. LinesToStr([ // statements
  932. 'rtl.module("UnitA", ["system"], function () {',
  933. ' var $mod = this;',
  934. ' rtl.createClass(this, "TBird$G1", pas.system.TObject, function () {',
  935. ' this.c = 13;',
  936. ' var c$1 = 14;',
  937. ' this.Create$1 = function (w) {',
  938. ' return this;',
  939. ' };',
  940. ' var c$2 = 15;',
  941. ' this.Create$2 = function (b) {',
  942. ' return this;',
  943. ' };',
  944. ' }, "TBird<System.Word>");',
  945. ' rtl.createClass(this, "TBird$G2", pas.system.TObject, function () {',
  946. ' this.c = 13;',
  947. ' var c$1 = 14;',
  948. ' this.Create$1 = function (w) {',
  949. ' return this;',
  950. ' };',
  951. ' var c$2 = 15;',
  952. ' this.Create$2 = function (b) {',
  953. ' return this;',
  954. ' };',
  955. ' }, "TBird<System.Double>");',
  956. '});',
  957. '']));
  958. CheckSource('TestGen_Class_OverloadsInUnit',
  959. LinesToStr([ // statements
  960. 'this.wb = null;',
  961. 'this.db = null;',
  962. '']),
  963. LinesToStr([ // $mod.$main
  964. '$mod.wb = pas.UnitA.TBird$G1.$create("Create$1", [3]);',
  965. '$mod.wb = pas.UnitA.TBird$G1.$create("Create$2", [true]);',
  966. '$mod.db = pas.UnitA.TBird$G2.$create("Create$1", [1.3]);',
  967. '$mod.db = pas.UnitA.TBird$G2.$create("Create$2", [true]);',
  968. '']));
  969. end;
  970. procedure TTestGenerics.TestGen_ClassForward_CircleRTTI;
  971. begin
  972. WithTypeInfo:=true;
  973. StartProgram(false);
  974. Add([
  975. '{$mode objfpc}',
  976. 'type',
  977. ' TObject = class end;',
  978. ' {$M+}',
  979. ' TPersistent = class end;',
  980. ' {$M-}',
  981. ' generic TAnt<T> = class;',
  982. ' generic TFish<U> = class(TPersistent)',
  983. ' private type AliasU = U;',
  984. ' published',
  985. ' a: specialize TAnt<AliasU>;',
  986. ' end;',
  987. ' generic TAnt<T> = class(TPersistent)',
  988. ' private type AliasT = T;',
  989. ' published',
  990. ' f: specialize TFish<AliasT>;',
  991. ' end;',
  992. 'var',
  993. ' WordFish: specialize TFish<word>;',
  994. ' p: pointer;',
  995. 'begin',
  996. ' p:=typeinfo(specialize TAnt<word>);',
  997. ' p:=typeinfo(specialize TFish<word>);',
  998. '']);
  999. ConvertProgram;
  1000. CheckSource('TestGen_ClassForward_CircleRTTI',
  1001. LinesToStr([ // statements
  1002. '$mod.$rtti.$Class("TAnt<System.Word>");',
  1003. '$mod.$rtti.$Class("TFish<System.Word>");',
  1004. 'rtl.createClass(this, "TObject", null, function () {',
  1005. ' this.$init = function () {',
  1006. ' };',
  1007. ' this.$final = function () {',
  1008. ' };',
  1009. '});',
  1010. 'rtl.createClass(this, "TPersistent", this.TObject, function () {',
  1011. '});',
  1012. 'rtl.createClass(this, "TAnt$G2", this.TPersistent, function () {',
  1013. ' this.$init = function () {',
  1014. ' $mod.TPersistent.$init.call(this);',
  1015. ' this.f = null;',
  1016. ' };',
  1017. ' this.$final = function () {',
  1018. ' this.f = undefined;',
  1019. ' $mod.TPersistent.$final.call(this);',
  1020. ' };',
  1021. ' var $r = this.$rtti;',
  1022. ' $r.addField("f", $mod.$rtti["TFish<System.Word>"]);',
  1023. '}, "TAnt<System.Word>");',
  1024. 'rtl.createClass(this, "TFish$G2", this.TPersistent, function () {',
  1025. ' this.$init = function () {',
  1026. ' $mod.TPersistent.$init.call(this);',
  1027. ' this.a = null;',
  1028. ' };',
  1029. ' this.$final = function () {',
  1030. ' this.a = undefined;',
  1031. ' $mod.TPersistent.$final.call(this);',
  1032. ' };',
  1033. ' var $r = this.$rtti;',
  1034. ' $r.addField("a", $mod.$rtti["TAnt<System.Word>"]);',
  1035. '}, "TFish<System.Word>");',
  1036. 'this.WordFish = null;',
  1037. 'this.p = null;',
  1038. '']),
  1039. LinesToStr([ // $mod.$main
  1040. '$mod.p = $mod.$rtti["TAnt<System.Word>"];',
  1041. '$mod.p = $mod.$rtti["TFish<System.Word>"];',
  1042. '']));
  1043. end;
  1044. procedure TTestGenerics.TestGen_Class_Nested_RTTI;
  1045. begin
  1046. WithTypeInfo:=true;
  1047. StartProgram(true,[supTObject]);
  1048. AddModuleWithIntfImplSrc('UnitA.pas',
  1049. LinesToStr([
  1050. 'type',
  1051. ' generic TAnt<T> = class',
  1052. ' type',
  1053. ' TLeg = class',
  1054. ' published',
  1055. ' Size: T;',
  1056. ' end;',
  1057. ' end;',
  1058. ' TBoolAnt = specialize TAnt<boolean>;',
  1059. '']),
  1060. LinesToStr([
  1061. '']));
  1062. Add([
  1063. 'uses UnitA;',
  1064. 'var',
  1065. ' BoolLeg: TBoolAnt.TLeg;',
  1066. 'begin',
  1067. ' if typeinfo(TBoolAnt.TLeg)=nil then ;',
  1068. '']);
  1069. ConvertProgram;
  1070. CheckUnit('UnitA.pas',
  1071. LinesToStr([ // statements
  1072. 'rtl.module("UnitA", ["system"], function () {',
  1073. ' var $mod = this;',
  1074. ' $mod.$rtti.$Class("TAnt<System.Boolean>");',
  1075. ' rtl.createClass(this, "TAnt$G1", pas.system.TObject, function () {',
  1076. ' rtl.createClass(this, "TLeg", pas.system.TObject, function () {',
  1077. ' this.$init = function () {',
  1078. ' pas.system.TObject.$init.call(this);',
  1079. ' this.Size = false;',
  1080. ' };',
  1081. ' var $r = this.$rtti;',
  1082. ' $r.addField("Size", rtl.boolean);',
  1083. ' }, "TAnt<System.Boolean>.TLeg");',
  1084. ' }, "TAnt<System.Boolean>");',
  1085. '});']));
  1086. CheckSource('TestGen_Class_Nested_RTTI',
  1087. LinesToStr([ // statements
  1088. 'this.BoolLeg = null;',
  1089. '']),
  1090. LinesToStr([ // $mod.$main
  1091. 'if (pas.UnitA.$rtti["TAnt<System.Boolean>.TLeg"] === null) ;',
  1092. '']));
  1093. end;
  1094. procedure TTestGenerics.TestGen_Class_ClassVarRecord_UnitImpl;
  1095. begin
  1096. StartProgram(true,[supTObject]);
  1097. AddModuleWithIntfImplSrc('UnitA.pas',
  1098. LinesToStr([
  1099. 'type',
  1100. ' generic TAnt<T> = class',
  1101. ' public',
  1102. ' class var x: T;',
  1103. ' class var a: array[1..2] of T;',
  1104. ' end;',
  1105. '']),
  1106. LinesToStr([
  1107. 'type',
  1108. ' TBird = record',
  1109. ' b: word;',
  1110. ' end;',
  1111. 'var f: specialize TAnt<TBird>;',
  1112. 'begin',
  1113. ' f.x.b:=f.x.b+10;',
  1114. '']));
  1115. Add([
  1116. 'uses UnitA;',
  1117. 'begin',
  1118. 'end.']);
  1119. ConvertProgram;
  1120. CheckUnit('UnitA.pas',
  1121. LinesToStr([ // statements
  1122. 'rtl.module("UnitA", ["system"], function () {',
  1123. ' var $mod = this;',
  1124. ' var $impl = $mod.$impl;',
  1125. ' rtl.createClass(this, "TAnt$G1", pas.system.TObject, function () {',
  1126. ' this.$initSpec = function () {',
  1127. ' this.x = $impl.TBird.$new();',
  1128. ' this.a = rtl.arraySetLength(null, $impl.TBird, 2);',
  1129. ' };',
  1130. ' }, "TAnt<UnitA.TBird>");',
  1131. ' $mod.$implcode = function () {',
  1132. ' rtl.recNewT($impl, "TBird", function () {',
  1133. ' this.b = 0;',
  1134. ' this.$eq = function (b) {',
  1135. ' return this.b === b.b;',
  1136. ' };',
  1137. ' this.$assign = function (s) {',
  1138. ' this.b = s.b;',
  1139. ' return this;',
  1140. ' };',
  1141. ' });',
  1142. ' $impl.f = null;',
  1143. ' };',
  1144. ' $mod.$init = function () {',
  1145. ' $impl.f.x.b = $impl.f.x.b + 10;',
  1146. ' };',
  1147. '}, []);',
  1148. '']));
  1149. CheckSource('TestGen_Class_ClassVarRecord_UnitImpl',
  1150. LinesToStr([ // statements
  1151. 'pas.UnitA.TAnt$G1.$initSpec();',
  1152. '']),
  1153. LinesToStr([ // $mod.$main
  1154. '']));
  1155. end;
  1156. procedure TTestGenerics.TestGen_ExtClass_VarArgsOfType;
  1157. begin
  1158. StartProgram(false);
  1159. Add([
  1160. '{$mode objfpc}',
  1161. '{$modeswitch externalclass}',
  1162. 'type',
  1163. ' TJSObject = class external name ''Object''',
  1164. ' end;',
  1165. ' generic TGJSSet<T> = class external name ''Set''',
  1166. ' constructor new(aElement1: T); varargs of T; overload;',
  1167. ' function bind(thisArg: TJSObject): T; varargs of T;',
  1168. ' end;',
  1169. ' TJSWordSet = specialize TGJSSet<word>;',
  1170. 'var',
  1171. ' s: TJSWordSet;',
  1172. ' w: word;',
  1173. 'begin',
  1174. ' s:=TJSWordSet.new(3);',
  1175. ' s:=TJSWordSet.new(3,5);',
  1176. ' w:=s.bind(nil);',
  1177. ' w:=s.bind(nil,6);',
  1178. ' w:=s.bind(nil,7,8);',
  1179. '']);
  1180. ConvertProgram;
  1181. CheckSource('TestGen_ExtClass_VarArgsOfType',
  1182. LinesToStr([ // statements
  1183. 'this.s = null;',
  1184. 'this.w = 0;',
  1185. '']),
  1186. LinesToStr([ // $mod.$main
  1187. '$mod.s = new Set(3);',
  1188. '$mod.s = new Set(3, 5);',
  1189. '$mod.w = $mod.s.bind(null);',
  1190. '$mod.w = $mod.s.bind(null, 6);',
  1191. '$mod.w = $mod.s.bind(null, 7, 8);',
  1192. '']));
  1193. end;
  1194. procedure TTestGenerics.TestGen_ExtClass_Array;
  1195. begin
  1196. StartProgram(false);
  1197. Add([
  1198. '{$mode delphi}',
  1199. '{$ModeSwitch externalclass}',
  1200. 'type',
  1201. ' NativeInt = longint;',
  1202. ' TJSGenArray<T> = Class external name ''Array''',
  1203. ' private',
  1204. ' function GetElements(Index: NativeInt): T; external name ''[]'';',
  1205. ' procedure SetElements(Index: NativeInt; const AValue: T); external name ''[]'';',
  1206. ' public',
  1207. ' type TSelfType = TJSGenArray<T>;',
  1208. ' public',
  1209. ' FLength : NativeInt; external name ''length'';',
  1210. ' constructor new; overload;',
  1211. ' constructor new(aLength : NativeInt); overload;',
  1212. ' class function _of() : TSelfType; varargs; external name ''of'';',
  1213. ' function fill(aValue : T) : TSelfType; overload;',
  1214. ' function fill(aValue : T; aStartIndex : NativeInt) : TSelfType; overload;',
  1215. ' function fill(aValue : T; aStartIndex,aEndIndex : NativeInt) : TSelfType; overload;',
  1216. ' property Length : NativeInt Read FLength Write FLength;',
  1217. ' property Elements[Index: NativeInt]: T read GetElements write SetElements; default;',
  1218. ' end;',
  1219. ' TJSWordArray = TJSGenArray<word>;',
  1220. 'var',
  1221. ' wa: TJSWordArray;',
  1222. ' w: word;',
  1223. 'begin',
  1224. ' wa:=TJSWordArray.new;',
  1225. ' wa:=TJSWordArray.new(3);',
  1226. ' wa:=TJSWordArray._of(4,5);',
  1227. ' wa:=wa.fill(7);',
  1228. ' wa:=wa.fill(7,8,9);',
  1229. ' w:=wa.length;',
  1230. ' wa.length:=10;',
  1231. ' wa[11]:=w;',
  1232. ' w:=wa[12];',
  1233. '']);
  1234. ConvertProgram;
  1235. CheckSource('TestGen_ExtClass_Array',
  1236. LinesToStr([ // statements
  1237. 'this.wa = null;',
  1238. 'this.w = 0;',
  1239. '']),
  1240. LinesToStr([ // $mod.$main
  1241. '$mod.wa = new Array();',
  1242. '$mod.wa = new Array(3);',
  1243. '$mod.wa = Array.of(4, 5);',
  1244. '$mod.wa = $mod.wa.fill(7);',
  1245. '$mod.wa = $mod.wa.fill(7, 8, 9);',
  1246. '$mod.w = $mod.wa.length;',
  1247. '$mod.wa.length = 10;',
  1248. '$mod.wa[11] = $mod.w;',
  1249. '$mod.w = $mod.wa[12];',
  1250. '']));
  1251. end;
  1252. procedure TTestGenerics.TestGen_ExtClass_GenJSValueAssign;
  1253. begin
  1254. StartProgram(false);
  1255. Add([
  1256. '{$mode delphi}',
  1257. '{$modeswitch externalclass}',
  1258. 'type',
  1259. ' TExt<T> = class external name ''Ext''',
  1260. ' F: T;',
  1261. ' end;',
  1262. ' TExtWord = TExt<Word>;',
  1263. ' TExtAny = TExt<JSValue>;',
  1264. 'procedure Run(e: TExtAny);',
  1265. 'begin end;',
  1266. 'var',
  1267. ' w: TExtWord;',
  1268. ' a: TExtAny;',
  1269. 'begin',
  1270. ' a:=w;',
  1271. ' Run(w);',
  1272. '']);
  1273. ConvertProgram;
  1274. CheckSource('TestGen_ExtClass_GenJSValueAssign',
  1275. LinesToStr([ // statements
  1276. 'this.Run = function (e) {',
  1277. '};',
  1278. 'this.w = null;',
  1279. 'this.a = null;',
  1280. '']),
  1281. LinesToStr([ // $mod.$main
  1282. '$mod.a = $mod.w;',
  1283. '$mod.Run($mod.w);',
  1284. '']));
  1285. CheckResolverUnexpectedHints();
  1286. end;
  1287. procedure TTestGenerics.TestGen_ExtClass_AliasMemberType;
  1288. begin
  1289. StartProgram(false);
  1290. Add([
  1291. '{$mode objfpc}',
  1292. '{$modeswitch externalclass}',
  1293. 'type',
  1294. ' generic TExt<T> = class external name ''Ext''',
  1295. ' public type TRun = reference to function(a: T): T;',
  1296. ' end;',
  1297. ' TExtWord = specialize TExt<word>;',
  1298. ' TExtWordRun = TExtWord.TRun;',
  1299. 'begin',
  1300. '']);
  1301. ConvertProgram;
  1302. CheckSource('TestGen_ExtClass_AliasMemberType',
  1303. LinesToStr([ // statements
  1304. '']),
  1305. LinesToStr([ // $mod.$main
  1306. '']));
  1307. end;
  1308. procedure TTestGenerics.TestGen_ExtClass_RTTI;
  1309. begin
  1310. WithTypeInfo:=true;
  1311. StartProgram(false);
  1312. Add([
  1313. '{$mode objfpc}',
  1314. '{$modeswitch externalclass}',
  1315. 'type',
  1316. ' generic TGJSSET<T> = class external name ''SET''',
  1317. ' A: T;',
  1318. ' end;',
  1319. ' TJSSet = specialize TGJSSET<JSValue>;',
  1320. ' TJSSetEventProc = reference to procedure(value : JSValue; key: NativeInt; set_: TJSSet);',
  1321. 'var p: Pointer;',
  1322. 'begin',
  1323. ' p:=typeinfo(TJSSetEventProc);',
  1324. '']);
  1325. ConvertProgram;
  1326. CheckSource('TestGen_ExtClass_RTTI',
  1327. LinesToStr([ // statements
  1328. 'this.$rtti.$ExtClass("TGJSSET<System.JSValue>", {',
  1329. ' jsclass: "SET"',
  1330. '});',
  1331. 'this.$rtti.$RefToProcVar("TJSSetEventProc", {',
  1332. ' procsig: rtl.newTIProcSig([["value", rtl.jsvalue], ["key", rtl.nativeint], ["set_", this.$rtti["TGJSSET<System.JSValue>"]]])',
  1333. '});',
  1334. 'this.p = null;',
  1335. '']),
  1336. LinesToStr([ // $mod.$main
  1337. '$mod.p = $mod.$rtti["TJSSetEventProc"];',
  1338. '']));
  1339. end;
  1340. procedure TTestGenerics.TestGen_ExtClass_UnitImplRec;
  1341. begin
  1342. WithTypeInfo:=true;
  1343. StartProgram(true,[supTObject]);
  1344. AddModuleWithIntfImplSrc('UnitA.pas',
  1345. LinesToStr([
  1346. '{$mode objfpc}',
  1347. '{$modeswitch externalclass}',
  1348. 'type',
  1349. ' generic TAnt<T> = class external name ''SET''',
  1350. ' x: T;',
  1351. ' end;',
  1352. '']),
  1353. LinesToStr([
  1354. 'type',
  1355. ' TBird = record',
  1356. ' b: word;',
  1357. ' end;',
  1358. 'var',
  1359. ' f: specialize TAnt<TBird>;',
  1360. 'begin',
  1361. ' f.x.b:=f.x.b+10;',
  1362. '']));
  1363. Add([
  1364. 'uses UnitA;',
  1365. 'begin',
  1366. 'end.']);
  1367. ConvertProgram;
  1368. CheckUnit('UnitA.pas',
  1369. LinesToStr([ // statements
  1370. 'rtl.module("UnitA", ["system"], function () {',
  1371. ' var $mod = this;',
  1372. ' var $impl = $mod.$impl;',
  1373. ' this.$rtti.$ExtClass("TAnt<UnitA.TBird>", {',
  1374. ' jsclass: "SET"',
  1375. ' });',
  1376. ' $mod.$implcode = function () {',
  1377. ' rtl.recNewT($impl, "TBird", function () {',
  1378. ' this.b = 0;',
  1379. ' this.$eq = function (b) {',
  1380. ' return this.b === b.b;',
  1381. ' };',
  1382. ' this.$assign = function (s) {',
  1383. ' this.b = s.b;',
  1384. ' return this;',
  1385. ' };',
  1386. ' var $r = $mod.$rtti.$Record("TBird", {});',
  1387. ' $r.addField("b", rtl.word);',
  1388. ' });',
  1389. ' $impl.f = null;',
  1390. ' };',
  1391. ' $mod.$init = function () {',
  1392. ' $impl.f.x.b = $impl.f.x.b + 10;',
  1393. ' };',
  1394. '}, []);']));
  1395. CheckSource('TestGen_Class_ClassVarRecord_UnitImpl',
  1396. LinesToStr([ // statements
  1397. //'pas.UnitA.TAnt$G1.$initSpec();',
  1398. '']),
  1399. LinesToStr([ // $mod.$main
  1400. '']));
  1401. end;
  1402. procedure TTestGenerics.TestGen_ClassInterface_Corba;
  1403. begin
  1404. StartProgram(false);
  1405. Add([
  1406. '{$interfaces corba}',
  1407. 'type',
  1408. ' IUnknown = interface;',
  1409. ' IUnknown = interface',
  1410. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1411. ' end;',
  1412. ' IInterface = IUnknown;',
  1413. ' generic IBird<T> = interface(IInterface)',
  1414. ' function GetSize: T;',
  1415. ' procedure SetSize(i: T);',
  1416. ' property Size: T read GetSize write SetSize;',
  1417. ' procedure DoIt(i: T);',
  1418. ' end;',
  1419. ' TObject = class',
  1420. ' end;',
  1421. ' generic TBird<T> = class(TObject,specialize IBird<T>)',
  1422. ' function GetSize: T; virtual; abstract;',
  1423. ' procedure SetSize(i: T); virtual; abstract;',
  1424. ' procedure DoIt(i: T); virtual; abstract;',
  1425. ' end;',
  1426. ' IWordBird = specialize IBird<Word>;',
  1427. ' TWordBird = specialize TBird<Word>;',
  1428. 'var',
  1429. ' BirdIntf: IWordBird;',
  1430. 'begin',
  1431. ' BirdIntf.Size:=BirdIntf.Size;',
  1432. '']);
  1433. ConvertProgram;
  1434. CheckSource('TestGen_ClassInterface_Corba',
  1435. LinesToStr([ // statements
  1436. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  1437. 'rtl.createClass(this, "TObject", null, function () {',
  1438. ' this.$init = function () {',
  1439. ' };',
  1440. ' this.$final = function () {',
  1441. ' };',
  1442. '});',
  1443. 'rtl.createInterface(',
  1444. ' this,',
  1445. ' "IBird$G2",',
  1446. ' "{33AB51C6-6240-3BDF-B4B0-D48A593EAB0A}",',
  1447. ' ["GetSize", "SetSize", "DoIt"],',
  1448. ' this.IUnknown,',
  1449. ' "IBird<System.Word>"',
  1450. ');',
  1451. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  1452. ' rtl.addIntf(this, $mod.IBird$G2);',
  1453. '}, "TBird<System.Word>");',
  1454. 'this.BirdIntf = null;',
  1455. '']),
  1456. LinesToStr([ // $mod.$main
  1457. '$mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  1458. '']));
  1459. end;
  1460. procedure TTestGenerics.TestGen_ClassInterface_InterfacedObject;
  1461. begin
  1462. StartProgram(true,[supTInterfacedObject]);
  1463. Add([
  1464. '{$mode delphi}',
  1465. 'type',
  1466. ' IComparer<T> = interface [''{505778ED-F783-4456-9691-32F419CC5E18}'']',
  1467. ' function Compare(const Left, Right: T): Integer; overload;',
  1468. ' end;',
  1469. ' TComparer<T> = class(TInterfacedObject, IComparer<T>)',
  1470. ' function Compare(const Left, Right: T): Integer;',
  1471. ' end;',
  1472. 'function TComparer<T>.Compare(const Left, Right: T): Integer; begin end;',
  1473. 'var',
  1474. ' aComparer : IComparer<Integer>;',
  1475. 'begin',
  1476. ' aComparer:=TComparer<Integer>.Create;',
  1477. '']);
  1478. ConvertProgram;
  1479. CheckSource('TestGen_ClassInterface_InterfacedObject',
  1480. LinesToStr([ // statements
  1481. 'rtl.createInterface(',
  1482. ' this,',
  1483. ' "IComparer$G2",',
  1484. ' "{505778ED-F783-4456-9691-32F419CC5E18}",',
  1485. ' ["Compare"],',
  1486. ' pas.system.IUnknown,',
  1487. ' "IComparer<System.Longint>"',
  1488. ');',
  1489. 'this.aComparer = null;',
  1490. 'rtl.createClass(this, "TComparer$G1", pas.system.TInterfacedObject, function () {',
  1491. ' this.Compare = function (Left, Right) {',
  1492. ' var Result = 0;',
  1493. ' return Result;',
  1494. ' };',
  1495. ' rtl.addIntf(this, $mod.IComparer$G2);',
  1496. ' rtl.addIntf(this, pas.system.IUnknown);',
  1497. '}, "TComparer<System.Longint>");',
  1498. '']),
  1499. LinesToStr([ // $mod.$main
  1500. 'rtl.setIntfP($mod, "aComparer", rtl.queryIntfT($mod.TComparer$G1.$create("Create"), $mod.IComparer$G2), true);',
  1501. '']));
  1502. end;
  1503. procedure TTestGenerics.TestGen_ClassInterface_COM_RTTI;
  1504. begin
  1505. StartProgram(true,[supTInterfacedObject]);
  1506. Add([
  1507. '{$mode delphi}',
  1508. 'type',
  1509. ' TBird = class',
  1510. ' function Fly<T: IInterface>: T;',
  1511. ' end;',
  1512. ' IAnt = interface',
  1513. ' procedure InterfaceProc;',
  1514. ' end;',
  1515. 'function TBird.Fly<T>: T;',
  1516. 'begin',
  1517. ' if TypeInfo(T)=nil then ;',
  1518. 'end;',
  1519. 'var Bird: TBird;',
  1520. ' Ant: IAnt;',
  1521. 'begin',
  1522. ' Ant := Bird.Fly<IAnt>;',
  1523. '']);
  1524. ConvertProgram;
  1525. CheckSource('TestGen_ClassInterface_COM_RTTI',
  1526. LinesToStr([ // statements
  1527. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  1528. ' this.Fly$G1 = function () {',
  1529. ' var Result = null;',
  1530. ' if ($mod.$rtti["IAnt"] === null) ;',
  1531. ' return Result;',
  1532. ' };',
  1533. '});',
  1534. 'rtl.createInterface(this, "IAnt", "{B9D0FF27-A446-3A1B-AA85-F167837AA297}", ["InterfaceProc"], pas.system.IUnknown);',
  1535. 'this.Bird = null;',
  1536. 'this.Ant = null;',
  1537. '']),
  1538. LinesToStr([ // $mod.$main
  1539. 'rtl.setIntfP($mod, "Ant", $mod.Bird.Fly$G1(), true);',
  1540. '']));
  1541. end;
  1542. procedure TTestGenerics.TestGen_ClassInterface_Helper;
  1543. begin
  1544. StartProgram(true,[supTInterfacedObject]);
  1545. Add([
  1546. '{$mode objfpc}',
  1547. '{$ModeSwitch typehelpers}',
  1548. 'type',
  1549. ' IAnt = interface',
  1550. ' procedure InterfaceProc;',
  1551. ' end;',
  1552. ' TBird = type helper for IAnt',
  1553. ' generic procedure Fly<T>(a: T);',
  1554. ' end;',
  1555. 'generic procedure TBird.Fly<T>(a: T);',
  1556. 'begin',
  1557. 'end;',
  1558. 'var ',
  1559. ' Ant: IAnt;',
  1560. 'begin',
  1561. ' Ant.specialize Fly<word>(3);',
  1562. '']);
  1563. ConvertProgram;
  1564. CheckSource('TestGen_ClassInterface_COM_RTTI',
  1565. LinesToStr([ // statements
  1566. 'rtl.createInterface(this, "IAnt", "{B9D0FF27-A446-3A1B-AA85-F167837AA297}", ["InterfaceProc"], pas.system.IUnknown);',
  1567. 'rtl.createHelper(this, "TBird", null, function () {',
  1568. ' this.Fly$G1 = function (a) {',
  1569. ' };',
  1570. '});',
  1571. 'this.Ant = null;',
  1572. '']),
  1573. LinesToStr([ // $mod.$main
  1574. '$mod.TBird.Fly$G1.call({',
  1575. ' p: $mod,',
  1576. ' get: function () {',
  1577. ' return this.p.Ant;',
  1578. ' },',
  1579. ' set: function (v) {',
  1580. ' rtl.setIntfP(this.p, "Ant", v);',
  1581. ' }',
  1582. '}, 3);',
  1583. '']));
  1584. end;
  1585. procedure TTestGenerics.TestGen_InlineSpec_Constructor;
  1586. begin
  1587. StartProgram(false);
  1588. Add([
  1589. '{$mode objfpc}',
  1590. 'type',
  1591. ' TObject = class',
  1592. ' public',
  1593. ' constructor Create;',
  1594. ' end;',
  1595. ' generic TBird<T> = class',
  1596. ' end;',
  1597. 'constructor TObject.Create; begin end;',
  1598. 'var b: specialize TBird<word>;',
  1599. 'begin',
  1600. ' b:=specialize TBird<word>.Create;',
  1601. '']);
  1602. ConvertProgram;
  1603. CheckSource('TestGen_InlineSpec_Constructor',
  1604. LinesToStr([ // statements
  1605. 'rtl.createClass(this, "TObject", null, function () {',
  1606. ' this.$init = function () {',
  1607. ' };',
  1608. ' this.$final = function () {',
  1609. ' };',
  1610. ' this.Create = function () {',
  1611. ' return this;',
  1612. ' };',
  1613. '});',
  1614. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  1615. '}, "TBird<System.Word>");',
  1616. 'this.b = null;',
  1617. '']),
  1618. LinesToStr([ // $mod.$main
  1619. '$mod.b = $mod.TBird$G1.$create("Create");',
  1620. '']));
  1621. end;
  1622. procedure TTestGenerics.TestGen_CallUnitImplProc;
  1623. begin
  1624. AddModuleWithIntfImplSrc('UnitA.pas',
  1625. LinesToStr([
  1626. 'type',
  1627. ' generic TBird<T> = class',
  1628. ' procedure Fly;',
  1629. ' end;',
  1630. 'var b: specialize TBird<boolean>;',
  1631. '']),
  1632. LinesToStr([
  1633. 'procedure DoIt;',
  1634. 'var b: specialize TBird<word>;',
  1635. 'begin',
  1636. ' b:=specialize TBird<word>.Create;',
  1637. ' b.Fly;',
  1638. 'end;',
  1639. 'procedure TBird.Fly;',
  1640. 'begin',
  1641. ' DoIt;',
  1642. 'end;',
  1643. '']));
  1644. StartProgram(true,[supTObject]);
  1645. Add('uses UnitA;');
  1646. Add('begin');
  1647. ConvertProgram;
  1648. CheckUnit('UnitA.pas',
  1649. LinesToStr([ // statements
  1650. 'rtl.module("UnitA", ["system"], function () {',
  1651. ' var $mod = this;',
  1652. ' var $impl = $mod.$impl;',
  1653. ' rtl.createClass(this, "TBird$G1", pas.system.TObject, function () {',
  1654. ' this.Fly = function () {',
  1655. ' $impl.DoIt();',
  1656. ' };',
  1657. ' }, "TBird<System.Boolean>");',
  1658. ' this.b = null;',
  1659. ' rtl.createClass(this, "TBird$G2", pas.system.TObject, function () {',
  1660. ' this.Fly = function () {',
  1661. ' $impl.DoIt();',
  1662. ' };',
  1663. ' }, "TBird<System.Word>");',
  1664. ' $mod.$implcode = function () {',
  1665. ' $impl.DoIt = function () {',
  1666. ' var b = null;',
  1667. ' b = $mod.TBird$G2.$create("Create");',
  1668. ' b.Fly();',
  1669. ' };',
  1670. ' };',
  1671. '}, []);',
  1672. '']));
  1673. end;
  1674. procedure TTestGenerics.TestGen_IntAssignTemplVar;
  1675. begin
  1676. StartProgram(false);
  1677. Add([
  1678. 'type',
  1679. ' TObject = class end;',
  1680. ' generic TBird<T> = class',
  1681. ' m: T;',
  1682. ' procedure Fly;',
  1683. ' end;',
  1684. 'var b: specialize TBird<word>;',
  1685. 'procedure TBird.Fly;',
  1686. 'var i: nativeint;',
  1687. 'begin',
  1688. ' i:=m;',
  1689. 'end;',
  1690. 'begin',
  1691. '']);
  1692. ConvertProgram;
  1693. CheckSource('TestGen_IntAssignTemplVar',
  1694. LinesToStr([ // statements
  1695. 'rtl.createClass(this, "TObject", null, function () {',
  1696. ' this.$init = function () {',
  1697. ' };',
  1698. ' this.$final = function () {',
  1699. ' };',
  1700. '});',
  1701. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  1702. ' this.$init = function () {',
  1703. ' $mod.TObject.$init.call(this);',
  1704. ' this.m = 0;',
  1705. ' };',
  1706. ' this.Fly = function () {',
  1707. ' var i = 0;',
  1708. ' i = this.m;',
  1709. ' };',
  1710. '}, "TBird<System.Word>");',
  1711. 'this.b = null;',
  1712. '']),
  1713. LinesToStr([ // $mod.$main
  1714. '']));
  1715. end;
  1716. procedure TTestGenerics.TestGen_TypeCastDotField;
  1717. begin
  1718. StartProgram(false);
  1719. Add([
  1720. 'type',
  1721. ' TObject = class end;',
  1722. ' generic TBird<T> = class',
  1723. ' Field: T;',
  1724. ' procedure Fly;',
  1725. ' end;',
  1726. 'var',
  1727. ' o: TObject;',
  1728. ' b: specialize TBird<word>;',
  1729. 'procedure TBird.Fly;',
  1730. 'begin',
  1731. ' specialize TBird<word>(o).Field:=3;',
  1732. ' if 4=specialize TBird<word>(o).Field then ;',
  1733. 'end;',
  1734. 'begin',
  1735. ' specialize TBird<word>(o).Field:=5;',
  1736. ' if 6=specialize TBird<word>(o).Field then ;',
  1737. '']);
  1738. ConvertProgram;
  1739. CheckSource('TestGen_TypeCastDotField',
  1740. LinesToStr([ // statements
  1741. 'rtl.createClass(this, "TObject", null, function () {',
  1742. ' this.$init = function () {',
  1743. ' };',
  1744. ' this.$final = function () {',
  1745. ' };',
  1746. '});',
  1747. 'this.o = null;',
  1748. 'rtl.createClass(this, "TBird$G1", this.TObject, function () {',
  1749. ' this.$init = function () {',
  1750. ' $mod.TObject.$init.call(this);',
  1751. ' this.Field = 0;',
  1752. ' };',
  1753. ' this.Fly = function () {',
  1754. ' $mod.o.Field = 3;',
  1755. ' if (4 === $mod.o.Field) ;',
  1756. ' };',
  1757. '}, "TBird<System.Word>");',
  1758. 'this.b = null;',
  1759. '']),
  1760. LinesToStr([ // $mod.$main
  1761. '$mod.o.Field = 5;',
  1762. 'if (6 === $mod.o.Field) ;',
  1763. '']));
  1764. end;
  1765. procedure TTestGenerics.TestGen_HelperForArray;
  1766. begin
  1767. StartProgram(false);
  1768. Add([
  1769. '{$ModeSwitch typehelpers}',
  1770. 'type',
  1771. ' generic TArr<T> = array[1..2] of T;',
  1772. ' TWordArrHelper = type helper for specialize TArr<word>',
  1773. ' procedure Fly(w: word);',
  1774. ' end;',
  1775. 'procedure TWordArrHelper.Fly(w: word);',
  1776. 'begin',
  1777. 'end;',
  1778. 'var',
  1779. ' a: specialize TArr<word>;',
  1780. 'begin',
  1781. ' a.Fly(3);',
  1782. '']);
  1783. ConvertProgram;
  1784. CheckSource('TestGen_HelperForArray',
  1785. LinesToStr([ // statements
  1786. 'rtl.createHelper(this, "TWordArrHelper", null, function () {',
  1787. ' this.Fly = function (w) {',
  1788. ' };',
  1789. '});',
  1790. 'this.a = rtl.arraySetLength(null, 0, 2);',
  1791. '']),
  1792. LinesToStr([ // $mod.$main
  1793. '$mod.TWordArrHelper.Fly.call({',
  1794. ' p: $mod,',
  1795. ' get: function () {',
  1796. ' return this.p.a;',
  1797. ' },',
  1798. ' set: function (v) {',
  1799. ' this.p.a = v;',
  1800. ' }',
  1801. '}, 3);',
  1802. '']));
  1803. end;
  1804. procedure TTestGenerics.TestGenProc_Function_ObjFPC;
  1805. begin
  1806. StartProgram(false);
  1807. Add([
  1808. 'generic function Run<T>(a: T): T;',
  1809. 'var i: T;',
  1810. 'begin',
  1811. ' a:=i;',
  1812. ' Result:=a;',
  1813. 'end;',
  1814. 'var w: word;',
  1815. 'begin',
  1816. ' w:=specialize Run<word>(3);',
  1817. '']);
  1818. ConvertProgram;
  1819. CheckSource('TestGenProc_Function_ObjFPC',
  1820. LinesToStr([ // statements
  1821. 'this.w = 0;',
  1822. 'this.Run$G1 = function (a) {',
  1823. ' var Result = 0;',
  1824. ' var i = 0;',
  1825. ' a = i;',
  1826. ' Result = a;',
  1827. ' return Result;',
  1828. '};',
  1829. '']),
  1830. LinesToStr([ // $mod.$main
  1831. '$mod.w = $mod.Run$G1(3);',
  1832. '']));
  1833. end;
  1834. procedure TTestGenerics.TestGenProc_Function_Delphi;
  1835. begin
  1836. StartProgram(false);
  1837. Add([
  1838. '{$mode delphi}',
  1839. 'function Run<T>(a: T): T;',
  1840. 'var i: T;',
  1841. 'begin',
  1842. ' a:=i;',
  1843. ' Result:=a;',
  1844. 'end;',
  1845. 'var w: word;',
  1846. 'begin',
  1847. ' w:=Run<word>(3);',
  1848. '']);
  1849. ConvertProgram;
  1850. CheckSource('TestGenProc_Function_Delphi',
  1851. LinesToStr([ // statements
  1852. 'this.w = 0;',
  1853. 'this.Run$G1 = function (a) {',
  1854. ' var Result = 0;',
  1855. ' var i = 0;',
  1856. ' a = i;',
  1857. ' Result = a;',
  1858. ' return Result;',
  1859. '};',
  1860. '']),
  1861. LinesToStr([ // $mod.$main
  1862. '$mod.w = $mod.Run$G1(3);',
  1863. '']));
  1864. end;
  1865. procedure TTestGenerics.TestGenProc_Overload;
  1866. begin
  1867. StartProgram(false);
  1868. Add([
  1869. 'generic procedure DoIt<T>(a: T; w: word); overload;',
  1870. 'begin',
  1871. 'end;',
  1872. 'generic procedure DoIt<T>(a: T; b: boolean); overload;',
  1873. 'begin',
  1874. 'end;',
  1875. 'begin',
  1876. ' specialize DoIt<word>(3,4);',
  1877. ' specialize DoIt<boolean>(false,5);',
  1878. ' specialize DoIt<word>(6,true);',
  1879. ' specialize DoIt<double>(7.3,true);',
  1880. '']);
  1881. ConvertProgram;
  1882. CheckSource('TestGenProc_Overload',
  1883. LinesToStr([ // statements
  1884. 'this.DoIt$G1 = function (a, w) {',
  1885. '};',
  1886. 'this.DoIt$G2 = function (a, w) {',
  1887. '};',
  1888. 'this.DoIt$1G1 = function (a, b) {',
  1889. '};',
  1890. 'this.DoIt$1G2 = function (a, b) {',
  1891. '};',
  1892. '']),
  1893. LinesToStr([ // $mod.$main
  1894. '$mod.DoIt$G1(3, 4);',
  1895. '$mod.DoIt$G2(false, 5);',
  1896. '$mod.DoIt$1G1(6, true);',
  1897. '$mod.DoIt$1G2(7.3, true);',
  1898. '']));
  1899. end;
  1900. procedure TTestGenerics.TestGenProc_Forward;
  1901. begin
  1902. StartProgram(false);
  1903. Add([
  1904. '{$mode delphi}',
  1905. 'procedure Run<S>(a: S; b: boolean); forward;',
  1906. 'procedure Run<S>(a: S; b: boolean);',
  1907. 'begin',
  1908. ' Run<word>(1,true);',
  1909. 'end;',
  1910. 'begin',
  1911. ' Run(1.3,true);',
  1912. '']);
  1913. ConvertProgram;
  1914. CheckSource('TestGenProc_infer_OverloadForward',
  1915. LinesToStr([ // statements
  1916. 'this.Run$G1 = function (a, b) {',
  1917. ' $mod.Run$G1(1, true);',
  1918. '};',
  1919. 'this.Run$G2 = function (a, b) {',
  1920. ' $mod.Run$G1(1, true);',
  1921. '};',
  1922. '']),
  1923. LinesToStr([ // $mod.$main
  1924. '$mod.Run$G2(1.3, true);',
  1925. '']));
  1926. end;
  1927. procedure TTestGenerics.TestGenProc_Infer_OverloadForward;
  1928. begin
  1929. StartProgram(false);
  1930. Add([
  1931. '{$mode delphi}',
  1932. 'procedure {#A}Run<S>(a: S; b: boolean); forward; overload;',
  1933. 'procedure {#B}Run<T>(a: T; w: word); forward; overload;',
  1934. 'procedure {#C}Run<U>(a: U; b: U); forward; overload;',
  1935. 'procedure {#A2}Run<S>(a: S; b: boolean); overload;',
  1936. 'begin',
  1937. ' {@A}Run(1,true);', // non generic take precedence
  1938. ' {@B}Run(2,word(3));', // non generic take precedence
  1939. ' {@C}Run(''foo'',''bar'');',
  1940. 'end;',
  1941. 'procedure {#B2}Run<T>(a: T; w: word); overload;',
  1942. 'begin',
  1943. 'end;',
  1944. 'procedure {#C2}Run<U>(a: U; b: U); overload;',
  1945. 'begin',
  1946. 'end;',
  1947. 'begin',
  1948. ' {@A}Run(1,true);', // non generic take precedence
  1949. ' {@B}Run(2,word(3));', // non generic take precedence
  1950. ' {@C}Run(''foo'',''bar'');',
  1951. '']);
  1952. ConvertProgram;
  1953. CheckSource('TestGenProc_infer_OverloadForward',
  1954. LinesToStr([ // statements
  1955. 'this.Run$G1 = function (a, b) {',
  1956. ' $mod.Run$G1(1, true);',
  1957. ' $mod.Run$1G1(2, 3);',
  1958. ' $mod.Run$2G1("foo", "bar");',
  1959. '};',
  1960. 'this.Run$1G1 = function (a, w) {',
  1961. '};',
  1962. 'this.Run$2G1 = function (a, b) {',
  1963. '};',
  1964. '']),
  1965. LinesToStr([ // $mod.$main
  1966. '$mod.Run$G1(1, true);',
  1967. '$mod.Run$1G1(2, 3);',
  1968. '$mod.Run$2G1("foo", "bar");',
  1969. '']));
  1970. end;
  1971. procedure TTestGenerics.TestGenProc_TypeInfo;
  1972. begin
  1973. WithTypeInfo:=true;
  1974. StartProgram(true,[supTypeInfo]);
  1975. Add([
  1976. '{$modeswitch implicitfunctionspecialization}',
  1977. 'generic procedure Run<S>(a: S);',
  1978. 'var',
  1979. ' p: TTypeInfo;',
  1980. 'begin',
  1981. ' p:=TypeInfo(S);',
  1982. ' p:=TypeInfo(a);',
  1983. 'end;',
  1984. 'begin',
  1985. ' Run(word(3));',
  1986. ' Run(''foo'');',
  1987. '']);
  1988. ConvertProgram;
  1989. CheckSource('TestGenProc_TypeInfo',
  1990. LinesToStr([ // statements
  1991. 'this.Run$G1 = function (a) {',
  1992. ' var p = null;',
  1993. ' p = rtl.word;',
  1994. ' p = rtl.word;',
  1995. '};',
  1996. 'this.Run$G2 = function (a) {',
  1997. ' var p = null;',
  1998. ' p = rtl.string;',
  1999. ' p = rtl.string;',
  2000. '};',
  2001. '']),
  2002. LinesToStr([ // $mod.$main
  2003. '$mod.Run$G1(3);',
  2004. '$mod.Run$G2("foo");',
  2005. '']));
  2006. end;
  2007. procedure TTestGenerics.TestGenProc_Infer_Widen;
  2008. begin
  2009. StartProgram(false);
  2010. Add([
  2011. '{$mode delphi}',
  2012. 'procedure Run<S>(a: S; b: S);',
  2013. 'begin',
  2014. 'end;',
  2015. 'begin',
  2016. ' Run(word(1),longint(2));',
  2017. ' Run(byte(2),smallint(2));',
  2018. ' Run(longword(3),longint(2));',
  2019. ' Run(nativeint(4),longint(2));',
  2020. ' Run(nativeint(5),nativeuint(2));',
  2021. ' Run(''a'',''foo'');',
  2022. ' Run(''bar'',''c'');',
  2023. '']);
  2024. ConvertProgram;
  2025. CheckSource('TestGenProc_Infer_Widen',
  2026. LinesToStr([ // statements
  2027. 'this.Run$G1 = function (a, b) {',
  2028. '};',
  2029. 'this.Run$G2 = function (a, b) {',
  2030. '};',
  2031. 'this.Run$G3 = function (a, b) {',
  2032. '};',
  2033. '']),
  2034. LinesToStr([ // $mod.$main
  2035. '$mod.Run$G1(1, 2);',
  2036. '$mod.Run$G1(2, 2);',
  2037. '$mod.Run$G2(3, 2);',
  2038. '$mod.Run$G2(4, 2);',
  2039. '$mod.Run$G2(5, 2);',
  2040. '$mod.Run$G3("a", "foo");',
  2041. '$mod.Run$G3("bar", "c");',
  2042. '']));
  2043. end;
  2044. procedure TTestGenerics.TestGenProc_Infer_PassAsArg;
  2045. begin
  2046. StartProgram(false);
  2047. Add([
  2048. '{$mode delphi}',
  2049. 'function Run<T>(a: T): T;',
  2050. 'var b: T;',
  2051. 'begin',
  2052. ' Run(Run<word>(3));',
  2053. ' Run(Run(word(4)));',
  2054. 'end;',
  2055. 'begin',
  2056. ' Run(Run<word>(5));',
  2057. ' Run(Run(word(6)));',
  2058. '']);
  2059. ConvertProgram;
  2060. CheckSource('TestGenProc_Infer_PassAsArg',
  2061. LinesToStr([ // statements
  2062. 'this.Run$G1 = function (a) {',
  2063. ' var Result = 0;',
  2064. ' var b = 0;',
  2065. ' $mod.Run$G1($mod.Run$G1(3));',
  2066. ' $mod.Run$G1($mod.Run$G1(4));',
  2067. ' return Result;',
  2068. '};',
  2069. '']),
  2070. LinesToStr([ // $mod.$main
  2071. '$mod.Run$G1($mod.Run$G1(5));',
  2072. '$mod.Run$G1($mod.Run$G1(6));',
  2073. '']));
  2074. end;
  2075. procedure TTestGenerics.TestGenMethod_ObjFPC;
  2076. begin
  2077. StartProgram(false);
  2078. Add([
  2079. '{$mode objfpc}',
  2080. '{$ModeSwitch implicitfunctionspecialization}',
  2081. 'type',
  2082. ' TObject = class',
  2083. ' generic procedure {#A}Run<S>(a: S; b: boolean); overload;',
  2084. ' generic procedure {#B}Run<T>(a: T; w: word); overload;',
  2085. ' generic procedure {#C}Run<U>(a: U; b: U); overload;',
  2086. ' end; ',
  2087. 'generic procedure {#A2}TObject.Run<S>(a: S; b: boolean); overload;',
  2088. 'begin',
  2089. ' {@A}Run(1,true);', // non generic take precedence
  2090. ' {@B}Run(2,word(3));', // non generic take precedence
  2091. ' {@C}Run(''foo'',''bar'');',
  2092. 'end;',
  2093. 'generic procedure {#B2}TObject.Run<T>(a: T; w: word); overload;',
  2094. 'begin',
  2095. 'end;',
  2096. 'generic procedure {#C2}TObject.Run<U>(a: U; b: U); overload;',
  2097. 'begin',
  2098. 'end;',
  2099. 'var o: TObject;',
  2100. 'begin',
  2101. ' o.{@A}Run(1,true);', // non generic take precedence
  2102. ' o.{@B}Run(2,word(3));', // non generic take precedence
  2103. ' o.{@C}Run(''foo'',''bar'');',
  2104. '']);
  2105. ConvertProgram;
  2106. CheckSource('TestGenMethod_ObjFPC',
  2107. LinesToStr([ // statements
  2108. 'rtl.createClass(this, "TObject", null, function () {',
  2109. ' this.$init = function () {',
  2110. ' };',
  2111. ' this.$final = function () {',
  2112. ' };',
  2113. ' this.Run$G1 = function (a, b) {',
  2114. ' this.Run$G1(1, true);',
  2115. ' this.Run$1G1(2, 3);',
  2116. ' this.Run$2G1("foo", "bar");',
  2117. ' };',
  2118. ' this.Run$1G1 = function (a, w) {',
  2119. ' };',
  2120. ' this.Run$2G1 = function (a, b) {',
  2121. ' };',
  2122. '});',
  2123. 'this.o = null;',
  2124. '']),
  2125. LinesToStr([ // $mod.$main
  2126. '$mod.o.Run$G1(1, true);',
  2127. '$mod.o.Run$1G1(2, 3);',
  2128. '$mod.o.Run$2G1("foo", "bar");',
  2129. '']));
  2130. end;
  2131. procedure TTestGenerics.TestGen_Array_OtherUnit;
  2132. begin
  2133. WithTypeInfo:=true;
  2134. StartProgram(true,[supTObject]);
  2135. AddModuleWithIntfImplSrc('UnitA.pas',
  2136. LinesToStr([
  2137. 'type',
  2138. ' generic TDyn<T> = array of T;',
  2139. ' generic TStatic<T> = array[1..2] of T;',
  2140. '']),
  2141. '');
  2142. AddModuleWithIntfImplSrc('UnitB.pas',
  2143. LinesToStr([
  2144. 'uses UnitA;',
  2145. 'type',
  2146. ' TAnt = class end;',
  2147. ' TAntArray = specialize TDyn<TAnt>;',
  2148. 'procedure Run;',
  2149. '']),
  2150. LinesToStr([
  2151. 'procedure Run;',
  2152. 'begin',
  2153. ' if typeinfo(TAntArray)=nil then ;',
  2154. 'end;',
  2155. '']));
  2156. Add([
  2157. 'uses UnitB;',
  2158. 'begin',
  2159. ' Run;',
  2160. '']);
  2161. ConvertProgram;
  2162. CheckUnit('UnitA.pas',
  2163. LinesToStr([ // statements
  2164. 'rtl.module("UnitA", ["system"], function () {',
  2165. ' var $mod = this;',
  2166. ' this.$rtti.$DynArray("TDyn<UnitB.TAnt>", {});',
  2167. '});']));
  2168. CheckUnit('UnitB.pas',
  2169. LinesToStr([ // statements
  2170. 'rtl.module("UnitB", ["system", "UnitA"], function () {',
  2171. ' var $mod = this;',
  2172. ' rtl.createClass(this, "TAnt", pas.system.TObject, function () {',
  2173. ' });',
  2174. ' this.Run = function () {',
  2175. ' if (pas.UnitA.$rtti["TDyn<UnitB.TAnt>"] === null) ;',
  2176. ' };',
  2177. '});']));
  2178. CheckSource('TestGen_Array_OtherUnit',
  2179. LinesToStr([ // statements
  2180. 'pas.UnitA.$rtti["TDyn<UnitB.TAnt>"].eltype = pas.UnitB.$rtti["TAnt"];',
  2181. '']),
  2182. LinesToStr([ // $mod.$main
  2183. ' pas.UnitB.Run();',
  2184. '']));
  2185. end;
  2186. procedure TTestGenerics.TestGen_ArrayOfUnitImplRec;
  2187. begin
  2188. WithTypeInfo:=true;
  2189. StartProgram(true,[supTObject]);
  2190. AddModuleWithIntfImplSrc('UnitA.pas',
  2191. LinesToStr([
  2192. 'type',
  2193. ' generic TDyn<T> = array of T;',
  2194. ' generic TStatic<T> = array[1..2] of T;',
  2195. '']),
  2196. LinesToStr([
  2197. 'type',
  2198. ' TBird = record',
  2199. ' b: word;',
  2200. ' end;',
  2201. ' TAnt = class end;',
  2202. ' TAntArray = specialize TDyn<TAnt>;',
  2203. 'var',
  2204. ' d: specialize TDyn<TBird>;',
  2205. ' s: specialize TStatic<TBird>;',
  2206. ' p: pointer;',
  2207. 'begin',
  2208. ' d[0].b:=s[1].b;',
  2209. ' s:=s;',
  2210. ' p:=typeinfo(TAntArray);',
  2211. '']));
  2212. Add([
  2213. 'uses UnitA;',
  2214. 'begin',
  2215. '']);
  2216. ConvertProgram;
  2217. CheckUnit('UnitA.pas',
  2218. LinesToStr([ // statements
  2219. 'rtl.module("UnitA", ["system"], function () {',
  2220. ' var $mod = this;',
  2221. ' var $impl = $mod.$impl;',
  2222. ' this.$rtti.$DynArray("TDyn<UnitA.TAnt>", {});',
  2223. ' this.$rtti.$DynArray("TDyn<UnitA.TBird>", {});',
  2224. ' this.TStatic$G1$clone = function (a) {',
  2225. ' var r = [];',
  2226. ' for (var i = 0; i < 2; i++) r.push($impl.TBird.$clone(a[i]));',
  2227. ' return r;',
  2228. ' };',
  2229. ' this.$rtti.$StaticArray("TStatic<UnitA.TBird>", {',
  2230. ' dims: [2]',
  2231. ' });',
  2232. ' $mod.$implcode = function () {',
  2233. ' rtl.recNewT($impl, "TBird", function () {',
  2234. ' this.b = 0;',
  2235. ' this.$eq = function (b) {',
  2236. ' return this.b === b.b;',
  2237. ' };',
  2238. ' this.$assign = function (s) {',
  2239. ' this.b = s.b;',
  2240. ' return this;',
  2241. ' };',
  2242. ' var $r = $mod.$rtti.$Record("TBird", {});',
  2243. ' $r.addField("b", rtl.word);',
  2244. ' });',
  2245. ' rtl.createClass($impl, "TAnt", pas.system.TObject, function () {',
  2246. ' });',
  2247. ' $impl.d = [];',
  2248. ' $impl.s = rtl.arraySetLength(null, $impl.TBird, 2);',
  2249. ' $impl.p = null;',
  2250. ' };',
  2251. ' $mod.$init = function () {',
  2252. ' $impl.d[0].b = $impl.s[0].b;',
  2253. ' $impl.s = $mod.TStatic$G1$clone($impl.s);',
  2254. ' $impl.p = $mod.$rtti["TDyn<UnitA.TAnt>"];',
  2255. ' };',
  2256. '}, []);']));
  2257. CheckSource('TestGen_ArrayOfUnitImplRec',
  2258. LinesToStr([ // statements
  2259. 'pas.UnitA.$rtti["TDyn<UnitA.TAnt>"].eltype = pas.UnitA.$rtti["TAnt"];',
  2260. 'pas.UnitA.$rtti["TDyn<UnitA.TBird>"].eltype = pas.UnitA.$rtti["TBird"];',
  2261. 'pas.UnitA.$rtti["TStatic<UnitA.TBird>"].eltype = pas.UnitA.$rtti["TBird"];',
  2262. '']),
  2263. LinesToStr([ // $mod.$main
  2264. '']));
  2265. end;
  2266. procedure TTestGenerics.TestGen_ProcType_ProcLocal;
  2267. begin
  2268. StartProgram(false);
  2269. Add([
  2270. 'procedure Fly(w: word);',
  2271. 'begin',
  2272. 'end;',
  2273. 'procedure Run(w: word);',
  2274. 'type generic TProc<T> = procedure(a: T);',
  2275. 'var p: specialize TProc<word>;',
  2276. 'begin',
  2277. ' p:=@Fly;',
  2278. ' p(w);',
  2279. 'end;',
  2280. 'begin',
  2281. 'end.']);
  2282. ConvertProgram;
  2283. CheckSource('TestGen_ProcType_ProcLocal',
  2284. LinesToStr([ // statements
  2285. 'this.Fly = function (w) {',
  2286. '};',
  2287. 'this.Run = function (w) {',
  2288. ' var p = null;',
  2289. ' p = $mod.Fly;',
  2290. ' p(w);',
  2291. '};',
  2292. '']),
  2293. LinesToStr([ // $mod.$main
  2294. '']));
  2295. end;
  2296. procedure TTestGenerics.TestGen_ProcType_Local_RTTI_Fail;
  2297. begin
  2298. WithTypeInfo:=true;
  2299. StartProgram(false);
  2300. Add([
  2301. 'procedure Fly(w: word);',
  2302. 'begin',
  2303. 'end;',
  2304. 'procedure Run(w: word);',
  2305. 'type generic TProc<T> = procedure(a: T);',
  2306. 'var',
  2307. ' p: specialize TProc<word>;',
  2308. ' t: Pointer;',
  2309. 'begin',
  2310. ' p:=@Fly;',
  2311. ' p(w);',
  2312. ' t:=typeinfo(p);',
  2313. 'end;',
  2314. 'begin',
  2315. 'end.']);
  2316. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  2317. ConvertProgram;
  2318. end;
  2319. procedure TTestGenerics.TestGen_ProcType_ParamUnitImpl;
  2320. begin
  2321. WithTypeInfo:=true;
  2322. StartProgram(true,[supTObject]);
  2323. AddModuleWithIntfImplSrc('UnitA.pas',
  2324. LinesToStr([
  2325. 'type',
  2326. ' generic TAnt<T> = function(const a: T): T;',
  2327. '']),
  2328. LinesToStr([
  2329. 'type',
  2330. ' TBird = record',
  2331. ' b: word;',
  2332. ' end;',
  2333. 'var',
  2334. ' f: specialize TAnt<TBird>;',
  2335. ' b: TBird;',
  2336. ' p: pointer;',
  2337. 'begin',
  2338. ' b:=f(b);',
  2339. ' p:=typeinfo(f);',
  2340. '']));
  2341. Add([
  2342. 'uses UnitA;',
  2343. 'begin',
  2344. 'end.']);
  2345. ConvertProgram;
  2346. CheckUnit('UnitA.pas',
  2347. LinesToStr([ // statements
  2348. 'rtl.module("UnitA", ["system"], function () {',
  2349. ' var $mod = this;',
  2350. ' var $impl = $mod.$impl;',
  2351. ' this.$rtti.$ProcVar("TAnt<UnitA.TBird>", {',
  2352. ' init: function () {',
  2353. ' this.procsig = rtl.newTIProcSig([["a", $mod.$rtti["TBird"], 2]], $mod.$rtti["TBird"]);',
  2354. ' }',
  2355. ' });',
  2356. ' $mod.$implcode = function () {',
  2357. ' rtl.recNewT($impl, "TBird", function () {',
  2358. ' this.b = 0;',
  2359. ' this.$eq = function (b) {',
  2360. ' return this.b === b.b;',
  2361. ' };',
  2362. ' this.$assign = function (s) {',
  2363. ' this.b = s.b;',
  2364. ' return this;',
  2365. ' };',
  2366. ' var $r = $mod.$rtti.$Record("TBird", {});',
  2367. ' $r.addField("b", rtl.word);',
  2368. ' });',
  2369. ' $impl.f = null;',
  2370. ' $impl.b = $impl.TBird.$new();',
  2371. ' $impl.p = null;',
  2372. ' };',
  2373. ' $mod.$init = function () {',
  2374. ' $impl.b.$assign($impl.f($impl.b));',
  2375. ' $impl.p = $mod.$rtti["TAnt<UnitA.TBird>"];',
  2376. ' };',
  2377. '}, []);']));
  2378. CheckSource('TestGen_Class_ClassVarRecord_UnitImpl',
  2379. LinesToStr([ // statements
  2380. 'pas.UnitA.$rtti["TAnt<UnitA.TBird>"].init();',
  2381. '']),
  2382. LinesToStr([ // $mod.$main
  2383. '']));
  2384. end;
  2385. Initialization
  2386. RegisterTests([TTestGenerics]);
  2387. end.