tcresolvegenerics.pas 70 KB

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