tcresolvegenerics.pas 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906
  1. unit tcresolvegenerics;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, testregistry, tcresolver, PasResolveEval, PParser;
  6. type
  7. { TTestResolveGenerics }
  8. TTestResolveGenerics = Class(TCustomTestResolver)
  9. Published
  10. // generic types
  11. procedure TestGen_MissingTemplateFail;
  12. procedure TestGen_VarTypeWithoutSpecializeFail;
  13. procedure TestGen_GenTypeWithWrongParamCountFail;
  14. procedure TestGen_GenericNotFoundFail;
  15. procedure TestGen_SameNameSameParamCountFail;
  16. procedure TestGen_TypeAliasWithoutSpecializeFail;
  17. procedure TestGen_TemplNameEqTypeNameFail; // type T<T>
  18. // constraints
  19. procedure TestGen_ConstraintStringFail;
  20. procedure TestGen_ConstraintMultiClassFail;
  21. procedure TestGen_ConstraintRecordExpectedFail;
  22. procedure TestGen_ConstraintClassRecordFail;
  23. procedure TestGen_ConstraintRecordClassFail;
  24. procedure TestGen_ConstraintArrayFail;
  25. procedure TestGen_ConstraintConstructor;
  26. procedure TestGen_ConstraintUnit;
  27. // ToDo: constraint T:Unit2.specialize TGen<word>
  28. procedure TestGen_ConstraintSpecialize;
  29. procedure TestGen_ConstraintTSpecializeWithT;
  30. procedure TestGen_ConstraintTSpecializeAsTFail; // TBird<T; U: T<word>> and no T<>
  31. procedure TestGen_ConstraintTSpecializeWithTFail; // TBird<T: TAnt<T>>
  32. procedure TestGen_ConstraintSameNameFail; // TAnt<T:T>
  33. procedure TestGen_ConstraintInheritedMissingRecordFail;
  34. procedure TestGen_ConstraintInheritedMissingClassTypeFail;
  35. procedure TestGen_ConstraintMultiParam;
  36. procedure TestGen_ConstraintMultiParamClassMismatch;
  37. procedure TestGen_ConstraintClassType_DotIsAsTypeCast;
  38. procedure TestGen_ConstraintClassType_ForInT;
  39. procedure TestGen_ConstraintClassType_IsAs;
  40. // generic record
  41. procedure TestGen_RecordLocalNameDuplicateFail;
  42. procedure TestGen_Record;
  43. procedure TestGen_RecordDelphi;
  44. procedure TestGen_RecordNestedSpecialize_ClassRecord;
  45. procedure TestGen_RecordNestedSpecialize_Self;
  46. procedure TestGen_Record_SpecializeSelfInsideFail;
  47. procedure TestGen_Record_ReferGenericSelfFail;
  48. procedure TestGen_RecordAnoArray;
  49. // ToDo: unitname.specialize TBird<word>.specialize TAnt<word>
  50. // generic class
  51. procedure TestGen_Class;
  52. procedure TestGen_ClassDelphi;
  53. procedure TestGen_ClassDelphi_TypeOverload;
  54. procedure TestGen_ClassObjFPC;
  55. procedure TestGen_ClassObjFPC_OverloadFail;
  56. procedure TestGen_ClassObjFPC_OverloadOtherUnit;
  57. procedure TestGen_ClassForward;
  58. procedure TestGen_ClassForwardConstraints;
  59. procedure TestGen_ClassForwardConstraintNameMismatch;
  60. procedure TestGen_ClassForwardConstraintKeywordMismatch;
  61. procedure TestGen_ClassForwardConstraintTypeMismatch;
  62. procedure TestGen_ClassForward_Circle;
  63. procedure TestGen_Class_RedeclareInUnitImplFail;
  64. procedure TestGen_Class_TypeOverloadInUnitImpl;
  65. procedure TestGen_Class_MethodObjFPC;
  66. procedure TestGen_Class_MethodOverride;
  67. procedure TestGen_Class_MethodDelphi;
  68. procedure TestGen_Class_MethodDelphiTypeParamMissing;
  69. procedure TestGen_Class_MethodImplConstraintFail;
  70. procedure TestGen_Class_MethodImplTypeParamNameMismatch;
  71. procedure TestGen_Class_SpecializeSelfInside;
  72. procedure TestGen_Class_AncestorTFail;
  73. procedure TestGen_Class_GenAncestor;
  74. procedure TestGen_Class_AncestorSelfFail;
  75. procedure TestGen_ClassOfSpecializeFail;
  76. // ToDo: UnitA.impl uses UnitB.intf uses UnitA.intf, UnitB has specialize of UnitA
  77. procedure TestGen_Class_NestedType;
  78. procedure TestGen_Class_NestedRecord;
  79. procedure TestGen_Class_NestedClass;
  80. procedure TestGen_Class_Enums_NotPropagating;
  81. procedure TestGen_Class_Self;
  82. procedure TestGen_Class_MemberTypeConstructor;
  83. procedure TestGen_Class_AliasMemberType;
  84. procedure TestGen_Class_AccessGenericMemberTypeFail;
  85. procedure TestGen_Class_ReferenceTo; // ToDo
  86. procedure TestGen_Class_List;
  87. // ToDo: different modeswitches at parse time and specialize time
  88. // generic external class
  89. procedure TestGen_ExtClass_Array;
  90. procedure TestGen_ExtClass_VarargsOfType;
  91. // generic interface
  92. procedure TestGen_ClassInterface;
  93. procedure TestGen_ClassInterface_Method;
  94. // generic array
  95. procedure TestGen_DynArray;
  96. procedure TestGen_StaticArray;
  97. procedure TestGen_Array_Anoynmous;
  98. // generic procedure type
  99. procedure TestGen_ProcType;
  100. procedure TestGen_ProcType_AnonymousFunc_Delphi;
  101. // pointer of generic
  102. procedure TestGen_PointerDirectSpecializeFail;
  103. // ToDo: helpers for generics
  104. procedure TestGen_HelperForArray;
  105. // ToDo: default class prop array helper: arr<b>[c]
  106. // generic statements
  107. procedure TestGen_LocalVar;
  108. procedure TestGen_Statements;
  109. procedure TestGen_InlineSpecializeExpr;
  110. // ToDo: a.b<c>(d)
  111. // ToDo: with a do b<c>
  112. procedure TestGen_TryExcept;
  113. procedure TestGen_Call;
  114. procedure TestGen_NestedProc;
  115. // ToDo: obj<b>[c]
  116. // generic functions
  117. procedure TestGenProc_Function;
  118. procedure TestGenProc_FunctionDelphi;
  119. procedure TestGenProc_OverloadDuplicate;
  120. procedure TestGenProc_MissingTemplatesFail;
  121. procedure TestGenProc_Forward;
  122. procedure TestGenProc_External;
  123. procedure TestGenProc_UnitIntf;
  124. procedure TestGenProc_BackRef1Fail;
  125. procedure TestGenProc_BackRef2Fail;
  126. procedure TestGenProc_BackRef3Fail;
  127. procedure TestGenProc_CallSelf;
  128. procedure TestGenProc_CallSelfNoParams;
  129. procedure TestGenProc_ForwardConstraints;
  130. procedure TestGenProc_ForwardConstraintsRepeatFail;
  131. procedure TestGenProc_ForwardTempNameMismatch;
  132. procedure TestGenProc_ForwardOverload;
  133. procedure TestGenProc_NestedFail;
  134. procedure TestGenProc_TypeParamCntOverload;
  135. procedure TestGenProc_TypeParamCntOverloadNoParams;
  136. procedure TestGenProc_TypeParamWithDefaultParamDelphiFail;
  137. procedure TestGenProc_ParamSpecWithT; // ToDo: Func<T>(Bird: TBird<T>)
  138. // ToDo: NestedResultAssign
  139. // generic function infer types
  140. procedure TestGenProc_Infer_NeedExplicitFail;
  141. procedure TestGenProc_Infer_Overload;
  142. procedure TestGenProc_Infer_OverloadForward;
  143. procedure TestGenProc_Infer_Var_Overload;
  144. procedure TestGenProc_Infer_Widen;
  145. procedure TestGenProc_Infer_DefaultValue;
  146. procedure TestGenProc_Infer_DefaultValueMismatch;
  147. procedure TestGenProc_Infer_ProcT;
  148. procedure TestGenProc_Infer_Mismatch;
  149. procedure TestGenProc_Infer_ArrayOfT;
  150. procedure TestGenProc_Infer_PassAsArgDelphi;
  151. procedure TestGenProc_Infer_PassAsArgObjFPC;
  152. // ToDo procedure TestGenProc_Infer_ProcType;
  153. // generic methods
  154. procedure TestGenMethod_VirtualFail;
  155. procedure TestGenMethod_PublishedFail;
  156. procedure TestGenMethod_ClassInterfaceMethodFail;
  157. procedure TestGenMethod_ClassConstructorFail;
  158. procedure TestGenMethod_TemplNameDifferFail;
  159. procedure TestGenMethod_ImplConstraintFail;
  160. procedure TestGenMethod_NestedSelf;
  161. procedure TestGenMethod_OverloadTypeParamCntObjFPC;
  162. procedure TestGenMethod_OverloadTypeParamCntDelphi;
  163. procedure TestGenMethod_OverloadArgs;
  164. end;
  165. implementation
  166. { TTestResolveGenerics }
  167. procedure TTestResolveGenerics.TestGen_MissingTemplateFail;
  168. begin
  169. StartProgram(false);
  170. Add([
  171. 'type generic g< > = array of word;',
  172. 'begin',
  173. '']);
  174. CheckParserException('Expected "Identifier"',nParserExpectTokenError);
  175. end;
  176. procedure TTestResolveGenerics.TestGen_VarTypeWithoutSpecializeFail;
  177. begin
  178. StartProgram(false);
  179. Add([
  180. 'type generic TBird<T> = record end;',
  181. 'var b: TBird;',
  182. 'begin',
  183. '']);
  184. CheckResolverException('Generics without specialization cannot be used as a type for a variable',
  185. nGenericsWithoutSpecializationAsType);
  186. end;
  187. procedure TTestResolveGenerics.TestGen_GenTypeWithWrongParamCountFail;
  188. begin
  189. StartProgram(false);
  190. Add([
  191. 'type generic TBird<T> = record end;',
  192. 'var b: specialize TBird<word, byte>;',
  193. 'begin',
  194. '']);
  195. CheckResolverException('identifier not found "TBird<,>"',
  196. nIdentifierNotFound);
  197. end;
  198. procedure TTestResolveGenerics.TestGen_GenericNotFoundFail;
  199. begin
  200. StartProgram(false);
  201. Add([
  202. '{$mode objfpc}',
  203. 'type',
  204. ' TBird = specialize TAnimal<word>;',
  205. 'begin',
  206. '']);
  207. CheckResolverException('identifier not found "TAnimal<>"',
  208. nIdentifierNotFound);
  209. end;
  210. procedure TTestResolveGenerics.TestGen_SameNameSameParamCountFail;
  211. begin
  212. StartProgram(false);
  213. Add([
  214. '{$mode delphi}',
  215. 'type',
  216. ' TBird<S,T> = record w: T; end;',
  217. ' TBird<X,Y> = record f: X; end;',
  218. 'begin',
  219. '']);
  220. CheckResolverException('Duplicate identifier "TBird" at afile.pp(4,8)',
  221. nDuplicateIdentifier);
  222. end;
  223. procedure TTestResolveGenerics.TestGen_TypeAliasWithoutSpecializeFail;
  224. begin
  225. StartProgram(false);
  226. Add([
  227. '{$mode delphi}',
  228. 'type',
  229. ' TBird<T> = record w: T; end;',
  230. ' TBirdAlias = TBird;',
  231. 'begin',
  232. '']);
  233. CheckResolverException('type expected, but TBird<> found',
  234. nXExpectedButYFound);
  235. end;
  236. procedure TTestResolveGenerics.TestGen_TemplNameEqTypeNameFail;
  237. begin
  238. StartProgram(false);
  239. Add([
  240. '{$mode objfpc}',
  241. 'type',
  242. ' generic TBird<TBird> = record v: T; end;',
  243. 'var r: specialize TBird<word>;',
  244. 'begin',
  245. '']);
  246. CheckResolverException('Duplicate identifier "TBird" at afile.pp(4,16)',
  247. nDuplicateIdentifier);
  248. end;
  249. procedure TTestResolveGenerics.TestGen_ConstraintStringFail;
  250. begin
  251. StartProgram(false);
  252. Add([
  253. 'type generic TRec<T:string> = record end;',
  254. 'begin',
  255. '']);
  256. CheckResolverException('"String" is not a valid constraint',
  257. nXIsNotAValidConstraint);
  258. end;
  259. procedure TTestResolveGenerics.TestGen_ConstraintMultiClassFail;
  260. begin
  261. StartProgram(false);
  262. Add([
  263. '{$mode objfpc}',
  264. 'type',
  265. ' TObject = class end;',
  266. ' TBird = class end;',
  267. ' TBear = class end;',
  268. ' generic TRec<T: TBird, TBear> = record end;',
  269. 'begin',
  270. '']);
  271. CheckResolverException('"TBird" constraint and "TBear" constraint cannot be specified together',
  272. nConstraintXAndConstraintYCannotBeTogether);
  273. end;
  274. procedure TTestResolveGenerics.TestGen_ConstraintRecordExpectedFail;
  275. begin
  276. StartProgram(false);
  277. Add([
  278. '{$mode objfpc}',
  279. 'type',
  280. ' generic TBird<T:record> = record v: T; end;',
  281. 'var r: specialize TBird<word>;',
  282. 'begin',
  283. '']);
  284. CheckResolverException('record type expected, but Word found',
  285. nXExpectedButYFound);
  286. end;
  287. procedure TTestResolveGenerics.TestGen_ConstraintClassRecordFail;
  288. begin
  289. StartProgram(false);
  290. Add([
  291. '{$mode objfpc}',
  292. 'type',
  293. ' TRec = record end;',
  294. ' generic TBird<T:class> = record v: T; end;',
  295. 'var r: specialize TBird<TRec>;',
  296. 'begin',
  297. '']);
  298. CheckResolverException('class type expected, but TRec found',
  299. nXExpectedButYFound);
  300. end;
  301. procedure TTestResolveGenerics.TestGen_ConstraintRecordClassFail;
  302. begin
  303. StartProgram(false);
  304. Add([
  305. '{$mode objfpc}',
  306. 'type',
  307. ' TObject = class end;',
  308. ' generic TBird<T:record> = record v: T; end;',
  309. 'var r: specialize TBird<TObject>;',
  310. 'begin',
  311. '']);
  312. CheckResolverException('record type expected, but TObject found',
  313. nXExpectedButYFound);
  314. end;
  315. procedure TTestResolveGenerics.TestGen_ConstraintArrayFail;
  316. begin
  317. StartProgram(false);
  318. Add([
  319. '{$mode objfpc}',
  320. 'type',
  321. ' TArr = array of word;',
  322. ' generic TBird<T:TArr> = record v: T; end;',
  323. 'begin',
  324. '']);
  325. CheckResolverException('"array of Word" is not a valid constraint',
  326. nXIsNotAValidConstraint);
  327. end;
  328. procedure TTestResolveGenerics.TestGen_ConstraintConstructor;
  329. begin
  330. StartProgram(true,[supTObject]);
  331. Add([
  332. '{$mode objfpc}',
  333. 'type',
  334. ' generic TBird<T:constructor> = class',
  335. ' o: T;',
  336. ' procedure Fly;',
  337. ' end;',
  338. ' TAnt = class end;',
  339. 'var a: specialize TBird<TAnt>;',
  340. 'procedure TBird.Fly;',
  341. 'begin',
  342. ' o:=T.Create;',
  343. 'end;',
  344. 'begin',
  345. '']);
  346. ParseProgram;
  347. end;
  348. procedure TTestResolveGenerics.TestGen_ConstraintUnit;
  349. begin
  350. AddModuleWithIntfImplSrc('unit1.pas',
  351. LinesToStr([
  352. 'type',
  353. ' TBird = class b1: word; end;',
  354. ' generic TAnt<T> = class a1: T; end;',
  355. '']),
  356. LinesToStr([
  357. '']));
  358. StartProgram(true,[supTObject]);
  359. Add([
  360. 'uses unit1;',
  361. 'type',
  362. ' generic TCat<T: unit1.TBird> = class v: T; end;',
  363. ' generic TFish<T: specialize TAnt<word>> = class v: T; end;',
  364. ' TEagle = class(unit1.TBird);',
  365. ' TRedAnt = specialize TAnt<word>;',
  366. 'var',
  367. ' eagle: TEagle;',
  368. ' redant: TRedAnt;',
  369. ' cat: specialize TCat<TEagle>;',
  370. ' fish: specialize TFish<TRedAnt>;',
  371. 'begin',
  372. ' cat.v:=eagle;',
  373. ' fish.v:=redant;',
  374. '']);
  375. ParseProgram;
  376. end;
  377. procedure TTestResolveGenerics.TestGen_ConstraintSpecialize;
  378. begin
  379. StartProgram(false);
  380. Add([
  381. '{$mode objfpc}',
  382. 'type',
  383. ' TObject = class end;',
  384. ' generic TAnt<S> = class m: S; end;',
  385. ' generic TBird<T:specialize TAnt<word>> = class',
  386. ' o: T;',
  387. ' end;',
  388. ' TFireAnt = class(specialize TAnt<word>) end;',
  389. 'var',
  390. ' a: specialize TBird<TFireAnt>;',
  391. ' f: TFireAnt;',
  392. 'begin',
  393. ' a.o:=f;',
  394. '']);
  395. ParseProgram;
  396. end;
  397. procedure TTestResolveGenerics.TestGen_ConstraintTSpecializeWithT;
  398. begin
  399. StartProgram(false);
  400. Add([
  401. '{$mode delphi}',
  402. 'type',
  403. ' TObject = class end;',
  404. ' TAnt<S> = class m: S; end;',
  405. ' TBird<X; Y: TAnt<X>> = class',
  406. ' Ant: Y;',
  407. ' end;',
  408. ' TEagle<X; Y:X> = class',
  409. ' e: Y;',
  410. ' end;',
  411. ' TFireAnt<F> = class(TAnt<F>) end;',
  412. ' TAntWord = TAnt<word>;',
  413. ' TBirdAntWord = TBird<word, TAnt<word>>;',
  414. 'var',
  415. ' a: TAnt<word>;',
  416. ' b: TBird<word, TAntWord>;',
  417. ' c: TBird<TBirdAntWord, TAnt<TBirdAntWord>>;',
  418. ' f: TEagle<TAnt<boolean>, TFireAnt<boolean>>;',
  419. ' fb: TFireAnt<boolean>;',
  420. 'begin',
  421. ' b.Ant:=a;',
  422. ' f.e:=fb;',
  423. '']);
  424. ParseProgram;
  425. end;
  426. procedure TTestResolveGenerics.TestGen_ConstraintTSpecializeAsTFail;
  427. begin
  428. StartProgram(false);
  429. Add([
  430. '{$mode objfpc}',
  431. 'type',
  432. ' TObject = class end;',
  433. // Note: would work if generic T<S> exists
  434. ' generic TBird<T; U: specialize T<word>> = record v: T; end;',
  435. 'begin',
  436. '']);
  437. CheckResolverException('identifier not found "T<>"',nIdentifierNotFound);
  438. end;
  439. procedure TTestResolveGenerics.TestGen_ConstraintTSpecializeWithTFail;
  440. begin
  441. StartProgram(false);
  442. Add([
  443. '{$mode objfpc}',
  444. 'type',
  445. ' TObject = class end;',
  446. ' generic TAnt<S> = class v: S; end;',
  447. ' generic TBird<T: specialize TAnt<T>> = class v: T; end;',
  448. ' TEagle = specialize TBird<specialize TAnt<word>>;',
  449. 'begin',
  450. '']);
  451. CheckResolverException('identifier not found "T"',nIdentifierNotFound);
  452. end;
  453. procedure TTestResolveGenerics.TestGen_ConstraintSameNameFail;
  454. begin
  455. StartProgram(false);
  456. Add([
  457. '{$mode objfpc}',
  458. 'type',
  459. ' TObject = class end;',
  460. ' T = TObject;',
  461. ' generic TAnt<T:T> = record v: word; end;',
  462. 'begin',
  463. '']);
  464. CheckResolverException(sTypeCycleFound,nTypeCycleFound);
  465. end;
  466. procedure TTestResolveGenerics.TestGen_ConstraintInheritedMissingRecordFail;
  467. begin
  468. StartProgram(false);
  469. Add([
  470. '{$mode objfpc}',
  471. 'type',
  472. ' TObject = class end;',
  473. ' generic TBird<T: record> = class v: T; end;',
  474. ' generic TEagle<U> = class(specialize TBird<U>)',
  475. ' end;',
  476. 'begin',
  477. '']);
  478. CheckResolverException('Type parameter "U" is missing constraint "record"',
  479. nTypeParamXIsMissingConstraintY);
  480. end;
  481. procedure TTestResolveGenerics.TestGen_ConstraintInheritedMissingClassTypeFail;
  482. begin
  483. StartProgram(false);
  484. Add([
  485. '{$mode objfpc}',
  486. 'type',
  487. ' TObject = class end;',
  488. ' TAnt = class end;',
  489. ' generic TBird<T: TAnt> = class v: T; end;',
  490. ' generic TEagle<U> = class(specialize TBird<U>)',
  491. ' end;',
  492. 'begin',
  493. '']);
  494. CheckResolverException('Type parameter "U" is not compatible with type "TAnt"',
  495. nTypeParamXIsNotCompatibleWithY);
  496. end;
  497. procedure TTestResolveGenerics.TestGen_ConstraintMultiParam;
  498. begin
  499. StartProgram(false);
  500. Add([
  501. '{$mode objfpc}',
  502. 'type',
  503. ' TObject = class end;',
  504. ' TAnt = class end;',
  505. ' generic TBird<S,T: TAnt> = class',
  506. ' x: S;',
  507. ' y: T;',
  508. ' end;',
  509. ' TRedAnt = class(TAnt) end;',
  510. ' TEagle = specialize TBird<TRedAnt,TAnt>;',
  511. 'begin',
  512. '']);
  513. ParseProgram;
  514. end;
  515. procedure TTestResolveGenerics.TestGen_ConstraintMultiParamClassMismatch;
  516. begin
  517. StartProgram(false);
  518. Add([
  519. '{$mode objfpc}',
  520. 'type',
  521. ' TObject = class end;',
  522. ' TAnt = class end;',
  523. ' TRedAnt = class(TAnt) end;',
  524. ' generic TBird<S,T: TRedAnt> = class',
  525. ' x: S;',
  526. ' y: T;',
  527. ' end;',
  528. ' TEagle = specialize TBird<TRedAnt,TAnt>;',
  529. 'begin',
  530. '']);
  531. CheckResolverException('Incompatible types: got "TAnt" expected "TRedAnt"',
  532. nIncompatibleTypesGotExpected);
  533. end;
  534. procedure TTestResolveGenerics.TestGen_ConstraintClassType_DotIsAsTypeCast;
  535. begin
  536. StartProgram(false);
  537. Add([
  538. '{$mode objfpc}',
  539. 'type',
  540. ' TObject = class end;',
  541. ' TAnt = class',
  542. ' procedure Run; external; overload;',
  543. ' end;',
  544. ' TRedAnt = class(TAnt)',
  545. ' procedure Run(w: word); external; overload;',
  546. ' end;',
  547. ' generic TBird<T: TRedAnt> = class',
  548. ' y: T;',
  549. ' procedure Fly;',
  550. ' end;',
  551. ' TFireAnt = class(TRedAnt);',
  552. ' generic TEagle<U: TRedAnt> = class(specialize TBird<U>) end;',
  553. ' TRedEagle = specialize TEagle<TRedAnt>;',
  554. 'procedure TBird.Fly;',
  555. 'var f: TFireAnt;',
  556. 'begin',
  557. ' y.Run;',
  558. ' y.Run(3);',
  559. ' if y is TFireAnt then',
  560. ' f:=y as TFireAnt;',
  561. ' f:=TFireAnt(y);',
  562. ' y:=T(f);',
  563. 'end;',
  564. 'begin',
  565. '']);
  566. ParseProgram;
  567. end;
  568. procedure TTestResolveGenerics.TestGen_ConstraintClassType_ForInT;
  569. begin
  570. StartProgram(false);
  571. Add([
  572. '{$mode objfpc}',
  573. 'type',
  574. ' TObject = class end;',
  575. ' generic TEnumerator<TItem> = class',
  576. ' FCurrent: TItem;',
  577. ' property Current: TItem read FCurrent;',
  578. ' function MoveNext: boolean;',
  579. ' end;',
  580. ' generic TAnt<U> = class',
  581. ' function GetEnumerator: specialize TEnumerator<U>;',
  582. ' end;',
  583. ' generic TBird<S; T: specialize TAnt<S>> = class',
  584. ' m: T;',
  585. ' procedure Fly;',
  586. ' end;',
  587. 'function TEnumerator.MoveNext: boolean;',
  588. 'begin',
  589. 'end;',
  590. 'function TAnt.GetEnumerator: specialize TEnumerator<U>;',
  591. 'begin',
  592. 'end;',
  593. 'procedure TBird.Fly;',
  594. 'var i: S;',
  595. 'begin',
  596. ' for i in m do ;',
  597. 'end;',
  598. 'var',
  599. ' a: specialize TAnt<word>;',
  600. ' w: word;',
  601. ' b: specialize TBird<word,specialize TAnt<word>>;',
  602. 'begin',
  603. ' for w in a do ;',
  604. ' for w in b.m do ;',
  605. '']);
  606. ParseProgram;
  607. end;
  608. procedure TTestResolveGenerics.TestGen_ConstraintClassType_IsAs;
  609. begin
  610. StartProgram(false);
  611. Add([
  612. '{$mode objfpc}',
  613. 'type',
  614. ' TObject = class end;',
  615. ' generic TAnt<U> = class',
  616. ' v: U;',
  617. ' function Run: U;',
  618. ' end;',
  619. 'function TAnt.Run: U;',
  620. 'var a: specialize TAnt<U>;',
  621. 'begin',
  622. ' if v is TObject then ;',
  623. ' if v is specialize TAnt<TObject> then',
  624. ' specialize TAnt<TObject>(v).v:=nil;',
  625. ' a:=v as specialize TAnt<U>;',
  626. ' if (v as specialize TAnt<TObject>).v=nil then ;',
  627. ' if nil=(v as specialize TAnt<TObject>).v then ;',
  628. 'end;',
  629. 'begin',
  630. '']);
  631. ParseProgram;
  632. end;
  633. procedure TTestResolveGenerics.TestGen_RecordLocalNameDuplicateFail;
  634. begin
  635. StartProgram(false);
  636. Add([
  637. '{$mode objfpc}',
  638. 'type',
  639. ' generic TBird<T> = record T: word; end;',
  640. 'begin',
  641. '']);
  642. CheckResolverException('Duplicate identifier "T" at afile.pp(4,18)',
  643. nDuplicateIdentifier);
  644. end;
  645. procedure TTestResolveGenerics.TestGen_Record;
  646. begin
  647. StartProgram(false);
  648. Add([
  649. '{$mode objfpc}',
  650. 'type',
  651. ' {#Typ}T = word;',
  652. ' generic TRec<{#Templ}T> = record',
  653. ' {=Templ}v: T;',
  654. ' end;',
  655. 'var',
  656. ' r: specialize TRec<word>;',
  657. ' {=Typ}w: T;',
  658. 'begin',
  659. ' r.v:=w;',
  660. '']);
  661. ParseProgram;
  662. end;
  663. procedure TTestResolveGenerics.TestGen_RecordDelphi;
  664. begin
  665. StartProgram(false);
  666. Add([
  667. '{$mode delphi}',
  668. 'type',
  669. ' {#Typ}T = word;',
  670. ' TRec<{#Templ}T> = record',
  671. ' {=Templ}v: T;',
  672. ' end;',
  673. 'var',
  674. ' r: TRec<word>;',
  675. ' {=Typ}w: T;',
  676. 'begin',
  677. ' r.v:=w;',
  678. '']);
  679. ParseProgram;
  680. end;
  681. procedure TTestResolveGenerics.TestGen_RecordNestedSpecialize_ClassRecord;
  682. begin
  683. StartProgram(false);
  684. Add([
  685. '{$mode objfpc}',
  686. 'type',
  687. ' TObject = class end;',
  688. ' generic TBird<T> = class v: T; end;',
  689. ' generic TFish<T:class> = record v: T; end;',
  690. 'var f: specialize TFish<specialize TBird<word>>;',
  691. 'begin',
  692. '']);
  693. ParseProgram;
  694. end;
  695. procedure TTestResolveGenerics.TestGen_RecordNestedSpecialize_Self;
  696. begin
  697. StartProgram(false);
  698. Add([
  699. '{$mode objfpc}',
  700. 'type',
  701. ' generic TBird<T> = record v: T; end;',
  702. 'var',
  703. ' a: specialize TBird<specialize TBird<word>>;',
  704. 'begin',
  705. ' a.v.v:=3;',
  706. '']);
  707. ParseProgram;
  708. end;
  709. procedure TTestResolveGenerics.TestGen_Record_SpecializeSelfInsideFail;
  710. begin
  711. StartProgram(false);
  712. Add([
  713. '{$mode objfpc}',
  714. 'type',
  715. ' generic TBird<T> = record',
  716. ' v: specialize TBird<word>;',
  717. ' end;',
  718. 'begin',
  719. '']);
  720. CheckResolverException('type "TBird<>" is not yet completely defined',
  721. nTypeXIsNotYetCompletelyDefined);
  722. end;
  723. procedure TTestResolveGenerics.TestGen_Record_ReferGenericSelfFail;
  724. begin
  725. StartProgram(false);
  726. Add([
  727. '{$mode delphi}',
  728. 'Type',
  729. ' TBird<T> = record',
  730. ' b: TBird<T>;',
  731. ' end;',
  732. 'begin',
  733. '']);
  734. CheckResolverException('type "TBird<>" is not yet completely defined',
  735. nTypeXIsNotYetCompletelyDefined);
  736. end;
  737. procedure TTestResolveGenerics.TestGen_RecordAnoArray;
  738. begin
  739. StartProgram(false);
  740. Add([
  741. '{$mode objfpc}',
  742. 'type',
  743. ' generic TBird<T> = record v: T; end;',
  744. 'var',
  745. ' a: specialize TBird<array of word>;',
  746. ' b: specialize TBird<array of word>;',
  747. 'begin',
  748. ' a:=b;',
  749. '']);
  750. ParseProgram;
  751. end;
  752. procedure TTestResolveGenerics.TestGen_Class;
  753. begin
  754. StartProgram(false);
  755. Add([
  756. '{$mode objfpc}',
  757. 'type',
  758. ' TObject = class end;',
  759. ' {#Typ}T = word;',
  760. ' generic TBird<{#Templ}T> = class',
  761. ' {=Templ}v: T;',
  762. ' end;',
  763. 'var',
  764. ' b: specialize TBird<word>;',
  765. ' {=Typ}w: T;',
  766. 'begin',
  767. ' b.v:=w;',
  768. '']);
  769. ParseProgram;
  770. end;
  771. procedure TTestResolveGenerics.TestGen_ClassDelphi;
  772. begin
  773. StartProgram(false);
  774. Add([
  775. '{$mode delphi}',
  776. 'type',
  777. ' TObject = class end;',
  778. ' {#Typ}T = word;',
  779. ' TBird<{#Templ}T> = class',
  780. ' {=Templ}v: T;',
  781. ' end;',
  782. 'var',
  783. ' b: TBird<word>;',
  784. ' {=Typ}w: T;',
  785. 'begin',
  786. ' b.v:=w;',
  787. '']);
  788. ParseProgram;
  789. end;
  790. procedure TTestResolveGenerics.TestGen_ClassDelphi_TypeOverload;
  791. begin
  792. StartProgram(false);
  793. Add([
  794. '{$mode delphi}',
  795. 'type',
  796. ' TObject = class end;',
  797. ' {#a}TBird = word;',
  798. ' {#b}TBird<T> = class',
  799. ' v: T;',
  800. ' end;',
  801. ' {=b}TEagle = TBird<word>;',
  802. 'var',
  803. ' b: {@b}TBird<word>;',
  804. ' {=a}w: TBird;',
  805. 'begin',
  806. ' b.v:=w;',
  807. '']);
  808. ParseProgram;
  809. end;
  810. procedure TTestResolveGenerics.TestGen_ClassObjFPC;
  811. begin
  812. StartProgram(false);
  813. Add([
  814. '{$mode objfpc}',
  815. 'type',
  816. ' TObject = class end;',
  817. ' generic TBird<T> = class',
  818. ' v: TBird;',
  819. ' end;',
  820. 'var',
  821. ' b: specialize TBird<word>;',
  822. 'begin',
  823. ' b.v:=b;',
  824. '']);
  825. ParseProgram;
  826. end;
  827. procedure TTestResolveGenerics.TestGen_ClassObjFPC_OverloadFail;
  828. begin
  829. StartProgram(false);
  830. Add([
  831. '{$mode objfpc}',
  832. 'type',
  833. ' TObject = class end;',
  834. ' TBird = word;',
  835. ' generic TBird<T> = class',
  836. ' v: T;',
  837. ' end;',
  838. 'begin',
  839. '']);
  840. CheckResolverException('Duplicate identifier "TBird" at afile.pp(5,8)',nDuplicateIdentifier);
  841. end;
  842. procedure TTestResolveGenerics.TestGen_ClassObjFPC_OverloadOtherUnit;
  843. begin
  844. AddModuleWithIntfImplSrc('unit1.pas',
  845. LinesToStr([
  846. 'type',
  847. ' TBird = class b1: word; end;',
  848. ' generic TAnt<T> = class a1: T; end;',
  849. '']),
  850. LinesToStr([
  851. '']));
  852. AddModuleWithIntfImplSrc('unit2.pas',
  853. LinesToStr([
  854. 'type',
  855. ' generic TBird<T> = class b2:T; end;',
  856. ' TAnt = class a2:word; end;',
  857. '']),
  858. LinesToStr([
  859. '']));
  860. StartProgram(true,[supTObject]);
  861. Add([
  862. 'uses unit1, unit2;',
  863. 'var',
  864. ' b1: TBird;',
  865. ' b2: specialize TBird<word>;',
  866. ' a1: specialize TAnt<word>;',
  867. ' a2: TAnt;',
  868. 'begin',
  869. ' b1.b1:=1;',
  870. ' b2.b2:=2;',
  871. ' a1.a1:=3;',
  872. ' a2.a2:=4;',
  873. '']);
  874. ParseProgram;
  875. end;
  876. procedure TTestResolveGenerics.TestGen_ClassForward;
  877. begin
  878. StartProgram(false);
  879. Add([
  880. '{$mode objfpc}',
  881. 'type',
  882. ' TObject = class end;',
  883. ' {#Typ}T = word;',
  884. ' generic TBird<{#Templ_Forward}T> = class;',
  885. ' TRec = record',
  886. ' b: specialize TBird<T>;',
  887. ' end;',
  888. ' generic TBird<{#Templ}T> = class',
  889. ' {=Templ}v: T;',
  890. ' r: TRec;',
  891. ' end;',
  892. 'var',
  893. ' s: TRec;',
  894. ' {=Typ}w: T;',
  895. 'begin',
  896. ' s.b.v:=w;',
  897. ' s.b.r:=s;',
  898. '']);
  899. ParseProgram;
  900. end;
  901. procedure TTestResolveGenerics.TestGen_ClassForwardConstraints;
  902. begin
  903. StartProgram(false);
  904. Add([
  905. '{$mode objfpc}',
  906. 'type',
  907. ' TObject = class end;',
  908. ' TAnt = class end;',
  909. ' generic TBird<T: class; U; V: TAnt> = class;',
  910. ' TRec = record',
  911. ' b: specialize TBird<TAnt,word,TAnt>;',
  912. ' end;',
  913. ' generic TBird<T: class; U; V: TAnt> = class',
  914. ' i: U;',
  915. ' r: TRec;',
  916. ' end;',
  917. 'var',
  918. ' s: TRec;',
  919. ' w: word;',
  920. 'begin',
  921. ' s.b.i:=w;',
  922. ' s.b.r:=s;',
  923. '']);
  924. ParseProgram;
  925. end;
  926. procedure TTestResolveGenerics.TestGen_ClassForwardConstraintNameMismatch;
  927. begin
  928. StartProgram(false);
  929. Add([
  930. '{$mode objfpc}',
  931. 'type',
  932. ' TObject = class end;',
  933. ' generic TBird<T> = class;',
  934. ' generic TBird<U> = class',
  935. ' i: U;',
  936. ' end;',
  937. 'begin',
  938. '']);
  939. CheckResolverException('Declaration of "U" differs from previous declaration at afile.pp(5,18)',
  940. nDeclOfXDiffersFromPrevAtY);
  941. end;
  942. procedure TTestResolveGenerics.TestGen_ClassForwardConstraintKeywordMismatch;
  943. begin
  944. StartProgram(false);
  945. Add([
  946. '{$mode objfpc}',
  947. 'type',
  948. ' TObject = class end;',
  949. ' generic TBird<T: class, constructor> = class;',
  950. ' generic TBird<U: class> = class',
  951. ' i: U;',
  952. ' end;',
  953. 'begin',
  954. '']);
  955. CheckResolverException('Declaration of "U" differs from previous declaration at afile.pp(5,18)',
  956. nDeclOfXDiffersFromPrevAtY);
  957. end;
  958. procedure TTestResolveGenerics.TestGen_ClassForwardConstraintTypeMismatch;
  959. begin
  960. StartProgram(false);
  961. Add([
  962. '{$mode objfpc}',
  963. 'type',
  964. ' TObject = class end;',
  965. ' TAnt = class end;',
  966. ' TFish = class end;',
  967. ' generic TBird<T: TAnt> = class;',
  968. ' generic TBird<T: TFish> = class',
  969. ' i: U;',
  970. ' end;',
  971. 'begin',
  972. '']);
  973. CheckResolverException('Declaration of "T" differs from previous declaration at afile.pp(7,18)',
  974. nDeclOfXDiffersFromPrevAtY);
  975. end;
  976. procedure TTestResolveGenerics.TestGen_ClassForward_Circle;
  977. begin
  978. StartProgram(false);
  979. Add([
  980. '{$mode objfpc}',
  981. 'type',
  982. ' TObject = class end;',
  983. ' generic TAnt<T> = class;',
  984. ' generic TFish<U> = class',
  985. ' private type AliasU = U;',
  986. ' var a: specialize TAnt<AliasU>;',
  987. ' Size: AliasU;',
  988. ' end;',
  989. ' generic TAnt<T> = class',
  990. ' private type AliasT = T;',
  991. ' var f: specialize TFish<AliasT>;',
  992. ' Speed: AliasT;',
  993. ' end;',
  994. 'var',
  995. ' WordFish: specialize TFish<word>;',
  996. ' BoolAnt: specialize TAnt<boolean>;',
  997. ' w: word;',
  998. ' b: boolean;',
  999. 'begin',
  1000. ' WordFish.Size:=w;',
  1001. ' WordFish.a.Speed:=w;',
  1002. ' WordFish.a.f.Size:=w;',
  1003. ' BoolAnt.Speed:=b;',
  1004. ' BoolAnt.f.Size:=b;',
  1005. ' BoolAnt.f.a.Speed:=b;',
  1006. '']);
  1007. ParseProgram;
  1008. end;
  1009. procedure TTestResolveGenerics.TestGen_Class_RedeclareInUnitImplFail;
  1010. begin
  1011. StartUnit(false);
  1012. Add([
  1013. 'interface',
  1014. 'type',
  1015. ' TObject = class end;',
  1016. ' generic TBird<T> = class v: T; end;',
  1017. 'implementation',
  1018. 'type generic TBird<T> = record v: T; end;',
  1019. '']);
  1020. CheckResolverException('Duplicate identifier "TBird" at afile.pp(5,16)',
  1021. nDuplicateIdentifier);
  1022. end;
  1023. procedure TTestResolveGenerics.TestGen_Class_TypeOverloadInUnitImpl;
  1024. begin
  1025. StartUnit(false);
  1026. Add([
  1027. 'interface',
  1028. 'type',
  1029. ' TObject = class end;',
  1030. ' generic TBird<T> = class v: T; end;',
  1031. 'implementation',
  1032. 'type generic TBird<T,U> = record x: T; y: U; end;',
  1033. '']);
  1034. ParseUnit;
  1035. end;
  1036. procedure TTestResolveGenerics.TestGen_Class_MethodObjFPC;
  1037. begin
  1038. StartProgram(false);
  1039. Add([
  1040. '{$mode objfpc}',
  1041. 'type',
  1042. ' TObject = class end;',
  1043. ' {#Typ}T = word;',
  1044. ' generic TBird<{#Templ}T> = class',
  1045. ' function Fly(p:T): T; virtual; abstract;',
  1046. ' function Run(p:T): T;',
  1047. ' procedure Jump(p:T);',
  1048. ' class procedure Go(p:T);',
  1049. ' end;',
  1050. 'function TBird.Run(p:T): T;',
  1051. 'begin',
  1052. 'end;',
  1053. 'generic procedure TBird<T>.Jump(p:T);',
  1054. 'begin',
  1055. 'end;',
  1056. 'generic class procedure TBird<T>.Go(p:T);',
  1057. 'begin',
  1058. 'end;',
  1059. 'var',
  1060. ' b: specialize TBird<word>;',
  1061. ' {=Typ}w: T;',
  1062. 'begin',
  1063. ' w:=b.Fly(w);',
  1064. ' w:=b.Run(w);',
  1065. '']);
  1066. ParseProgram;
  1067. end;
  1068. procedure TTestResolveGenerics.TestGen_Class_MethodOverride;
  1069. begin
  1070. StartProgram(false);
  1071. Add([
  1072. '{$mode objfpc}',
  1073. 'type',
  1074. ' TObject = class end;',
  1075. ' generic TBird<T> = class',
  1076. ' function Fly(p:T): T; virtual; abstract;',
  1077. ' end;',
  1078. ' generic TEagle<S> = class(specialize TBird<S>)',
  1079. ' function Fly(p:S): S; override;',
  1080. ' end;',
  1081. 'function TEagle.Fly(p:S): S;',
  1082. 'begin',
  1083. 'end;',
  1084. 'var',
  1085. ' e: specialize TEagle<word>;',
  1086. ' w: word;',
  1087. 'begin',
  1088. ' w:=e.Fly(w);',
  1089. '']);
  1090. ParseProgram;
  1091. end;
  1092. procedure TTestResolveGenerics.TestGen_Class_MethodDelphi;
  1093. begin
  1094. StartProgram(false);
  1095. Add([
  1096. '{$mode delphi}',
  1097. 'type',
  1098. ' TObject = class end;',
  1099. ' {#Typ}T = word;',
  1100. ' TBird<{#Templ}T> = class',
  1101. ' function Fly(p:T): T; virtual; abstract;',
  1102. ' function Run(p:T): T;',
  1103. ' end;',
  1104. 'function TBird<T>.Run(p:T): T;',
  1105. 'begin',
  1106. 'end;',
  1107. 'var',
  1108. ' b: TBird<word>;',
  1109. ' {=Typ}w: T;',
  1110. 'begin',
  1111. ' w:=b.Fly(w);',
  1112. ' w:=b.Run(w);',
  1113. '']);
  1114. ParseProgram;
  1115. end;
  1116. procedure TTestResolveGenerics.TestGen_Class_MethodDelphiTypeParamMissing;
  1117. begin
  1118. StartProgram(false);
  1119. Add([
  1120. '{$mode delphi}',
  1121. 'type',
  1122. ' TObject = class end;',
  1123. ' TBird<T> = class',
  1124. ' function Run(p:T): T;',
  1125. ' end;',
  1126. 'function TBird.Run(p:T): T;',
  1127. 'begin',
  1128. 'end;',
  1129. 'begin',
  1130. '']);
  1131. CheckResolverException('TBird<> expected, but TBird found',nXExpectedButYFound);
  1132. end;
  1133. procedure TTestResolveGenerics.TestGen_Class_MethodImplConstraintFail;
  1134. begin
  1135. StartProgram(false);
  1136. Add([
  1137. '{$mode delphi}',
  1138. 'type',
  1139. ' TObject = class end;',
  1140. ' TBird<T: record> = class',
  1141. ' function Run(p:T): T;',
  1142. ' end;',
  1143. 'function TBird<T: record>.Run(p:T): T;',
  1144. 'begin',
  1145. 'end;',
  1146. 'begin',
  1147. '']);
  1148. CheckResolverException('illegal qualifier ":" after "T"',nIllegalQualifierAfter);
  1149. end;
  1150. procedure TTestResolveGenerics.TestGen_Class_MethodImplTypeParamNameMismatch;
  1151. begin
  1152. StartProgram(false);
  1153. Add([
  1154. '{$mode delphi}',
  1155. 'type',
  1156. ' TObject = class end;',
  1157. ' TBird<T> = class',
  1158. ' procedure DoIt;',
  1159. ' end;',
  1160. 'procedure TBird<S>.DoIt;',
  1161. 'begin',
  1162. 'end;',
  1163. 'begin',
  1164. '']);
  1165. CheckResolverException('T expected, but S found',nXExpectedButYFound);
  1166. end;
  1167. procedure TTestResolveGenerics.TestGen_Class_SpecializeSelfInside;
  1168. begin
  1169. StartProgram(false);
  1170. Add([
  1171. '{$mode objfpc}',
  1172. 'type',
  1173. ' TObject = class end;',
  1174. ' generic TBird<T> = class',
  1175. ' e: T;',
  1176. ' v: specialize TBird<boolean>;',
  1177. ' end;',
  1178. 'var',
  1179. ' b: specialize TBird<word>;',
  1180. ' w: word;',
  1181. 'begin',
  1182. ' b.e:=w;',
  1183. ' if b.v.e then ;',
  1184. '']);
  1185. ParseProgram;
  1186. end;
  1187. procedure TTestResolveGenerics.TestGen_Class_AncestorTFail;
  1188. begin
  1189. StartProgram(false);
  1190. Add([
  1191. '{$mode objfpc}',
  1192. 'type',
  1193. ' TObject = class end;',
  1194. ' TBird = class end;',
  1195. ' generic TFish<T: TBird> = class(T)',
  1196. ' v: T;',
  1197. ' end;',
  1198. 'begin',
  1199. '']);
  1200. CheckResolverException('class type expected, but T found',nXExpectedButYFound);
  1201. end;
  1202. procedure TTestResolveGenerics.TestGen_Class_GenAncestor;
  1203. begin
  1204. StartProgram(false);
  1205. Add([
  1206. '{$mode objfpc}',
  1207. 'type',
  1208. ' TObject = class end;',
  1209. ' generic TBird<T> = class',
  1210. ' i: T;',
  1211. ' end;',
  1212. ' generic TEagle<T> = class(specialize TBird<T>)',
  1213. ' j: T;',
  1214. ' end;',
  1215. 'var',
  1216. ' e: specialize TEagle<word>;',
  1217. 'begin',
  1218. ' e.i:=e.j;',
  1219. '']);
  1220. ParseProgram;
  1221. end;
  1222. procedure TTestResolveGenerics.TestGen_Class_AncestorSelfFail;
  1223. begin
  1224. StartProgram(false);
  1225. Add([
  1226. '{$mode objfpc}',
  1227. 'type',
  1228. ' TObject = class end;',
  1229. ' generic TBird<T> = class(specialize TBird<word>)',
  1230. ' e: T;',
  1231. ' end;',
  1232. 'var',
  1233. ' b: specialize TBird<word>;',
  1234. 'begin',
  1235. '']);
  1236. CheckResolverException('type "TBird<>" is not yet completely defined',nTypeXIsNotYetCompletelyDefined);
  1237. end;
  1238. procedure TTestResolveGenerics.TestGen_ClassOfSpecializeFail;
  1239. begin
  1240. StartProgram(false);
  1241. Add([
  1242. '{$mode objfpc}',
  1243. 'type',
  1244. ' TObject = class end;',
  1245. ' generic TBird<T> = class',
  1246. ' e: T;',
  1247. ' end;',
  1248. ' TBirdClass = class of specialize TBird<word>;',
  1249. 'begin',
  1250. '']);
  1251. CheckParserException('Expected "Identifier" at token "specialize" in file afile.pp at line 8 column 25',nParserExpectTokenError);
  1252. end;
  1253. procedure TTestResolveGenerics.TestGen_Class_NestedType;
  1254. begin
  1255. StartProgram(false);
  1256. Add([
  1257. '{$mode objfpc}',
  1258. 'type',
  1259. ' TObject = class end;',
  1260. ' generic TBird<T> = class',
  1261. ' public type',
  1262. ' TArrayEvent = reference to procedure(El: T);',
  1263. ' public',
  1264. ' p: TArrayEvent;',
  1265. ' end;',
  1266. ' TBirdWord = specialize TBird<word>;',
  1267. 'var',
  1268. ' b: TBirdWord;',
  1269. 'begin',
  1270. ' b.p:=procedure(El: word) begin end;']);
  1271. ParseProgram;
  1272. end;
  1273. procedure TTestResolveGenerics.TestGen_Class_NestedRecord;
  1274. begin
  1275. StartProgram(false);
  1276. Add([
  1277. '{$mode objfpc}',
  1278. '{$modeswitch advancedrecords}',
  1279. 'type',
  1280. ' TObject = class end;',
  1281. ' generic TBird<T> = class',
  1282. ' public type TWing = record',
  1283. ' s: T;',
  1284. ' function GetIt: T;',
  1285. ' end;',
  1286. ' public',
  1287. ' w: TWing;',
  1288. ' end;',
  1289. ' TBirdWord = specialize TBird<word>;',
  1290. 'function TBird.TWing.GetIt: T;',
  1291. 'begin',
  1292. 'end;',
  1293. 'var',
  1294. ' b: TBirdWord;',
  1295. ' i: word;',
  1296. 'begin',
  1297. ' b.w.s:=i;',
  1298. ' i:=b.w.GetIt;',
  1299. '']);
  1300. ParseProgram;
  1301. end;
  1302. procedure TTestResolveGenerics.TestGen_Class_NestedClass;
  1303. begin
  1304. StartProgram(false);
  1305. Add([
  1306. '{$mode objfpc}',
  1307. 'type',
  1308. ' TObject = class end;',
  1309. ' generic TBird<T> = class',
  1310. ' public type TWing = class',
  1311. ' s: T;',
  1312. ' function GetIt: T;',
  1313. ' end;',
  1314. ' public',
  1315. ' w: TWing;',
  1316. ' end;',
  1317. ' TBirdWord = specialize TBird<word>;',
  1318. 'function TBird.TWing.GetIt: T;',
  1319. 'begin',
  1320. 'end;',
  1321. 'var',
  1322. ' b: TBirdWord;',
  1323. ' i: word;',
  1324. 'begin',
  1325. ' b.w.s:=3;',
  1326. ' i:=b.w.GetIt;',
  1327. '']);
  1328. ParseProgram;
  1329. end;
  1330. procedure TTestResolveGenerics.TestGen_Class_Enums_NotPropagating;
  1331. begin
  1332. StartProgram(false);
  1333. Add([
  1334. '{$mode objfpc}',
  1335. 'type',
  1336. ' TObject = class end;',
  1337. ' generic TBird<T> = class',
  1338. ' public type',
  1339. ' TEnum = (red, blue);',
  1340. ' const',
  1341. ' e = blue;',
  1342. ' end;',
  1343. 'const',
  1344. ' r = red;',
  1345. 'begin']);
  1346. CheckResolverException('identifier not found "red"',nIdentifierNotFound);
  1347. end;
  1348. procedure TTestResolveGenerics.TestGen_Class_Self;
  1349. begin
  1350. StartProgram(false);
  1351. Add([
  1352. '{$mode objfpc}',
  1353. 'type',
  1354. ' TObject = class',
  1355. ' end;',
  1356. ' generic TAnimal<T> = class end;',
  1357. ' generic TBird<T> = class(specialize TAnimal<T>)',
  1358. ' function GetObj: TObject;',
  1359. ' procedure Fly(Obj: TObject); virtual; abstract;',
  1360. ' end;',
  1361. ' TProc = procedure(Obj: TObject) of object;',
  1362. ' TWordBird = specialize TBird<word>;',
  1363. 'function TBird.GetObj: TObject;',
  1364. 'var p: TProc;',
  1365. 'begin',
  1366. ' Result:=Self;',
  1367. ' if Self.GetObj=Result then ;',
  1368. ' Fly(Self);',
  1369. ' p:=@Fly;',
  1370. ' p(Self);',
  1371. 'end;',
  1372. 'begin']);
  1373. ParseProgram;
  1374. end;
  1375. procedure TTestResolveGenerics.TestGen_Class_MemberTypeConstructor;
  1376. begin
  1377. StartProgram(false);
  1378. Add([
  1379. '{$mode delphi}',
  1380. 'type',
  1381. ' TObject = class end;',
  1382. ' TAnimal<A> = class',
  1383. ' end;',
  1384. ' TAnt<L> = class',
  1385. ' constructor Create(A: TAnimal<L>);',
  1386. ' end;',
  1387. ' TBird<T> = class(TAnimal<T>)',
  1388. ' type TMyAnt = TAnt<T>;',
  1389. ' function Fly: TMyAnt;',
  1390. ' end;',
  1391. ' TWordBird = TBird<word>;',
  1392. 'constructor TAnt<L>.Create(A: TAnimal<L>);',
  1393. 'begin',
  1394. 'end;',
  1395. 'function TBird<T>.Fly: TMyAnt;',
  1396. 'begin',
  1397. ' Result:=TMyAnt.Create(Self);',
  1398. 'end;',
  1399. 'begin']);
  1400. ParseProgram;
  1401. end;
  1402. procedure TTestResolveGenerics.TestGen_Class_AliasMemberType;
  1403. begin
  1404. StartProgram(false);
  1405. Add([
  1406. '{$mode objfpc}',
  1407. '{$modeswitch externalclass}',
  1408. 'type',
  1409. ' TObject = class end;',
  1410. '',
  1411. ' generic TBird<T> = class',
  1412. ' public type',
  1413. ' TRun = reference to function (aValue : T) : T;',
  1414. ' end;',
  1415. ' TBirdWord = specialize TBird<Word>;',
  1416. ' TBirdWordRun = TBirdWord.TRun;',
  1417. '',
  1418. ' generic TExt<T> = class external name ''Ext''',
  1419. ' public type',
  1420. ' TRun = reference to function (aValue : T) : T;',
  1421. ' end;',
  1422. ' TExtWord = specialize TExt<Word>;',
  1423. ' TExtWordRun = TExtWord.TRun;',
  1424. 'begin',
  1425. '']);
  1426. ParseProgram;
  1427. end;
  1428. procedure TTestResolveGenerics.TestGen_Class_AccessGenericMemberTypeFail;
  1429. begin
  1430. StartProgram(false);
  1431. Add([
  1432. '{$mode objfpc}',
  1433. 'type',
  1434. ' TObject = class end;',
  1435. '',
  1436. ' generic TBird<T> = class',
  1437. ' public type',
  1438. ' TRun = reference to function (aValue : T) : T;',
  1439. ' end;',
  1440. ' TBirdRun = TBird.TRun;',
  1441. 'begin',
  1442. '']);
  1443. CheckResolverException('Generics without specialization cannot be used as a type for a reference',
  1444. nGenericsWithoutSpecializationAsType);
  1445. end;
  1446. procedure TTestResolveGenerics.TestGen_Class_ReferenceTo;
  1447. begin
  1448. StartProgram(false);
  1449. Add([
  1450. '{$mode objfpc}',
  1451. 'type',
  1452. ' TObject = class end;',
  1453. ' generic TGJSPromise<T> = class',
  1454. ' public type',
  1455. ' TGJSPromiseResolver = reference to function (aValue : T) : T;',
  1456. ' TGJSPromiseExecutor = reference to procedure (resolve,reject : TGJSPromiseResolver);',
  1457. ' public',
  1458. ' constructor new(Executor : TGJSPromiseExecutor);',
  1459. ' end;',
  1460. 'constructor TGJSPromise.new(Executor : TGJSPromiseExecutor);',
  1461. 'begin',
  1462. 'end;',
  1463. '',
  1464. 'type',
  1465. ' TJSPromise = specialize TGJSPromise<Word>;',
  1466. ' TJSPromiseResolver = reference to function (aValue : Word) : Word;',
  1467. '',
  1468. ' TURLLoader = Class(TObject)',
  1469. ' procedure dofetch(resolve, reject: TJSPromiseResolver); virtual; abstract;',
  1470. ' Function fetch : TJSPromise;',
  1471. ' end;',
  1472. 'function TURLLoader.fetch : TJSPromise;',
  1473. 'begin',
  1474. ' Result:=TJSPromise.New(@Dofetch);',
  1475. 'end;',
  1476. 'begin',
  1477. '']);
  1478. ParseProgram;
  1479. end;
  1480. procedure TTestResolveGenerics.TestGen_Class_List;
  1481. begin
  1482. StartProgram(false);
  1483. Add([
  1484. '{$mode objfpc}',
  1485. 'type',
  1486. ' TObject = class end;',
  1487. ' generic TList<T> = class',
  1488. ' strict private',
  1489. ' FItems: array of T;',
  1490. ' function GetItems(Index: longint): T;',
  1491. ' procedure SetItems(Index: longint; Value: T);',
  1492. ' public',
  1493. ' procedure Alter(w: T);',
  1494. ' property Items[Index: longint]: T read GetItems write SetItems; default;',
  1495. ' end;',
  1496. ' TWordList = specialize TList<word>;',
  1497. 'function TList.GetItems(Index: longint): T;',
  1498. 'begin',
  1499. ' Result:=FItems[Index];',
  1500. 'end;',
  1501. 'procedure TList.SetItems(Index: longint; Value: T);',
  1502. 'begin',
  1503. ' FItems[Index]:=Value;',
  1504. 'end;',
  1505. 'procedure TList.Alter(w: T);',
  1506. 'begin',
  1507. ' SetLength(FItems,length(FItems)+1);',
  1508. ' Insert(w,FItems,2);',
  1509. ' Delete(FItems,2,3);',
  1510. 'end;',
  1511. 'var l: TWordList;',
  1512. ' w: word;',
  1513. 'begin',
  1514. ' l[1]:=w;',
  1515. ' w:=l[2];']);
  1516. ParseProgram;
  1517. end;
  1518. procedure TTestResolveGenerics.TestGen_ExtClass_Array;
  1519. begin
  1520. StartProgram(false);
  1521. Add([
  1522. '{$mode delphi}',
  1523. '{$ModeSwitch externalclass}',
  1524. 'type',
  1525. ' NativeInt = longint;',
  1526. ' TJSGenArray<T> = Class external name ''Array''',
  1527. ' private',
  1528. ' function GetElements(Index: NativeInt): T; external name ''[]'';',
  1529. ' procedure SetElements(Index: NativeInt; const AValue: T); external name ''[]'';',
  1530. ' public',
  1531. ' type TSelfType = TJSGenArray<T>;',
  1532. ' TArrayEvent = reference to function(El: T; Arr: TSelfType): Boolean;',
  1533. ' TArrayCallback = TArrayEvent;',
  1534. ' public',
  1535. ' FLength : NativeInt; external name ''length'';',
  1536. ' constructor new; overload;',
  1537. ' constructor new(aLength : NativeInt); overload;',
  1538. ' class function _of() : TSelfType; varargs; external name ''of'';',
  1539. ' function every(const aCallback: TArrayCallBack): boolean; overload;',
  1540. ' function fill(aValue : T) : TSelfType; overload;',
  1541. ' function fill(aValue : T; aStartIndex : NativeInt) : TSelfType; overload;',
  1542. ' function fill(aValue : T; aStartIndex,aEndIndex : NativeInt) : TSelfType; overload;',
  1543. ' property Length : NativeInt Read FLength Write FLength;',
  1544. ' property Elements[Index: NativeInt]: T read GetElements write SetElements; default;',
  1545. ' end;',
  1546. ' TJSWordArray = TJSGenArray<word>;',
  1547. 'var',
  1548. ' wa: TJSWordArray;',
  1549. ' w: word;',
  1550. 'begin',
  1551. ' wa:=TJSWordArray.new;',
  1552. ' wa:=TJSWordArray.new(3);',
  1553. ' wa:=TJSWordArray._of(4,5);',
  1554. ' wa:=wa.fill(7);',
  1555. ' wa:=wa.fill(7,8,9);',
  1556. ' w:=wa.length;',
  1557. ' wa.length:=10;',
  1558. ' wa[11]:=w;',
  1559. ' w:=wa[12];',
  1560. ' wa.every(function(El: word; Arr: TJSWordArray): Boolean',
  1561. ' begin',
  1562. ' end',
  1563. ' );',
  1564. '']);
  1565. ParseProgram;
  1566. end;
  1567. procedure TTestResolveGenerics.TestGen_ExtClass_VarargsOfType;
  1568. begin
  1569. StartProgram(false);
  1570. Add([
  1571. '{$mode objfpc}',
  1572. '{$modeswitch externalclass}',
  1573. 'type',
  1574. ' TJSObject = class external name ''Object''',
  1575. ' end;',
  1576. ' generic TGJSSet<T> = class external name ''Set''',
  1577. ' constructor new(aElement1: T); varargs of T; overload;',
  1578. ' function bind(thisArg: TJSObject): T; varargs of T;',
  1579. ' end;',
  1580. ' TJSWordSet = specialize TGJSSet<word>;',
  1581. 'var',
  1582. ' s: TJSWordSet;',
  1583. ' w: word;',
  1584. 'begin',
  1585. ' s:=TJSWordSet.new(3);',
  1586. ' s:=TJSWordSet.new(3,5);',
  1587. ' w:=s.bind(nil);',
  1588. ' w:=s.bind(nil,6);',
  1589. ' w:=s.bind(nil,7,8);',
  1590. '']);
  1591. ParseProgram;
  1592. end;
  1593. procedure TTestResolveGenerics.TestGen_ClassInterface;
  1594. begin
  1595. StartProgram(false);
  1596. Add([
  1597. 'type',
  1598. ' {$interfaces corba}',
  1599. ' generic ICorbaIntf<T> = interface',
  1600. ' procedure Fly(a: T);',
  1601. ' end;',
  1602. ' {$interfaces com}',
  1603. ' IUnknown = interface',
  1604. ' end;',
  1605. ' IInterface = IUnknown;',
  1606. ' generic IComIntf<T> = interface',
  1607. ' procedure Run(b: T);',
  1608. ' end;',
  1609. 'begin']);
  1610. ParseProgram;
  1611. end;
  1612. procedure TTestResolveGenerics.TestGen_ClassInterface_Method;
  1613. begin
  1614. StartProgram(false);
  1615. Add([
  1616. 'type',
  1617. ' {$interfaces corba}',
  1618. ' generic IBird<T> = interface',
  1619. ' procedure Fly(a: T);',
  1620. ' end;',
  1621. ' TObject = class end;',
  1622. ' generic TBird<U> = class(specialize IBird<U>)',
  1623. ' procedure Fly(a: U);',
  1624. ' end;',
  1625. 'procedure TBird.Fly(a: U);',
  1626. 'begin',
  1627. 'end;',
  1628. 'var b: specialize IBird<word>;',
  1629. 'begin',
  1630. ' b.Fly(3);']);
  1631. ParseProgram;
  1632. end;
  1633. procedure TTestResolveGenerics.TestGen_DynArray;
  1634. begin
  1635. StartProgram(false);
  1636. Add([
  1637. 'type',
  1638. ' generic TArray<T> = array of T;',
  1639. ' TWordArray = specialize TArray<word>;',
  1640. 'var',
  1641. ' a: specialize TArray<word>;',
  1642. ' b: TWordArray;',
  1643. ' w: word;',
  1644. 'begin',
  1645. ' a[1]:=2;',
  1646. ' b[2]:=a[3]+b[4];',
  1647. ' a:=b;',
  1648. ' b:=a;',
  1649. ' SetLength(a,5);',
  1650. ' SetLength(b,6);',
  1651. ' w:=length(a)+low(a)+high(a);',
  1652. '']);
  1653. ParseProgram;
  1654. end;
  1655. procedure TTestResolveGenerics.TestGen_StaticArray;
  1656. begin
  1657. StartProgram(false);
  1658. Add([
  1659. 'type',
  1660. ' generic TBird<T> = array[T] of word;',
  1661. ' TByteBird = specialize TBird<byte>;',
  1662. 'var',
  1663. ' a: specialize TBird<byte>;',
  1664. ' b: TByteBird;',
  1665. ' i: byte;',
  1666. 'begin',
  1667. ' a[1]:=2;',
  1668. ' b[2]:=a[3]+b[4];',
  1669. ' a:=b;',
  1670. ' b:=a;',
  1671. ' i:=low(a);',
  1672. ' i:=high(a);',
  1673. ' for i in a do ;',
  1674. '']);
  1675. ParseProgram;
  1676. end;
  1677. procedure TTestResolveGenerics.TestGen_Array_Anoynmous;
  1678. begin
  1679. StartProgram(false);
  1680. Add([
  1681. 'type',
  1682. ' generic TRec<T> = record',
  1683. ' a: array of T;',
  1684. ' end;',
  1685. ' TWordRec = specialize TRec<word>;',
  1686. 'var',
  1687. ' a: specialize TRec<word>;',
  1688. ' b: TWordRec;',
  1689. ' w: word;',
  1690. 'begin',
  1691. ' a:=b;',
  1692. ' a.a:=b.a;',
  1693. ' a.a[1]:=2;',
  1694. ' b.a[2]:=a.a[3]+b.a[4];',
  1695. ' b:=a;',
  1696. ' SetLength(a.a,5);',
  1697. ' SetLength(b.a,6);',
  1698. ' w:=length(a.a)+low(a.a)+high(a.a);',
  1699. '']);
  1700. ParseProgram;
  1701. end;
  1702. procedure TTestResolveGenerics.TestGen_ProcType;
  1703. begin
  1704. StartProgram(false);
  1705. Add([
  1706. 'type',
  1707. ' generic TFunc<T> = function(v: T): T;',
  1708. ' TWordFunc = specialize TFunc<word>;',
  1709. 'function GetIt(w: word): word;',
  1710. 'begin',
  1711. 'end;',
  1712. 'var',
  1713. ' a: specialize TFunc<word>;',
  1714. ' b: TWordFunc;',
  1715. ' w: word;',
  1716. 'begin',
  1717. ' a:=nil;',
  1718. ' b:=nil;',
  1719. ' a:=b;',
  1720. ' b:=a;',
  1721. ' w:=a(w);',
  1722. ' w:=b(w);',
  1723. ' a:=@GetIt;',
  1724. ' b:=@GetIt;',
  1725. '']);
  1726. ParseProgram;
  1727. end;
  1728. procedure TTestResolveGenerics.TestGen_ProcType_AnonymousFunc_Delphi;
  1729. begin
  1730. StartProgram(false);
  1731. Add([
  1732. '{$mode delphi}',
  1733. 'type',
  1734. ' TObject = class',
  1735. ' end;',
  1736. ' IInterface = interface',
  1737. ' end;',
  1738. ' Integer = longint;',
  1739. ' IComparer<T> = interface',
  1740. ' function Compare(const Left, Right: T): Integer; overload;',
  1741. ' end;',
  1742. ' TOnComparison<T> = function(const Left, Right: T): Integer of object;',
  1743. ' TComparisonFunc<T> = reference to function(const Left, Right: T): Integer;',
  1744. ' TComparer<T> = class(TObject, IComparer<T>)',
  1745. ' public',
  1746. ' function Compare(const Left, Right: T): Integer; overload;',
  1747. ' class function Construct(const AComparison: TOnComparison<T>): IComparer<T>; overload;',
  1748. ' class function Construct(const AComparison: TComparisonFunc<T>): IComparer<T>; overload;',
  1749. ' end;',
  1750. 'function TComparer<T>.Compare(const Left, Right: T): Integer; overload;',
  1751. 'begin',
  1752. 'end;',
  1753. 'class function TComparer<T>.Construct(const AComparison: TOnComparison<T>): IComparer<T>;',
  1754. 'begin',
  1755. 'end;',
  1756. 'class function TComparer<T>.Construct(const AComparison: TComparisonFunc<T>): IComparer<T>;',
  1757. 'begin',
  1758. 'end;',
  1759. 'procedure Test;',
  1760. 'var',
  1761. ' aComparer : IComparer<Integer>;',
  1762. 'begin',
  1763. ' aComparer:=TComparer<Integer>.Construct(function (Const a,b : integer) : integer',
  1764. ' begin',
  1765. ' Result:=a-b;',
  1766. ' end);',
  1767. 'end;',
  1768. 'begin',
  1769. ' Test;']);
  1770. ParseModule;
  1771. end;
  1772. procedure TTestResolveGenerics.TestGen_PointerDirectSpecializeFail;
  1773. begin
  1774. StartProgram(false);
  1775. Add([
  1776. 'type',
  1777. ' generic TRec<T> = record v: T; end;',
  1778. ' PRec = ^specialize TRec<word>;',
  1779. 'begin',
  1780. '']);
  1781. CheckParserException('Expected "Identifier" at token "specialize" in file afile.pp at line 4 column 11',nParserExpectTokenError);
  1782. end;
  1783. procedure TTestResolveGenerics.TestGen_HelperForArray;
  1784. begin
  1785. StartProgram(false);
  1786. Add([
  1787. '{$ModeSwitch typehelpers}',
  1788. 'type',
  1789. ' generic TArr<T> = array[1..2] of T;',
  1790. ' TWordArrHelper = type helper for specialize TArr<word>',
  1791. ' procedure Fly(w: word);',
  1792. ' end;',
  1793. 'procedure TWordArrHelper.Fly(w: word);',
  1794. 'begin',
  1795. 'end;',
  1796. 'var',
  1797. ' a: specialize TArr<word>;',
  1798. 'begin',
  1799. ' a.Fly(3);',
  1800. '']);
  1801. ParseProgram;
  1802. end;
  1803. procedure TTestResolveGenerics.TestGen_LocalVar;
  1804. begin
  1805. StartProgram(false);
  1806. Add([
  1807. '{$mode objfpc}',
  1808. 'type',
  1809. ' TObject = class end;',
  1810. ' generic TBird<{#Templ}T> = class',
  1811. ' function Fly(p:T): T;',
  1812. ' end;',
  1813. 'function TBird.Fly(p:T): T;',
  1814. 'var l: T;',
  1815. 'begin',
  1816. ' l:=p;',
  1817. ' p:=l;',
  1818. ' Result:=p;',
  1819. ' Result:=l;',
  1820. ' l:=Result;',
  1821. 'end;',
  1822. 'var',
  1823. ' b: specialize TBird<word>;',
  1824. ' w: word;',
  1825. 'begin',
  1826. ' w:=b.Fly(w);',
  1827. '']);
  1828. ParseProgram;
  1829. end;
  1830. procedure TTestResolveGenerics.TestGen_Statements;
  1831. begin
  1832. StartProgram(false);
  1833. Add([
  1834. '{$mode objfpc}',
  1835. 'type',
  1836. ' TObject = class end;',
  1837. ' generic TBird<{#Templ}T> = class',
  1838. ' function Fly(p:T): T;',
  1839. ' end;',
  1840. 'function TBird.Fly(p:T): T;',
  1841. 'var',
  1842. ' v1,v2,v3:T;',
  1843. 'begin',
  1844. ' v1:=1;',
  1845. ' v2:=v1+v1*v1+v1 div p;',
  1846. ' v3:=-v1;',
  1847. ' repeat',
  1848. ' v1:=v1+1;',
  1849. ' until v1>=5;',
  1850. ' while v1>=0 do',
  1851. ' v1:=v1-v2;',
  1852. ' for v1:=v2 to v3 do v2:=v1;',
  1853. ' if v1<v2 then v3:=v1 else v3:=v2;',
  1854. ' if v1<v2 then else ;',
  1855. ' case v1 of',
  1856. ' 1: v3:=3;',
  1857. ' end;',
  1858. 'end;',
  1859. 'var',
  1860. ' b: specialize TBird<word>;',
  1861. 'begin',
  1862. ' b.Fly(2);',
  1863. '']);
  1864. ParseProgram;
  1865. end;
  1866. procedure TTestResolveGenerics.TestGen_InlineSpecializeExpr;
  1867. begin
  1868. StartProgram(false);
  1869. Add([
  1870. '{$mode objfpc}',
  1871. 'type',
  1872. ' TObject = class end;',
  1873. ' generic TBird<T> = class',
  1874. ' constructor Create;',
  1875. ' end;',
  1876. ' generic TAnt<U> = class',
  1877. ' constructor Create;',
  1878. ' end;',
  1879. 'constructor TBird.Create;',
  1880. 'var',
  1881. ' a: specialize TAnt<T>;',
  1882. ' b: specialize TAnt<word>;',
  1883. 'begin',
  1884. ' a:=specialize TAnt<T>.create;',
  1885. ' b:=specialize TAnt<word>.create;',
  1886. 'end;',
  1887. 'constructor TAnt.Create;',
  1888. 'var',
  1889. ' i: specialize TBird<U>;',
  1890. ' j: specialize TBird<word>;',
  1891. ' k: specialize TAnt<U>;',
  1892. 'begin',
  1893. ' i:=specialize TBird<U>.create;',
  1894. ' j:=specialize TBird<word>.create;',
  1895. ' k:=specialize TAnt<U>.create;',
  1896. 'end;',
  1897. 'var a: specialize TAnt<word>;',
  1898. 'begin',
  1899. ' a:=specialize TAnt<word>.create;',
  1900. '']);
  1901. ParseProgram;
  1902. end;
  1903. procedure TTestResolveGenerics.TestGen_TryExcept;
  1904. begin
  1905. StartProgram(false);
  1906. Add([
  1907. '{$mode objfpc}',
  1908. 'type',
  1909. ' TObject = class end;',
  1910. ' generic TBird<{#Templ}T> = class',
  1911. ' function Fly(p:T): T;',
  1912. ' end;',
  1913. ' Exception = class',
  1914. ' end;',
  1915. ' generic EMsg<T> = class',
  1916. ' Msg: T;',
  1917. ' end;',
  1918. 'function TBird.Fly(p:T): T;',
  1919. 'var',
  1920. ' v1,v2,v3:T;',
  1921. 'begin',
  1922. ' try',
  1923. ' finally',
  1924. ' end;',
  1925. ' try',
  1926. ' v1:=v2;',
  1927. ' finally',
  1928. ' v2:=v1;',
  1929. ' end;',
  1930. ' try',
  1931. ' except',
  1932. ' on Exception do ;',
  1933. ' on E: Exception do ;',
  1934. ' on E: specialize EMsg<boolean> do E.Msg:=true;',
  1935. ' on E: specialize EMsg<T> do E.Msg:=1;',
  1936. ' end;',
  1937. 'end;',
  1938. 'var',
  1939. ' b: specialize TBird<word>;',
  1940. 'begin',
  1941. ' b.Fly(2);',
  1942. '']);
  1943. ParseProgram;
  1944. end;
  1945. procedure TTestResolveGenerics.TestGen_Call;
  1946. begin
  1947. StartProgram(false);
  1948. Add([
  1949. '{$mode objfpc}',
  1950. 'type',
  1951. ' TObject = class end;',
  1952. ' generic TBird<T> = class',
  1953. ' function Fly(p:T): T;',
  1954. ' end;',
  1955. 'procedure Run(b: boolean); overload;',
  1956. 'begin end;',
  1957. 'procedure Run(w: word); overload;',
  1958. 'begin end;',
  1959. 'function TBird.Fly(p:T): T;',
  1960. 'begin',
  1961. ' Run(p);',
  1962. ' Run(Result);',
  1963. 'end;',
  1964. 'var',
  1965. ' w: specialize TBird<word>;',
  1966. ' b: specialize TBird<boolean>;',
  1967. 'begin',
  1968. '']);
  1969. ParseProgram;
  1970. end;
  1971. procedure TTestResolveGenerics.TestGen_NestedProc;
  1972. begin
  1973. StartProgram(false);
  1974. Add([
  1975. '{$mode objfpc}',
  1976. 'type',
  1977. ' TObject = class end;',
  1978. ' generic TBird<T> = class',
  1979. ' function Fly(p:T): T;',
  1980. ' end;',
  1981. 'function TBird.Fly(p:T): T;',
  1982. ' function Run: T;',
  1983. ' begin',
  1984. ' Fly:=Result;',
  1985. ' end;',
  1986. 'begin',
  1987. ' Run;',
  1988. 'end;',
  1989. 'var',
  1990. ' w: specialize TBird<word>;',
  1991. ' b: specialize TBird<boolean>;',
  1992. 'begin',
  1993. '']);
  1994. ParseProgram;
  1995. end;
  1996. procedure TTestResolveGenerics.TestGenProc_Function;
  1997. begin
  1998. StartProgram(false);
  1999. Add([
  2000. 'generic function DoIt<T>(a: T): T;',
  2001. 'var i: T;',
  2002. 'begin',
  2003. ' a:=i;',
  2004. ' Result:=a;',
  2005. 'end;',
  2006. 'var w: word;',
  2007. 'begin',
  2008. ' w:=specialize DoIt<word>(3);',
  2009. '']);
  2010. ParseProgram;
  2011. end;
  2012. procedure TTestResolveGenerics.TestGenProc_FunctionDelphi;
  2013. begin
  2014. StartProgram(false);
  2015. Add([
  2016. '{$mode delphi}',
  2017. 'function DoIt<T>(a: T): T;',
  2018. 'var i: T;',
  2019. 'begin',
  2020. ' a:=i;',
  2021. ' Result:=a;',
  2022. 'end;',
  2023. 'var w: word;',
  2024. 'begin',
  2025. ' w:=DoIt<word>(3);',
  2026. '']);
  2027. ParseProgram;
  2028. end;
  2029. procedure TTestResolveGenerics.TestGenProc_OverloadDuplicate;
  2030. begin
  2031. StartProgram(false);
  2032. Add([
  2033. 'generic procedure Fly<T>(a: T);',
  2034. 'begin',
  2035. 'end;',
  2036. 'generic procedure Fly<T>(a: T);',
  2037. 'begin',
  2038. 'end;',
  2039. 'begin',
  2040. '']);
  2041. CheckResolverException('Duplicate identifier "Fly" at afile.pp(2,22)',nDuplicateIdentifier);
  2042. end;
  2043. procedure TTestResolveGenerics.TestGenProc_MissingTemplatesFail;
  2044. begin
  2045. StartProgram(false);
  2046. Add([
  2047. 'generic procedure Run;',
  2048. 'begin',
  2049. 'end;',
  2050. 'begin',
  2051. '']);
  2052. CheckParserException('Expected "<"',nParserExpectTokenError);
  2053. end;
  2054. procedure TTestResolveGenerics.TestGenProc_Forward;
  2055. begin
  2056. StartProgram(false);
  2057. Add([
  2058. 'generic procedure Fly<T>(a: T); forward;',
  2059. 'procedure Run;',
  2060. 'begin',
  2061. ' specialize Fly<word>(3);',
  2062. 'end;',
  2063. 'generic procedure Fly<T>(a: T);',
  2064. 'var i: T;',
  2065. 'begin',
  2066. ' i:=a;',
  2067. 'end;',
  2068. 'begin',
  2069. ' specialize Fly<boolean>(true);',
  2070. '']);
  2071. ParseProgram;
  2072. end;
  2073. procedure TTestResolveGenerics.TestGenProc_External;
  2074. begin
  2075. StartProgram(false);
  2076. Add([
  2077. 'generic function Fly<T>(a: T): T; external name ''flap'';',
  2078. 'procedure Run;',
  2079. 'begin',
  2080. ' specialize Fly<word>(3);',
  2081. 'end;',
  2082. 'begin',
  2083. ' specialize Fly<boolean>(true);',
  2084. '']);
  2085. ParseProgram;
  2086. end;
  2087. procedure TTestResolveGenerics.TestGenProc_UnitIntf;
  2088. begin
  2089. AddModuleWithIntfImplSrc('unit2.pas',
  2090. LinesToStr([
  2091. 'generic function Fly<T>(a: T): T;',
  2092. '']),
  2093. LinesToStr([
  2094. 'generic function Fly<T>(a: T): T;',
  2095. 'var i: T;',
  2096. 'begin',
  2097. ' i:=a;',
  2098. 'end;',
  2099. '']));
  2100. StartProgram(true);
  2101. Add([
  2102. 'uses unit2;',
  2103. 'var w: word;',
  2104. 'begin',
  2105. ' w:=specialize Fly<word>(3);',
  2106. ' if specialize Fly<boolean>(false) then ;',
  2107. '']);
  2108. ParseProgram;
  2109. end;
  2110. procedure TTestResolveGenerics.TestGenProc_BackRef1Fail;
  2111. begin
  2112. StartProgram(false);
  2113. Add([
  2114. 'generic function Fly<T>(a: Fly): T;',
  2115. 'begin',
  2116. 'end;',
  2117. 'begin',
  2118. '']);
  2119. CheckResolverException('Wrong number of parameters specified for call to "function Fly<>(untyped)"',nWrongNumberOfParametersForCallTo);
  2120. end;
  2121. procedure TTestResolveGenerics.TestGenProc_BackRef2Fail;
  2122. begin
  2123. StartProgram(false);
  2124. Add([
  2125. 'generic function Fly<T>(a: Fly<word>): T;',
  2126. 'begin',
  2127. 'end;',
  2128. 'begin',
  2129. '']);
  2130. CheckResolverException('Wrong number of parameters specified for call to "function Fly<>(untyped)"',nWrongNumberOfParametersForCallTo);
  2131. end;
  2132. procedure TTestResolveGenerics.TestGenProc_BackRef3Fail;
  2133. begin
  2134. StartProgram(false);
  2135. Add([
  2136. 'generic function Fly<T>(a: Fly<T>): T;',
  2137. 'begin',
  2138. 'end;',
  2139. 'begin',
  2140. '']);
  2141. CheckResolverException('Wrong number of parameters specified for call to "function Fly<>(untyped)"',nWrongNumberOfParametersForCallTo);
  2142. end;
  2143. procedure TTestResolveGenerics.TestGenProc_CallSelf;
  2144. begin
  2145. StartProgram(false);
  2146. Add([
  2147. 'generic function Fly<T>(a: T): T;',
  2148. ' procedure Run;',
  2149. ' begin',
  2150. ' specialize Fly<T>(a);',
  2151. ' specialize Fly<word>(3);',
  2152. ' end;',
  2153. 'begin',
  2154. ' specialize Fly<T>(a);',
  2155. ' specialize Fly<boolean>(true);',
  2156. 'end;',
  2157. 'begin',
  2158. ' specialize Fly<string>(''fast'');',
  2159. '']);
  2160. ParseProgram;
  2161. end;
  2162. procedure TTestResolveGenerics.TestGenProc_CallSelfNoParams;
  2163. begin
  2164. StartProgram(false);
  2165. Add([
  2166. 'generic function Fly<T>(a: T = 0): T;',
  2167. ' procedure Run;',
  2168. ' begin',
  2169. ' specialize Fly<T>;',
  2170. ' specialize Fly<word>;',
  2171. ' end;',
  2172. 'begin',
  2173. ' specialize Fly<T>;',
  2174. ' specialize Fly<byte>;',
  2175. 'end;',
  2176. 'begin',
  2177. ' specialize Fly<shortint>;',
  2178. '']);
  2179. ParseProgram;
  2180. end;
  2181. procedure TTestResolveGenerics.TestGenProc_ForwardConstraints;
  2182. begin
  2183. StartProgram(false);
  2184. Add([
  2185. 'type',
  2186. ' TObject = class end;',
  2187. ' TBird = class end;',
  2188. 'var b: TBird;',
  2189. 'generic function Fly<T: class>(a: T): T; forward;',
  2190. 'procedure Run;',
  2191. 'begin',
  2192. ' specialize Fly<TBird>(b);',
  2193. 'end;',
  2194. 'generic function Fly<T>(a: T): T;',
  2195. 'begin',
  2196. 'end;',
  2197. 'begin',
  2198. ' specialize Fly<TBird>(b);',
  2199. '']);
  2200. ParseProgram;
  2201. end;
  2202. procedure TTestResolveGenerics.TestGenProc_ForwardConstraintsRepeatFail;
  2203. begin
  2204. StartProgram(false);
  2205. Add([
  2206. 'type',
  2207. ' TObject = class end;',
  2208. 'generic function Fly<T: class>(a: T): T; forward;',
  2209. 'generic function Fly<T: class>(a: T): T;',
  2210. 'begin',
  2211. 'end;',
  2212. 'begin',
  2213. '']);
  2214. CheckResolverException(sImplMustNotRepeatConstraints,nImplMustNotRepeatConstraints);
  2215. end;
  2216. procedure TTestResolveGenerics.TestGenProc_ForwardTempNameMismatch;
  2217. begin
  2218. StartProgram(false);
  2219. Add([
  2220. 'generic function Fly<T>(a: T): T; forward;',
  2221. 'generic function Fly<B>(a: B): B;',
  2222. 'begin',
  2223. 'end;',
  2224. 'begin',
  2225. '']);
  2226. CheckResolverException('Declaration of "Fly<B>" differs from previous declaration at afile.pp(2,23)',
  2227. nDeclOfXDiffersFromPrevAtY);
  2228. end;
  2229. procedure TTestResolveGenerics.TestGenProc_ForwardOverload;
  2230. begin
  2231. StartProgram(false);
  2232. Add([
  2233. 'generic function {#FlyA}Fly<T>(a: T; b: boolean): T; forward; overload;',
  2234. 'generic function {#FlyB}Fly<T>(a: T; w: word): T; forward; overload;',
  2235. 'procedure {#FlyC}Fly; overload;',
  2236. 'begin',
  2237. ' specialize {@FlyA}Fly<longint>(1,true);',
  2238. ' specialize {@FlyB}Fly<string>(''ABC'',3);',
  2239. 'end;',
  2240. 'generic function Fly<T>(a: T; b: boolean): T;',
  2241. 'begin',
  2242. 'end;',
  2243. 'generic function Fly<T>(a: T; w: word): T;',
  2244. 'begin',
  2245. 'end;',
  2246. 'begin',
  2247. '']);
  2248. ParseProgram;
  2249. end;
  2250. procedure TTestResolveGenerics.TestGenProc_NestedFail;
  2251. begin
  2252. StartProgram(false);
  2253. Add([
  2254. 'procedure Fly;',
  2255. ' generic procedure Run<T>(a: T);',
  2256. ' begin',
  2257. ' end;',
  2258. 'begin',
  2259. ' Run<boolean>(true);',
  2260. 'end;',
  2261. 'begin',
  2262. '']);
  2263. CheckResolverException('Type parameters not allowed on nested procedure',nTypeParamsNotAllowedOnX);
  2264. end;
  2265. procedure TTestResolveGenerics.TestGenProc_TypeParamCntOverload;
  2266. begin
  2267. StartProgram(false);
  2268. Add([
  2269. 'generic procedure {#A}Run<T>(a: T);',
  2270. 'begin',
  2271. 'end;',
  2272. 'generic procedure {#B}Run<M,N>(a: M);',
  2273. 'begin',
  2274. ' specialize {@A}Run<M>(a);',
  2275. ' specialize {@B}Run<double,char>(1.3);',
  2276. 'end;',
  2277. 'begin',
  2278. ' specialize {@A}Run<word>(3);',
  2279. ' specialize {@B}Run<word,char>(4);',
  2280. '']);
  2281. ParseProgram;
  2282. end;
  2283. procedure TTestResolveGenerics.TestGenProc_TypeParamCntOverloadNoParams;
  2284. begin
  2285. StartProgram(false);
  2286. Add([
  2287. 'generic procedure {#A}Run<T>;',
  2288. 'begin',
  2289. 'end;',
  2290. 'generic procedure {#B}Run<M,N>;',
  2291. 'begin',
  2292. ' specialize {@A}Run<M>;',
  2293. ' specialize {@A}Run<M>();',
  2294. ' specialize {@B}Run<double,char>;',
  2295. ' specialize {@B}Run<double,char>();',
  2296. 'end;',
  2297. 'begin',
  2298. ' specialize {@A}Run<word>;',
  2299. ' specialize {@A}Run<word>();',
  2300. ' specialize {@B}Run<word,char>;',
  2301. ' specialize {@B}Run<word,char>();',
  2302. '']);
  2303. ParseProgram;
  2304. end;
  2305. procedure TTestResolveGenerics.TestGenProc_TypeParamWithDefaultParamDelphiFail;
  2306. begin
  2307. // delphi 10.3 does not allow default values for args with generic types
  2308. StartProgram(false);
  2309. Add([
  2310. '{$mode delphi}',
  2311. 'procedure {#A}Run<T>(a: T = 0); overload;',
  2312. 'begin',
  2313. 'end;',
  2314. 'begin',
  2315. '']);
  2316. CheckResolverException(sParamOfThisTypeCannotHaveDefVal,nParamOfThisTypeCannotHaveDefVal);
  2317. end;
  2318. procedure TTestResolveGenerics.TestGenProc_ParamSpecWithT;
  2319. begin
  2320. StartProgram(false);
  2321. Add([
  2322. '{$mode delphi}',
  2323. 'type',
  2324. ' TObject = class end;',
  2325. ' TBird<T> = class v: T; end;',
  2326. ' TAnt = class',
  2327. ' procedure Func<T: class>(Bird: TBird<T>);',
  2328. ' end;',
  2329. 'procedure TAnt.Func<T>(Bird: TBird<T>);',
  2330. 'begin',
  2331. 'end;',
  2332. 'var',
  2333. ' Ant: TAnt;',
  2334. ' Bird: TBird<TObject>;',
  2335. ' BirdOfBird: TBird<TBird<TObject>>;',
  2336. 'begin',
  2337. ' Ant.Func<TObject>(Bird);',
  2338. ' Ant.Func<TBird<TObject>>(BirdOfBird);',
  2339. '']);
  2340. ParseProgram;
  2341. end;
  2342. procedure TTestResolveGenerics.TestGenProc_Infer_NeedExplicitFail;
  2343. begin
  2344. StartProgram(false);
  2345. Add([
  2346. '{$mode delphi}',
  2347. 'function {#A}Run<S,T>(a: S): T; overload;',
  2348. 'begin',
  2349. 'end;',
  2350. 'begin',
  2351. ' {@A}Run(1);',
  2352. '']);
  2353. CheckResolverException('Could not infer generic type argument "T" for method "Run"',
  2354. nCouldNotInferTypeArgXForMethodY);
  2355. end;
  2356. procedure TTestResolveGenerics.TestGenProc_Infer_Overload;
  2357. begin
  2358. StartProgram(false);
  2359. Add([
  2360. '{$mode delphi}',
  2361. 'procedure {#A}Run<S>(a: S; b: boolean); overload;',
  2362. 'begin',
  2363. 'end;',
  2364. 'procedure {#B}Run<T>(a: T; w: word); overload;',
  2365. 'begin',
  2366. 'end;',
  2367. 'procedure {#C}Run<U>(a: U; b: U); overload;',
  2368. 'begin',
  2369. 'end;',
  2370. 'begin',
  2371. ' {@A}Run(1,true);', // non generic take precedence
  2372. ' {@B}Run(2,word(3));', // non generic take precedence
  2373. ' {@C}Run(''foo'',''bar'');',
  2374. '']);
  2375. ParseProgram;
  2376. end;
  2377. procedure TTestResolveGenerics.TestGenProc_Infer_OverloadForward;
  2378. begin
  2379. StartProgram(false);
  2380. Add([
  2381. '{$mode delphi}',
  2382. 'procedure {#A}Run<S>(a: S; b: boolean); forward; overload;',
  2383. 'procedure {#B}Run<T>(a: T; w: word); forward; overload;',
  2384. 'procedure {#C}Run<U>(a: U; b: U); forward; overload;',
  2385. 'procedure {#A2}Run<S>(a: S; b: boolean); overload;',
  2386. 'begin',
  2387. ' {@A}Run(1,true);', // non generic take precedence
  2388. ' {@B}Run(2,word(3));', // non generic take precedence
  2389. ' {@C}Run(''foo'',''bar'');',
  2390. 'end;',
  2391. 'procedure {#B2}Run<T>(a: T; w: word); overload;',
  2392. 'begin',
  2393. 'end;',
  2394. 'procedure {#C2}Run<U>(a: U; b: U); overload;',
  2395. 'begin',
  2396. 'end;',
  2397. 'begin',
  2398. ' {@A}Run(1,true);', // non generic take precedence
  2399. ' {@B}Run(2,word(3));', // non generic take precedence
  2400. ' {@C}Run(''foo'',''bar'');',
  2401. '']);
  2402. ParseProgram;
  2403. end;
  2404. procedure TTestResolveGenerics.TestGenProc_Infer_Var_Overload;
  2405. begin
  2406. StartProgram(false);
  2407. Add([
  2408. '{$mode delphi}',
  2409. 'procedure {#A}Run<S>(var a: S; var b: boolean); overload;',
  2410. 'begin',
  2411. 'end;',
  2412. 'procedure {#B}Run<T>(var a: T; var w: word); overload;',
  2413. 'begin',
  2414. 'end;',
  2415. 'procedure {#C}Run<U>(var a: U; var b: U); overload;',
  2416. 'begin',
  2417. 'end;',
  2418. 'var',
  2419. ' w: word;',
  2420. ' b: boolean;',
  2421. ' s: string;',
  2422. 'begin',
  2423. ' {@A}Run(w,b);',
  2424. ' {@B}Run(s,w);',
  2425. ' {@C}Run(s,s);',
  2426. '']);
  2427. ParseProgram;
  2428. end;
  2429. procedure TTestResolveGenerics.TestGenProc_Infer_Widen;
  2430. begin
  2431. StartProgram(false);
  2432. Add([
  2433. '{$mode delphi}',
  2434. 'procedure {#A}Run<S>(a: S; b: S);',
  2435. 'begin',
  2436. 'end;',
  2437. 'begin',
  2438. ' {@A}Run(word(1),longint(2));',
  2439. ' {@A}Run(int64(1),longint(2));',
  2440. ' {@A}Run(boolean(false),wordbool(2));',
  2441. ' {@A}Run(''a'',''foo'');',
  2442. '']);
  2443. ParseProgram;
  2444. end;
  2445. procedure TTestResolveGenerics.TestGenProc_Infer_DefaultValue;
  2446. begin
  2447. StartProgram(false);
  2448. Add([
  2449. '{$mode objfpc}',
  2450. '{$modeswitch implicitfunctionspecialization}',
  2451. 'generic procedure {#A}Run<S>(a: S = 2; b: S = 10); overload;',
  2452. 'begin',
  2453. 'end;',
  2454. 'begin',
  2455. ' {@A}Run(1,2);',
  2456. ' {@A}Run(3);',
  2457. ' {@A}Run();',
  2458. '']);
  2459. ParseProgram;
  2460. end;
  2461. procedure TTestResolveGenerics.TestGenProc_Infer_DefaultValueMismatch;
  2462. begin
  2463. StartProgram(false);
  2464. Add([
  2465. '{$mode objfpc}',
  2466. '{$modeswitch implicitfunctionspecialization}',
  2467. 'generic procedure {#A}Run<S>(a: S; b: S = 10); overload;',
  2468. 'begin',
  2469. 'end;',
  2470. 'begin',
  2471. ' {@A}Run(false,true);',
  2472. '']);
  2473. CheckResolverException('Incompatible types: got "Longint" expected "Boolean"',
  2474. nIncompatibleTypesGotExpected);
  2475. end;
  2476. procedure TTestResolveGenerics.TestGenProc_Infer_ProcT;
  2477. begin
  2478. StartProgram(false);
  2479. Add([
  2480. '{$mode delphi}',
  2481. 'type',
  2482. ' TProc<S> = reference to procedure(a: S);',
  2483. ' TObject = class',
  2484. ' procedure {#A}Run<T: class>(a: TProc<T>);',
  2485. ' end;',
  2486. ' TBird = class end;',
  2487. 'procedure Tobject.Run<T>(a: TProc<T>);',
  2488. 'begin',
  2489. 'end;',
  2490. 'var obj: TObject;',
  2491. 'begin',
  2492. ' obj.{@A}Run<TBird>(procedure(Bird: TBird) begin end);',
  2493. //' obj.{@A}Run(procedure(Bird: TBird) begin end);', // not supported by Delphi
  2494. '']);
  2495. ParseProgram;
  2496. end;
  2497. procedure TTestResolveGenerics.TestGenProc_Infer_Mismatch;
  2498. begin
  2499. StartProgram(false);
  2500. Add([
  2501. '{$mode delphi}',
  2502. 'procedure Run<T>(a: T; b: T);',
  2503. 'begin',
  2504. 'end;',
  2505. 'begin',
  2506. ' Run(1,true);',
  2507. '']);
  2508. CheckResolverException('Inferred type "T" from different arguments mismatch for method "Run"',
  2509. nInferredTypeXFromDiffArgsMismatchFromMethodY);
  2510. end;
  2511. procedure TTestResolveGenerics.TestGenProc_Infer_ArrayOfT;
  2512. begin
  2513. StartProgram(false);
  2514. Add([
  2515. '{$mode delphi}',
  2516. 'procedure Run<T>(a: array of T);',
  2517. 'var b: T;',
  2518. 'begin',
  2519. ' b:=3;',
  2520. 'end;',
  2521. 'var Arr: array of byte;',
  2522. 'begin',
  2523. ' Run(Arr);',
  2524. '']);
  2525. ParseProgram;
  2526. end;
  2527. procedure TTestResolveGenerics.TestGenProc_Infer_PassAsArgDelphi;
  2528. begin
  2529. StartProgram(false);
  2530. Add([
  2531. '{$mode delphi}',
  2532. 'function Run<T>(a: T): T;',
  2533. 'var b: T;',
  2534. 'begin',
  2535. ' Run(Run<word>(3));',
  2536. ' Run(Run(4));',
  2537. 'end;',
  2538. 'begin',
  2539. ' Run(Run<word>(5));',
  2540. ' Run(Run(6));',
  2541. '']);
  2542. ParseProgram;
  2543. end;
  2544. procedure TTestResolveGenerics.TestGenProc_Infer_PassAsArgObjFPC;
  2545. begin
  2546. StartProgram(false);
  2547. Add([
  2548. '{$mode objfpc}',
  2549. '{$ModeSwitch implicitfunctionspecialization}',
  2550. 'generic function Run<T>(a: T): T;',
  2551. 'var b: T;',
  2552. 'begin',
  2553. ' Run(specialize Run<word>(3));',
  2554. ' Run(Run(4));',
  2555. 'end;',
  2556. 'begin',
  2557. ' Run(specialize Run<word>(5));',
  2558. ' Run(Run(6));',
  2559. '']);
  2560. ParseProgram;
  2561. end;
  2562. procedure TTestResolveGenerics.TestGenMethod_VirtualFail;
  2563. begin
  2564. StartProgram(false);
  2565. Add([
  2566. 'type',
  2567. ' TObject = class',
  2568. ' generic procedure Run<T>(a: T); virtual; abstract;',
  2569. ' end;',
  2570. 'begin',
  2571. '']);
  2572. CheckResolverException('virtual, dynamic or message methods cannot have type parameters',
  2573. nXMethodsCannotHaveTypeParams);
  2574. end;
  2575. procedure TTestResolveGenerics.TestGenMethod_PublishedFail;
  2576. begin
  2577. StartProgram(false);
  2578. Add([
  2579. 'type',
  2580. ' TObject = class',
  2581. ' published',
  2582. ' generic procedure Run<T>(a: T);',
  2583. ' end;',
  2584. 'generic procedure TObject.Run<T>(a: T);',
  2585. 'begin',
  2586. 'end;',
  2587. 'begin',
  2588. '']);
  2589. CheckResolverException('published methods cannot have type parameters',
  2590. nXMethodsCannotHaveTypeParams);
  2591. end;
  2592. procedure TTestResolveGenerics.TestGenMethod_ClassInterfaceMethodFail;
  2593. begin
  2594. StartProgram(false);
  2595. Add([
  2596. 'type',
  2597. ' IUnknown = interface',
  2598. ' generic procedure Run<T>(a: T); virtual; abstract;',
  2599. ' end;',
  2600. 'begin',
  2601. '']);
  2602. CheckParserException('generic is not allowed in interface',nParserXNotAllowedInY);
  2603. end;
  2604. procedure TTestResolveGenerics.TestGenMethod_ClassConstructorFail;
  2605. begin
  2606. StartProgram(false);
  2607. Add([
  2608. 'type',
  2609. ' TObject = class',
  2610. ' generic class constructor Run<T>(a: T);',
  2611. ' end;',
  2612. 'generic class constructor TObject.Run<T>(a: T);',
  2613. 'begin end;',
  2614. 'begin',
  2615. '']);
  2616. CheckParserException('Expected "Procedure" or "Function" at token "constructor" in file afile.pp at line 4 column 19',
  2617. nParserExpectToken2Error);
  2618. end;
  2619. procedure TTestResolveGenerics.TestGenMethod_TemplNameDifferFail;
  2620. begin
  2621. StartProgram(false);
  2622. Add([
  2623. 'type',
  2624. ' TObject = class',
  2625. ' generic procedure Run<T>(a: T);',
  2626. ' end;',
  2627. 'generic procedure TObject.Run<S>(a: S);',
  2628. 'begin',
  2629. 'end;',
  2630. 'begin',
  2631. '']);
  2632. CheckResolverException('Declaration of "TObject.Run<S>" differs from previous declaration at afile.pp(4,28)',
  2633. nDeclOfXDiffersFromPrevAtY);
  2634. end;
  2635. procedure TTestResolveGenerics.TestGenMethod_ImplConstraintFail;
  2636. begin
  2637. StartProgram(false);
  2638. Add([
  2639. 'type',
  2640. ' TObject = class',
  2641. ' generic procedure Run<T>(a: T);',
  2642. ' end;',
  2643. 'generic procedure TObject.Run<T: class>(a: T);',
  2644. 'begin',
  2645. 'end;',
  2646. 'begin',
  2647. '']);
  2648. CheckResolverException(sImplMustNotRepeatConstraints,nImplMustNotRepeatConstraints);
  2649. end;
  2650. procedure TTestResolveGenerics.TestGenMethod_NestedSelf;
  2651. begin
  2652. StartProgram(false);
  2653. Add([
  2654. 'type',
  2655. ' TObject = class',
  2656. ' w: word;',
  2657. ' generic function Fly<T>(a: T): T;',
  2658. ' end;',
  2659. 'generic function TObject.Fly<T>(a: T): T;',
  2660. ' function Sub: T;',
  2661. ' begin',
  2662. ' Result:=w+a;',
  2663. ' Result:=Self.w+a;',
  2664. //' specialize Fly<T> :=', not supported by FPC/Delphi
  2665. ' end;',
  2666. 'begin',
  2667. ' Result:=Sub;',
  2668. ' Result:=Self.w+Sub+a;',
  2669. 'end;',
  2670. 'var Obj: TObject;',
  2671. 'begin',
  2672. ' if Obj.specialize Fly<smallint>(3)=4 then ;',
  2673. '']);
  2674. ParseProgram;
  2675. end;
  2676. procedure TTestResolveGenerics.TestGenMethod_OverloadTypeParamCntObjFPC;
  2677. begin
  2678. StartProgram(false);
  2679. Add([
  2680. 'type',
  2681. ' TObject = class',
  2682. ' generic procedure {#A}Run<T>(a: T);',
  2683. ' generic procedure {#B}Run<M,N>(a: M);',
  2684. ' end;',
  2685. 'generic procedure TObject.Run<T>(a: T);',
  2686. 'begin',
  2687. 'end;',
  2688. 'generic procedure TObject.Run<M,N>(a: M);',
  2689. 'begin',
  2690. ' specialize {@A}Run<M>(a);',
  2691. ' specialize {@B}Run<double,char>(1.3);',
  2692. 'end;',
  2693. 'var obj: TObject;',
  2694. 'begin',
  2695. ' obj.specialize {@A}Run<word>(3);',
  2696. ' obj.specialize {@B}Run<word,char>(4);',
  2697. '']);
  2698. ParseProgram;
  2699. end;
  2700. procedure TTestResolveGenerics.TestGenMethod_OverloadTypeParamCntDelphi;
  2701. begin
  2702. StartProgram(false);
  2703. Add([
  2704. '{$mode delphi}',
  2705. 'type',
  2706. ' TObject = class',
  2707. ' procedure {#A}Run<T>(a: T); overload;',
  2708. ' procedure {#B}Run<M,N>(a: M); overload;',
  2709. ' end;',
  2710. 'procedure TObject.Run<T>(a: T);',
  2711. 'begin',
  2712. 'end;',
  2713. 'procedure TObject.Run<M,N>(a: M);',
  2714. 'begin',
  2715. ' {@A}Run<M>(a);',
  2716. ' {@B}Run<double,char>(1.3);',
  2717. 'end;',
  2718. 'var obj: TObject;',
  2719. 'begin',
  2720. ' obj.{@A}Run<word>(3);',
  2721. ' obj.{@B}Run<word,char>(4);',
  2722. '']);
  2723. ParseProgram;
  2724. end;
  2725. procedure TTestResolveGenerics.TestGenMethod_OverloadArgs;
  2726. begin
  2727. StartProgram(false);
  2728. Add([
  2729. 'type',
  2730. ' TObject = class',
  2731. ' generic function {#A}Run<T>(a: boolean): T;',
  2732. ' generic function {#B}Run<M>(a: word): M;',
  2733. ' end;',
  2734. 'generic function TObject.Run<T>(a: boolean): T;',
  2735. 'begin',
  2736. 'end;',
  2737. 'generic function TObject.Run<M>(a: word): M;',
  2738. 'begin',
  2739. ' Result:=specialize Run<M>(a);',
  2740. ' if specialize {@A}Run<string>(true)=''foo'' then ;',
  2741. ' if specialize {@B}Run<byte>(3)=4 then ;',
  2742. 'end;',
  2743. 'var obj: TObject;',
  2744. 'begin',
  2745. ' if obj.specialize {@A}Run<string>(true)=''bar'' then ;',
  2746. ' if obj.specialize {@B}Run<byte>(5)=6 then ;',
  2747. '']);
  2748. ParseProgram;
  2749. end;
  2750. initialization
  2751. RegisterTests([TTestResolveGenerics]);
  2752. end.