pgenutil.pas 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064
  1. {
  2. Copyright (c) 2011
  3. Contains different functions that are used in the context of
  4. parsing generics.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pgenutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globtype,
  26. { parser }
  27. pgentype,
  28. { node }
  29. node,
  30. { symtable }
  31. symtype,symdef,symbase;
  32. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string;parsedtype:tdef;const symname:string;parsedpos:tfileposinfo);inline;
  33. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string);inline;
  34. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean):tdef;inline;
  35. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;const symname:string;symtable:tsymtable):tdef;inline;
  36. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;parsedtype:tdef;const symname:string;symtable:tsymtable;parsedpos:tfileposinfo):tdef;
  37. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;const _prettyname:ansistring):tdef;
  38. function check_generic_constraints(genericdef:tstoreddef;paramlist:tfpobjectlist;poslist:tfplist):boolean;
  39. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  40. function parse_generic_specialization_types(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  41. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist;isfwd:boolean);
  42. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  43. function generate_generic_name(const name:tidstring;const specializename:ansistring;const owner_hierarchy:ansistring):tidstring;
  44. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  45. procedure add_generic_dummysym(sym:tsym);
  46. function resolve_generic_dummysym(const name:tidstring):tsym;
  47. function could_be_generic(const name:tidstring):boolean;inline;
  48. function try_implicit_specialization(sym:tsym;para:tnode;pdoverloadlist:tfpobjectlist;var unnamed_syms:tfplist;var first_procsym:tsym;var hasoverload:boolean):boolean;
  49. function finalize_specialization(var pd:tprocdef;spezcontext:tspecializationcontext):boolean;
  50. procedure generate_specialization_procs;
  51. procedure generate_specializations_for_forwarddef(def:tdef);
  52. procedure maybe_add_pending_specialization(def:tdef;unnamed_syms:tfplist);
  53. function determine_generic_def(const name:tidstring):tstoreddef;
  54. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  55. procedure specialization_done(var state:tspecializationstate);
  56. implementation
  57. uses
  58. { common }
  59. cutils,fpchash,
  60. { global }
  61. globals,tokens,verbose,finput,constexp,
  62. { symtable }
  63. symconst,symsym,symtable,defcmp,defutil,procinfo,
  64. { modules }
  65. fmodule,
  66. { node }
  67. nobj,ncon,ncal,
  68. { parser }
  69. scanner,
  70. pbase,pexpr,pdecsub,ptype,psub,pparautl,pdecl,procdefutil;
  71. type
  72. tdeftypeset = set of tdeftyp;
  73. const
  74. tgeneric_param_const_types : tdeftypeset = [orddef,stringdef,floatdef,setdef,pointerdef,enumdef];
  75. tgeneric_param_nodes : tnodetypeset = [typen,ordconstn,stringconstn,realconstn,setconstn,niln];
  76. procedure make_prettystring(paramtype:tdef;first:boolean;constprettyname:ansistring;var prettyname,specializename:ansistring);
  77. var
  78. namepart : string;
  79. prettynamepart : ansistring;
  80. module : tmodule;
  81. begin
  82. if assigned(paramtype.owner) then
  83. module:=find_module_from_symtable(paramtype.owner)
  84. else
  85. module:=current_module;
  86. if not assigned(module) then
  87. internalerror(2016112802);
  88. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+paramtype.unique_id_str;
  89. if constprettyname<>'' then
  90. namepart:=namepart+'$$'+constprettyname;
  91. { we use the full name of the type to uniquely identify it }
  92. if (symtablestack.top.symtabletype=parasymtable) and
  93. (symtablestack.top.defowner.typ=procdef) and
  94. (paramtype.owner=symtablestack.top) then
  95. begin
  96. { special handling for specializations inside generic function declarations }
  97. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname(true)+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  98. end
  99. else
  100. begin
  101. prettynamepart:=paramtype.fullownerhierarchyname(true);
  102. end;
  103. specializename:=specializename+namepart;
  104. if not first then
  105. prettyname:=prettyname+',';
  106. if constprettyname<>'' then
  107. prettyname:=prettyname+constprettyname
  108. else
  109. prettyname:=prettyname+prettynamepart+paramtype.typesym.prettyname;
  110. end;
  111. function get_generic_param_def(sym:tsym):tdef;
  112. begin
  113. if sym.typ=constsym then
  114. result:=tconstsym(sym).constdef
  115. else
  116. result:=ttypesym(sym).typedef;
  117. end;
  118. function compare_orddef_by_range(param1,param2:torddef;value:tconstvalue):boolean;
  119. begin
  120. if (value.valueord<param2.low) or (value.valueord>param2.high) then
  121. result:=false
  122. else
  123. result:=true;
  124. end;
  125. function compare_generic_params(param1,param2:tdef;constparamsym:tconstsym):boolean;
  126. begin
  127. if (param1.typ=orddef) and (param2.typ=orddef) then
  128. begin
  129. if is_boolean(param2) then
  130. result:=is_boolean(param1)
  131. else if is_char(param2) then
  132. result:=is_char(param1)
  133. else if compare_orddef_by_range(torddef(param1),torddef(param2),constparamsym.value) then
  134. result:=true
  135. else
  136. result:=false;
  137. end
  138. { arraydef is string constant so it's compatible with stringdef }
  139. else if (param1.typ=arraydef) and (param2.typ=stringdef) then
  140. result:=true
  141. { integer ords are compatible with float }
  142. else if (param1.typ=orddef) and is_integer(param1) and (param2.typ=floatdef) then
  143. result:=true
  144. { chars are compatible with stringdef }
  145. else if (param1.typ=orddef) and is_char(param1) and (param2.typ=stringdef) then
  146. result:=true
  147. { undefined def is compatible with all types }
  148. else if param2.typ=undefineddef then
  149. result:=true
  150. { sets require stricter checks }
  151. else if is_set(param2) then
  152. result:=equal_defs(param1,param2) or
  153. { constant could be empty set }
  154. not(assigned(tsetdef(param1).elementdef))
  155. else
  156. result:=param1.typ=param2.typ;
  157. end;
  158. function create_generic_constsym(fromdef:tdef;node:tnode;out prettyname:string):tconstsym;
  159. const
  160. undefinedname = 'undefined';
  161. var
  162. sym : tconstsym;
  163. setdef : tsetdef;
  164. enumsym : tsym;
  165. enumname : string;
  166. sp : pchar;
  167. ps : ^tconstset;
  168. pd : ^bestreal;
  169. i : integer;
  170. begin
  171. if node=nil then
  172. internalerror(2020011401);
  173. case node.nodetype of
  174. ordconstn:
  175. begin
  176. sym:=cconstsym.create_ord(undefinedname,constord,tordconstnode(node).value,fromdef);
  177. prettyname:=tostr(tordconstnode(node).value.svalue);
  178. end;
  179. stringconstn:
  180. begin
  181. getmem(sp,tstringconstnode(node).len+1);
  182. move(tstringconstnode(node).value_str^,sp^,tstringconstnode(node).len+1);
  183. sym:=cconstsym.create_string(undefinedname,conststring,sp,tstringconstnode(node).len,fromdef);
  184. prettyname:=''''+tstringconstnode(node).value_str+'''';
  185. end;
  186. realconstn:
  187. begin
  188. new(pd);
  189. pd^:=trealconstnode(node).value_real;
  190. sym:=cconstsym.create_ptr(undefinedname,constreal,pd,fromdef);
  191. prettyname:=realtostr(trealconstnode(node).value_real);
  192. end;
  193. setconstn:
  194. begin
  195. new(ps);
  196. ps^:=tsetconstnode(node).value_set^;
  197. sym:=cconstsym.create_ptr(undefinedname,constset,ps,fromdef);
  198. setdef:=tsetdef(tsetconstnode(node).resultdef);
  199. prettyname:='[';
  200. for i := setdef.setbase to setdef.setmax do
  201. if i in tsetconstnode(node).value_set^ then
  202. begin
  203. if setdef.elementdef.typ=enumdef then
  204. enumsym:=tenumdef(setdef.elementdef).int2enumsym(i)
  205. else
  206. enumsym:=nil;
  207. if assigned(enumsym) then
  208. enumname:=enumsym.realname
  209. else if setdef.elementdef.typ=orddef then
  210. begin
  211. if torddef(setdef.elementdef).ordtype=uchar then
  212. enumname:=chr(i)
  213. else
  214. enumname:=tostr(i);
  215. end
  216. else
  217. enumname:=tostr(i);
  218. if length(prettyname) > 1 then
  219. prettyname:=prettyname+','+enumname
  220. else
  221. prettyname:=prettyname+enumname;
  222. end;
  223. prettyname:=prettyname+']';
  224. end;
  225. niln:
  226. begin
  227. { only "nil" is available for pointer constants }
  228. sym:=cconstsym.create_ord(undefinedname,constnil,0,fromdef);
  229. prettyname:='nil';
  230. end;
  231. else
  232. internalerror(2019021601);
  233. end;
  234. { the sym needs an owner for later checks so use the typeparam owner }
  235. sym.owner:=fromdef.owner;
  236. include(sym.symoptions,sp_generic_const);
  237. result:=sym;
  238. end;
  239. procedure maybe_add_waiting_unit(tt:tdef);
  240. var
  241. hmodule : tmodule;
  242. begin
  243. if not assigned(tt) or
  244. not (df_generic in tt.defoptions) then
  245. exit;
  246. hmodule:=find_module_from_symtable(tt.owner);
  247. if not assigned(hmodule) then
  248. internalerror(2012092401);
  249. if hmodule=current_module then
  250. exit;
  251. if (hmodule.state = ms_load) and hmodule.interface_compiled then
  252. Exit;
  253. if not (hmodule.state in [ms_compiled,ms_processed]) then
  254. begin
  255. {$ifdef DEBUG_UNITWAITING}
  256. Writeln('Unit ', current_module.modulename^,
  257. ' waiting for ', hmodule.modulename^);
  258. {$endif DEBUG_UNITWAITING}
  259. if current_module.waitingforunit.indexof(hmodule)<0 then
  260. current_module.waitingforunit.add(hmodule);
  261. if hmodule.waitingunits.indexof(current_module)<0 then
  262. hmodule.waitingunits.add(current_module);
  263. end;
  264. end;
  265. procedure add_forward_generic_def(def:tdef;context:tspecializationcontext);
  266. var
  267. list : tfpobjectlist;
  268. fwdcontext : tspecializationcontext;
  269. begin
  270. if not is_implicit_pointer_object_type(def) then
  271. internalerror(2020070301);
  272. if not (oo_is_forward in tobjectdef(def).objectoptions) then
  273. internalerror(2020070302);
  274. if not assigned(tobjectdef(def).genericdef) then
  275. internalerror(2020070303);
  276. list:=tfpobjectlist(current_module.forwardgenericdefs.find(tobjectdef(def).genericdef.fulltypename));
  277. if not assigned(list) then
  278. begin
  279. list:=tfpobjectlist.create(true);
  280. current_module.forwardgenericdefs.add(tobjectdef(def).genericdef.fulltypename,list);
  281. end;
  282. fwdcontext:=context.getcopy;
  283. fwdcontext.forwarddef:=def;
  284. list.add(fwdcontext);
  285. end;
  286. function check_generic_constraints(genericdef:tstoreddef;paramlist:tfpobjectlist;poslist:tfplist):boolean;
  287. var
  288. i,j,
  289. intfcount : longint;
  290. formaldef,
  291. paradef : tstoreddef;
  292. genparadef : tdef;
  293. objdef,
  294. paraobjdef,
  295. formalobjdef : tobjectdef;
  296. intffound : boolean;
  297. filepos : tfileposinfo;
  298. is_const : boolean;
  299. begin
  300. { check whether the given specialization parameters fit to the eventual
  301. constraints of the generic }
  302. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  303. internalerror(2012101001);
  304. if genericdef.genericparas.count<>paramlist.count then
  305. internalerror(2012101002);
  306. if paramlist.count<>poslist.count then
  307. internalerror(2012120801);
  308. result:=true;
  309. for i:=0 to genericdef.genericparas.count-1 do
  310. begin
  311. filepos:=pfileposinfo(poslist[i])^;
  312. paradef:=tstoreddef(get_generic_param_def(tsym(paramlist[i])));
  313. is_const:=tsym(paramlist[i]).typ=constsym;
  314. genparadef:=genericdef.get_generic_param_def(i);
  315. { validate const params }
  316. if not genericdef.is_generic_param_const(i) and is_const then
  317. begin
  318. MessagePos(filepos,type_e_mismatch);
  319. exit(false);
  320. end
  321. else if genericdef.is_generic_param_const(i) then
  322. begin
  323. { param type mismatch (type <> const) }
  324. if genericdef.is_generic_param_const(i)<>is_const then
  325. begin
  326. MessagePos(filepos,type_e_mismatch);
  327. exit(false);
  328. end;
  329. { type constrained param doesn't match type }
  330. if not compare_generic_params(paradef,genericdef.get_generic_param_def(i),tconstsym(paramlist[i])) then
  331. begin
  332. MessagePos2(filepos,type_e_incompatible_types,FullTypeName(paradef,genparadef),FullTypeName(genparadef,paradef));
  333. exit(false);
  334. end;
  335. end;
  336. { test constraints for non-const params }
  337. if not genericdef.is_generic_param_const(i) then
  338. begin
  339. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  340. if formaldef.typ=undefineddef then
  341. { the parameter is of unspecified type, so no need to check }
  342. continue;
  343. if not (df_genconstraint in formaldef.defoptions) or
  344. not assigned(formaldef.genconstraintdata) then
  345. internalerror(2013021602);
  346. { undefineddef is compatible with anything }
  347. if formaldef.typ=undefineddef then
  348. continue;
  349. if paradef.typ<>formaldef.typ then
  350. begin
  351. case formaldef.typ of
  352. recorddef:
  353. { delphi has own fantasy about record constraint
  354. (almost non-nullable/non-nilable value type) }
  355. if m_delphi in current_settings.modeswitches then
  356. case paradef.typ of
  357. floatdef,enumdef,orddef:
  358. continue;
  359. objectdef:
  360. if tobjectdef(paradef).objecttype=odt_object then
  361. continue
  362. else
  363. MessagePos(filepos,type_e_record_type_expected);
  364. else
  365. MessagePos(filepos,type_e_record_type_expected);
  366. end
  367. else
  368. MessagePos(filepos,type_e_record_type_expected);
  369. objectdef:
  370. case tobjectdef(formaldef).objecttype of
  371. odt_class,
  372. odt_javaclass:
  373. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  374. odt_interfacecom,
  375. odt_interfacecorba,
  376. odt_dispinterface,
  377. odt_interfacejava:
  378. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  379. else
  380. internalerror(2012101003);
  381. end;
  382. errordef:
  383. { ignore }
  384. ;
  385. else
  386. internalerror(2012101004);
  387. end;
  388. result:=false;
  389. end
  390. else
  391. begin
  392. { the paradef types are the same, so do special checks for the
  393. cases in which they are needed }
  394. if formaldef.typ=objectdef then
  395. begin
  396. paraobjdef:=tobjectdef(paradef);
  397. formalobjdef:=tobjectdef(formaldef);
  398. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  399. internalerror(2012101102);
  400. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  401. begin
  402. { this is either a concerete interface or class type (the
  403. latter without specific implemented interfaces) }
  404. case paraobjdef.objecttype of
  405. odt_interfacecom,
  406. odt_interfacecorba,
  407. odt_interfacejava,
  408. odt_dispinterface:
  409. begin
  410. if (oo_is_forward in paraobjdef.objectoptions) and
  411. (paraobjdef.objecttype=formalobjdef.objecttype) and
  412. (df_genconstraint in formalobjdef.defoptions) and
  413. (
  414. (formalobjdef.objecttype=odt_interfacecom) and
  415. (formalobjdef.childof=interface_iunknown)
  416. )
  417. or
  418. (
  419. (formalobjdef.objecttype=odt_interfacecorba) and
  420. (formalobjdef.childof=nil)
  421. ) then
  422. continue;
  423. if not def_is_related(paraobjdef,formalobjdef.childof) then
  424. begin
  425. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  426. result:=false;
  427. end;
  428. end;
  429. odt_class,
  430. odt_javaclass:
  431. begin
  432. objdef:=paraobjdef;
  433. intffound:=false;
  434. while assigned(objdef) do
  435. begin
  436. for j:=0 to objdef.implementedinterfaces.count-1 do
  437. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  438. begin
  439. intffound:=true;
  440. break;
  441. end;
  442. if intffound then
  443. break;
  444. objdef:=objdef.childof;
  445. end;
  446. result:=intffound;
  447. if not result then
  448. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  449. end;
  450. else
  451. begin
  452. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  453. result:=false;
  454. end;
  455. end;
  456. end
  457. else
  458. begin
  459. { this is either a "class" or a concrete instance with
  460. or without implemented interfaces }
  461. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  462. begin
  463. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  464. result:=false;
  465. continue;
  466. end;
  467. { for forward declared classes we allow pure TObject/class declarations }
  468. if (oo_is_forward in paraobjdef.objectoptions) and
  469. (df_genconstraint in formaldef.defoptions) then
  470. begin
  471. if (formalobjdef.childof=class_tobject) and
  472. not formalobjdef.implements_any_interfaces then
  473. continue;
  474. end;
  475. if assigned(formalobjdef.childof) and
  476. not def_is_related(paradef,formalobjdef.childof) then
  477. begin
  478. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  479. result:=false;
  480. end;
  481. intfcount:=0;
  482. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  483. begin
  484. objdef:=paraobjdef;
  485. intffound:=false;
  486. while assigned(objdef) do
  487. begin
  488. intffound:=assigned(
  489. find_implemented_interface(objdef,
  490. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  491. )
  492. );
  493. if intffound then
  494. break;
  495. objdef:=objdef.childof;
  496. end;
  497. if intffound then
  498. inc(intfcount)
  499. else
  500. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  501. end;
  502. if intfcount<>formalobjdef.implementedinterfaces.count then
  503. result:=false;
  504. end;
  505. end;
  506. end;
  507. end;
  508. end;
  509. end;
  510. function parse_generic_specialization_types_internal(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  511. var
  512. old_block_type : tblock_type;
  513. first : boolean;
  514. typeparam : tnode;
  515. parampos : pfileposinfo;
  516. tmpparampos : tfileposinfo;
  517. namepart : string;
  518. module : tmodule;
  519. constprettyname : string;
  520. validparam : boolean;
  521. begin
  522. result:=true;
  523. prettyname:='';
  524. constprettyname:='';
  525. if paramlist=nil then
  526. internalerror(2012061401);
  527. { set the block type to type, so that the parsed type are returned as
  528. ttypenode (e.g. classes are in non type-compatible blocks returned as
  529. tloadvmtaddrnode) }
  530. old_block_type:=block_type;
  531. { if parsedtype is set, then the first type identifer was already parsed
  532. (happens in inline specializations) and thus we only need to parse
  533. the remaining types and do as if the first one was already given }
  534. first:=not assigned(parsedtype);
  535. if assigned(parsedtype) then
  536. begin
  537. paramlist.Add(parsedtype.typesym);
  538. module:=find_module_from_symtable(parsedtype.owner);
  539. if not assigned(module) then
  540. internalerror(2016112801);
  541. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+parsedtype.unique_id_str;
  542. specializename:='$'+namepart;
  543. prettyname:=parsedtype.fullownerhierarchyname(true)+parsedtype.typesym.prettyname;
  544. if assigned(poslist) then
  545. begin
  546. New(parampos);
  547. parampos^:=parsedpos;
  548. poslist.add(parampos);
  549. end;
  550. end
  551. else
  552. specializename:='$';
  553. while not (token in [_GT,_RSHARPBRACKET]) do
  554. begin
  555. { "first" is set to false at the end of the loop! }
  556. if not first then
  557. consume(_COMMA);
  558. block_type:=bt_type;
  559. tmpparampos:=current_filepos;
  560. typeparam:=factor(false,[ef_accept_equal]);
  561. { determine if the typeparam node is a valid type or const }
  562. validparam:=typeparam.nodetype in tgeneric_param_nodes;
  563. if validparam then
  564. begin
  565. if tstoreddef(typeparam.resultdef).is_generic and
  566. (
  567. not parse_generic or
  568. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  569. ) then
  570. Message(parser_e_no_generics_as_params);
  571. if assigned(poslist) then
  572. begin
  573. New(parampos);
  574. parampos^:=tmpparampos;
  575. poslist.add(parampos);
  576. end;
  577. if typeparam.resultdef.typ<>errordef then
  578. begin
  579. if (typeparam.nodetype=typen) and not assigned(typeparam.resultdef.typesym) then
  580. message(type_e_generics_cannot_reference_itself)
  581. else if (typeparam.resultdef.typ<>errordef) then
  582. begin
  583. { all non-type nodes are considered const }
  584. if typeparam.nodetype<>typen then
  585. paramlist.Add(create_generic_constsym(typeparam.resultdef,typeparam,constprettyname))
  586. else
  587. begin
  588. constprettyname:='';
  589. paramlist.Add(typeparam.resultdef.typesym);
  590. end;
  591. make_prettystring(typeparam.resultdef,first,constprettyname,prettyname,specializename);
  592. end;
  593. end
  594. else
  595. begin
  596. result:=false;
  597. end;
  598. end
  599. else
  600. begin
  601. Message(type_e_type_id_expected);
  602. result:=false;
  603. end;
  604. typeparam.free;
  605. first:=false;
  606. end;
  607. block_type:=old_block_type;
  608. end;
  609. function parse_generic_specialization_types(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  610. var
  611. dummypos : tfileposinfo;
  612. begin
  613. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  614. result:=parse_generic_specialization_types_internal(paramlist,poslist,prettyname,specializename,nil,dummypos);
  615. end;
  616. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string);
  617. var
  618. dummypos : tfileposinfo;
  619. begin
  620. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  621. generate_specialization(tt,enforce_unit,parse_class_parent,_prettyname,nil,'',dummypos);
  622. end;
  623. function finalize_specialization(var pd:tprocdef;spezcontext:tspecializationcontext):boolean;
  624. var
  625. def : tdef;
  626. begin
  627. result:=false;
  628. if assigned(spezcontext) then
  629. begin
  630. if not (df_generic in pd.defoptions) then
  631. internalerror(2015060301);
  632. { check whether the given parameters are compatible
  633. to the def's constraints }
  634. if not check_generic_constraints(pd,spezcontext.paramlist,spezcontext.poslist) then
  635. exit;
  636. def:=generate_specialization_phase2(spezcontext,pd,false,'');
  637. case def.typ of
  638. errordef:
  639. { do nothing }
  640. ;
  641. procdef:
  642. pd:=tprocdef(def);
  643. else
  644. internalerror(2015070303);
  645. end;
  646. end;
  647. result:=true;
  648. end;
  649. procedure transfer_unnamed_symbols(owner:tsymtable;unnamed_syms:tfplist);
  650. var
  651. i : integer;
  652. sym : tsym;
  653. begin
  654. for i:=0 to unnamed_syms.count-1 do
  655. begin
  656. sym:=tsym(unnamed_syms[i]);
  657. sym.ChangeOwnerAndName(owner,sym.realname);
  658. end;
  659. unnamed_syms.clear;
  660. end;
  661. function try_implicit_specialization(sym:tsym;para:tnode;pdoverloadlist:tfpobjectlist;var unnamed_syms:tfplist;var first_procsym:tsym;var hasoverload:boolean):boolean;
  662. { hash key for generic parameter lookups }
  663. function generic_param_hash(def:tdef):string;inline;
  664. begin
  665. result:=def.typename;
  666. end;
  667. { returns true if the def a literal array such as [1,2,3] and not a shortstring }
  668. function is_array_literal(def:tdef):boolean;
  669. begin
  670. result:=(def.typ=arraydef) and not is_conststring_array(def);
  671. end;
  672. { makes the specialization context from the generic proc def and generic params }
  673. procedure generate_implicit_specialization(out context:tspecializationcontext;genericdef:tprocdef;genericparams:tfphashlist);
  674. var
  675. parsedpos:tfileposinfo;
  676. poslist:tfplist;
  677. i: longint;
  678. paramtype: ttypesym;
  679. parampos : pfileposinfo;
  680. tmpparampos : tfileposinfo;
  681. paramname: string;
  682. begin
  683. context:=tspecializationcontext.create;
  684. fillchar(parsedpos,sizeof(parsedpos),0);
  685. poslist:=context.poslist;
  686. tmpparampos:=current_filepos;
  687. if genericparams.count<>genericdef.genericparas.count then
  688. internalerror(2021020901);
  689. poslist.capacity:=poslist.count+genericparams.count;
  690. context.paramlist.capacity:=context.paramlist.count+genericparams.count;
  691. for i:=0 to genericparams.count-1 do
  692. begin
  693. paramname:=generic_param_hash(ttypesym(genericdef.genericparas[i]).typedef);
  694. paramtype:=ttypesym(genericparams.find(paramname));
  695. if not assigned(paramtype) then
  696. internalerror(2021020902);
  697. new(parampos);
  698. parampos^:=tmpparampos;
  699. poslist.add(parampos);
  700. context.paramlist.Add(paramtype);
  701. make_prettystring(paramtype.typedef,i=0,'',context.prettyname,context.specializename);
  702. end;
  703. context.genname:=genericdef.procsym.realname;
  704. end;
  705. { specialization context parameter lists require a typesym so we need
  706. to generate a placeholder for unnamed constant types like
  707. short strings, open arrays, function pointers etc... }
  708. function create_unnamed_typesym(def:tdef):tsym;
  709. var
  710. newtype: tsym;
  711. begin
  712. newtype:=nil;
  713. if is_conststring_array(def) then
  714. begin
  715. { for constant strings we need to respect various modeswitches }
  716. if (cs_refcountedstrings in current_settings.localswitches) then
  717. begin
  718. if m_default_unicodestring in current_settings.modeswitches then
  719. newtype:=cunicodestringtype.typesym
  720. else
  721. newtype:=cansistringtype.typesym;
  722. end
  723. else
  724. newtype:=cshortstringtype.typesym;
  725. end
  726. else if def.typ=stringdef then
  727. newtype:=tstringdef(def).get_default_string_type.typesym
  728. else
  729. begin
  730. if is_array_constructor(def) then
  731. begin
  732. { array constructor is not a valid parameter type; getreusable
  733. avoids creating multiple implementations for calls with the
  734. same number of array elements of a particular type }
  735. def:=carraydef.getreusable(tarraydef(def).elementdef,tarraydef(def).highrange-tarraydef(def).lowrange+1);
  736. end;
  737. newtype:=ctypesym.create(def.fullownerhierarchyname(false)+typName[def.typ]+'$'+def.unique_id_str,def);
  738. include(newtype.symoptions,sp_generic_unnamed_type);
  739. newtype.owner:=def.owner;
  740. { ensure that there's no warning }
  741. newtype.refs:=1;
  742. end;
  743. if not assigned(newtype) then
  744. internalerror(2021020904);
  745. result:=newtype;
  746. end;
  747. { searches for the generic param in specializations }
  748. function find_param_in_specialization(owner:tprocdef;genericparam:ttypesym;def:tstoreddef):boolean;
  749. var
  750. parasym: ttypesym;
  751. k, i: integer;
  752. begin
  753. result:=false;
  754. for i:=0 to def.genericparas.count-1 do
  755. begin
  756. parasym:=ttypesym(def.genericparas[i]);
  757. { the generic param must have a named typesym }
  758. if not assigned(parasym.typedef.typesym) then
  759. internalerror(2021020907);
  760. { recurse into inline specialization }
  761. if tstoreddef(parasym.typedef).is_specialization then
  762. begin
  763. result:=find_param_in_specialization(owner,genericparam,tstoreddef(parasym.typedef));
  764. if result then
  765. exit;
  766. end
  767. else if (genericparam=parasym.typedef.typesym) and owner.is_generic_param(parasym.typedef) then
  768. exit(true);
  769. end;
  770. end;
  771. { searches for the generic param in arrays }
  772. function find_param_in_array(owner:tprocdef;genericparam:ttypesym;def:tarraydef):boolean;
  773. var
  774. elementdef:tstoreddef;
  775. begin
  776. elementdef:=tstoreddef(def.elementdef);
  777. { recurse into multi-dimensional array }
  778. if elementdef.typ=arraydef then
  779. result:=find_param_in_array(owner,genericparam,tarraydef(elementdef))
  780. { something went wrong during parsing and the element is invalid }
  781. else if elementdef.typ=errordef then
  782. result:=false
  783. else
  784. begin
  785. { the element must have a named typesym }
  786. if not assigned(elementdef.typesym) then
  787. internalerror(2021020906);
  788. result:=(genericparam=elementdef.typesym) and owner.is_generic_param(elementdef);
  789. end;
  790. end;
  791. { tests if the generic param is used in the parameter list }
  792. function is_generic_param_used(owner:tprocdef;genericparam:ttypesym;paras:tfplist):boolean;
  793. var
  794. paravar:tparavarsym;
  795. i: integer;
  796. begin
  797. result:=false;
  798. for i:=0 to paras.count-1 do
  799. begin
  800. paravar:=tparavarsym(paras[i]);
  801. { handle array types by using element types (for example: array of T) }
  802. if paravar.vardef.typ=arraydef then
  803. result:=find_param_in_array(owner,genericparam,tarraydef(paravar.vardef))
  804. { for specializations check search in generic params }
  805. else if tstoreddef(paravar.vardef).is_specialization then
  806. result:=find_param_in_specialization(owner,genericparam,tstoreddef(paravar.vardef))
  807. { something went wrong during parsing and the parameter is invalid }
  808. else if paravar.vardef.typ=errordef then
  809. exit(false)
  810. else
  811. begin
  812. if not assigned(paravar.vardef.typesym) then
  813. internalerror(2021020905);
  814. result:=(genericparam=paravar.vardef.typesym) and owner.is_generic_param(paravar.vardef)
  815. end;
  816. { exit if we find a used parameter }
  817. if result then
  818. exit;
  819. end;
  820. end;
  821. { handle generic specializations by using generic params from caller
  822. to specialize the target. for example "TRec<Integer>" can use "Integer"
  823. to specialize "TRec<T>" with "Integer" for "T". }
  824. procedure handle_specializations(genericparams:tfphashlist;target_def,caller_def:tstoreddef);
  825. var
  826. i,
  827. index : integer;
  828. key : string;
  829. target_param,
  830. caller_param : ttypesym;
  831. begin
  832. { the target and the caller must the same generic def
  833. with the same set of generic parameters }
  834. if target_def.genericdef<>caller_def.genericdef then
  835. internalerror(2021020909);
  836. for i:=0 to target_def.genericparas.count-1 do
  837. begin
  838. target_param:=ttypesym(target_def.genericparas[i]);
  839. caller_param:=ttypesym(caller_def.genericparas[i]);
  840. { reject generics with constants }
  841. if (target_param.typ=constsym) or (caller_param.typ=constsym) then
  842. exit;
  843. key:=generic_param_hash(target_param.typedef);
  844. { the generic param is already used }
  845. index:=genericparams.findindexof(key);
  846. if index>=0 then
  847. continue;
  848. { add the type to the generic params }
  849. genericparams.add(key,caller_param);
  850. end;
  851. end;
  852. { specialize arrays by using element types but arrays may be multi-dimensional
  853. so we need to examine the caller/target pairs recursively in order to
  854. verify the dimensionality is equal }
  855. function handle_arrays(owner:tprocdef;target_def,caller_def:tarraydef;out target_element,caller_element:tdef):boolean;
  856. begin
  857. { the target and the caller are both arrays and the target is a
  858. specialization so we can recurse into the targets element def }
  859. if is_array_literal(target_def.elementdef) and
  860. is_array_literal(caller_def.elementdef) and
  861. target_def.is_specialization then
  862. result:=handle_arrays(owner,tarraydef(target_def.elementdef),tarraydef(caller_def.elementdef),target_element,caller_element)
  863. else
  864. begin
  865. { the caller is an array which means the dimensionality is unbalanced
  866. and thus the arrays are compatible }
  867. if is_array_literal(caller_def.elementdef) then
  868. exit(false);
  869. { if the element is a generic param then return this type
  870. along with the caller element type at the same level }
  871. result:=owner.is_generic_param(target_def.elementdef);
  872. if result then
  873. begin
  874. target_element:=target_def.elementdef;
  875. caller_element:=caller_def.elementdef;
  876. end;
  877. end;
  878. end;
  879. { handle procvars by using the parameters from the caller to specialize
  880. the parameters of the target generic procedure specialization. for example:
  881. type generic TProc<S> = procedure(value: S);
  882. generic procedure Run<T>(proc: specialize TProc<T>);
  883. procedure DoCallback(value: integer);
  884. Run(@DoCallback);
  885. will specialize as Run<integer> because the signature
  886. of DoCallback() matches TProc<S> so we can specialize "S"
  887. with "integer", as they are both parameter #1
  888. }
  889. function handle_procvars(genericparams:tfphashlist;callerparams:tfplist;target_def:tdef;caller_def:tdef):boolean;
  890. var
  891. newparams : tfphashlist;
  892. procedure handle_generic_param(targetparadef,callerparadef:tdef);
  893. var
  894. key : string;
  895. index : integer;
  896. begin
  897. if not assigned(callerparadef.typesym) then
  898. internalerror(2021020908);
  899. key:=generic_param_hash(targetparadef);
  900. { the generic param must not already be used }
  901. index:=genericparams.findindexof(key);
  902. if index<0 then
  903. begin
  904. { add the type to the list }
  905. index:=newparams.findindexof(key);
  906. if index<0 then
  907. newparams.add(key,callerparadef.typesym);
  908. end;
  909. end;
  910. var
  911. i,j : integer;
  912. paravar : tparavarsym;
  913. target_proc,
  914. caller_proc : tprocvardef;
  915. target_proc_para,
  916. caller_proc_para : tparavarsym;
  917. valid_params : integer;
  918. begin
  919. result := false;
  920. target_proc:=tprocvardef(target_def);
  921. caller_proc:=tprocvardef(caller_def);
  922. { parameter count must match exactly
  923. currently default values are not considered }
  924. if target_proc.paras.count<>caller_proc.paras.count then
  925. exit;
  926. { a mixture of functions and procedures is not allowed }
  927. if (not assigned(target_proc.returndef) or is_void(target_proc.returndef)) xor
  928. (not assigned(caller_proc.returndef) or is_void(caller_proc.returndef)) then
  929. exit;
  930. { reject generics with constants }
  931. for i:=0 to target_proc.genericdef.genericparas.count-1 do
  932. if tsym(target_proc.genericdef.genericparas[i]).typ=constsym then
  933. exit;
  934. newparams:=tfphashlist.create;
  935. valid_params:=0;
  936. for i:=0 to target_proc.paras.count-1 do
  937. begin
  938. target_proc_para:=tparavarsym(target_proc.paras[i]);
  939. caller_proc_para:=tparavarsym(caller_proc.paras[i]);
  940. { the parameters are not compatible }
  941. if compare_defs(caller_proc_para.vardef,target_proc_para.vardef,nothingn)=te_incompatible then
  942. begin
  943. newparams.free;
  944. exit(false);
  945. end;
  946. if sp_generic_para in target_proc_para.vardef.typesym.symoptions then
  947. begin
  948. paravar:=tparavarsym(tprocvardef(target_proc.genericdef).paras[i]);
  949. { find the generic param name in the generic def parameters }
  950. j:=target_proc.genericdef.genericparas.findindexof(paravar.vardef.typesym.name);
  951. handle_generic_param(ttypesym(target_proc.genericparas[j]).typedef,caller_proc_para.vardef);
  952. end;
  953. inc(valid_params);
  954. end;
  955. if assigned(target_proc.returndef) and not is_void(target_proc.returndef) then
  956. begin
  957. { or check for exact? }
  958. if compare_defs(caller_proc.returndef,target_proc.returndef,nothingn)<te_equal then
  959. begin
  960. newparams.free;
  961. exit(false);
  962. end;
  963. if sp_generic_para in target_proc.returndef.typesym.symoptions then
  964. begin
  965. handle_generic_param(target_proc.returndef,caller_proc.returndef);
  966. end;
  967. end;
  968. { if the count of valid params matches the target then
  969. transfer the temporary params to the actual params }
  970. result:=valid_params=target_proc.paras.count;
  971. if result then
  972. for i := 0 to newparams.count-1 do
  973. genericparams.add(newparams.nameofindex(i),newparams[i]);
  974. newparams.free;
  975. end;
  976. function maybe_inherited_specialization(givendef,desireddef:tstoreddef;out basedef:tstoreddef):boolean;
  977. begin
  978. result:=false;
  979. basedef:=nil;
  980. if givendef.typ<>objectdef then
  981. begin
  982. result:=givendef.is_specialization and (givendef.genericdef=desireddef.genericdef);
  983. if result then
  984. basedef:=givendef;
  985. end
  986. else
  987. begin
  988. while assigned(givendef) do
  989. begin
  990. if givendef.is_specialization and (givendef.genericdef=desireddef.genericdef) then
  991. begin
  992. basedef:=givendef;
  993. result:=true;
  994. break;
  995. end;
  996. givendef:=tobjectdef(givendef).childof;
  997. end;
  998. end;
  999. end;
  1000. { compare generic parameters <T> with call node parameters. }
  1001. function is_possible_specialization(callerparams:tfplist;genericdef:tprocdef;out unnamed_syms:tfplist;out genericparams:tfphashlist):boolean;
  1002. var
  1003. i,j,
  1004. count : integer;
  1005. paravar : tparavarsym;
  1006. base_def : tstoreddef;
  1007. target_def,
  1008. caller_def : tdef;
  1009. target_key : string;
  1010. index : integer;
  1011. paras : tfplist;
  1012. target_element,
  1013. caller_element : tdef;
  1014. required_param_count : integer;
  1015. adef : tarraydef;
  1016. begin
  1017. result:=false;
  1018. paras:=nil;
  1019. genericparams:=nil;
  1020. required_param_count:=0;
  1021. unnamed_syms:=nil;
  1022. { first perform a check to reject generics with constants }
  1023. for i:=0 to genericdef.genericparas.count-1 do
  1024. if tsym(genericdef.genericparas[i]).typ=constsym then
  1025. exit;
  1026. { build list of visible target function parameters }
  1027. paras:=tfplist.create;
  1028. for i:=0 to genericdef.paras.count-1 do
  1029. begin
  1030. paravar:=tparavarsym(genericdef.paras[i]);
  1031. { ignore hidden parameters }
  1032. if vo_is_hidden_para in paravar.varoptions then
  1033. continue;
  1034. paras.add(paravar);
  1035. { const non-default parameters are required }
  1036. if not assigned(paravar.defaultconstsym) then
  1037. inc(required_param_count);
  1038. end;
  1039. { not enough parameters were supplied }
  1040. if callerparams.count<required_param_count then
  1041. begin
  1042. paras.free;
  1043. exit;
  1044. end;
  1045. { check to make sure the generic parameters are all used
  1046. at least once in the caller parameters. }
  1047. count:=0;
  1048. for i:=0 to genericdef.genericparas.count-1 do
  1049. if is_generic_param_used(genericdef,ttypesym(genericdef.genericparas[i]),paras) then
  1050. inc(count);
  1051. if count<genericdef.genericparas.count then
  1052. begin
  1053. paras.free;
  1054. exit;
  1055. end;
  1056. genericparams:=tfphashlist.create;
  1057. for i:=0 to callerparams.count-1 do
  1058. begin
  1059. caller_def:=ttypesym(callerparams[i]).typedef;
  1060. { caller parameter exceeded the possible parameters }
  1061. if i=paras.count then
  1062. begin
  1063. genericparams.free;
  1064. paras.free;
  1065. exit;
  1066. end;
  1067. target_def:=tparavarsym(paras[i]).vardef;
  1068. target_key:='';
  1069. { strings are compatible with "array of T" so we
  1070. need to use the element type for specialization }
  1071. if is_stringlike(caller_def) and
  1072. is_array_literal(target_def) and
  1073. genericdef.is_generic_param(tarraydef(target_def).elementdef) then
  1074. begin
  1075. target_def:=tarraydef(target_def).elementdef;
  1076. target_key:=generic_param_hash(target_def);
  1077. caller_def:=chartype_for_stringlike(caller_def);
  1078. end
  1079. { non-uniform array constructors (i.e. array of const) are not compatible
  1080. with normal arrays like "array of T" so we reject them }
  1081. else if is_array_literal(target_def) and
  1082. (caller_def.typ=arraydef) and
  1083. (ado_IsConstructor in tarraydef(caller_def).arrayoptions) and
  1084. (ado_IsArrayOfConst in tarraydef(caller_def).arrayoptions) then
  1085. begin
  1086. continue;
  1087. end
  1088. { handle generic arrays }
  1089. else if is_array_literal(caller_def) and
  1090. is_array_literal(target_def) and
  1091. handle_arrays(genericdef,tarraydef(target_def),tarraydef(caller_def),target_element,caller_element) then
  1092. begin
  1093. target_def:=target_element;
  1094. caller_def:=caller_element;
  1095. target_key:=generic_param_hash(target_def);
  1096. end
  1097. { handle generic procvars }
  1098. else if (caller_def.typ=procvardef) and
  1099. (target_def.typ=procvardef) and
  1100. tprocvardef(target_def).is_specialization and
  1101. handle_procvars(genericparams,callerparams,target_def,caller_def) then
  1102. begin
  1103. continue;
  1104. end
  1105. { handle specialized objects by taking the base class as the type to specialize }
  1106. else if is_class_or_object(caller_def) and
  1107. is_class_or_object(target_def) and
  1108. genericdef.is_generic_param(target_def) then
  1109. begin
  1110. target_key:=generic_param_hash(target_def);
  1111. target_def:=tobjectdef(target_def).childof;
  1112. end
  1113. { handle generic specializations }
  1114. else if tstoreddef(target_def).is_specialization and
  1115. maybe_inherited_specialization(tstoreddef(caller_def),tstoreddef(target_def),base_def) then
  1116. begin
  1117. handle_specializations(genericparams,tstoreddef(target_def),base_def);
  1118. continue;
  1119. end
  1120. { handle all other generic params }
  1121. else if target_def.typ=undefineddef then
  1122. target_key:=generic_param_hash(target_def);
  1123. { the param doesn't have a generic key which means we don't need to consider it }
  1124. if target_key='' then
  1125. continue;
  1126. { the generic param is already used }
  1127. index:=genericparams.findindexof(target_key);
  1128. if index>=0 then
  1129. continue;
  1130. { the caller type may not have a typesym so we need to create an unnamed one }
  1131. if not assigned(caller_def.typesym) then
  1132. begin
  1133. sym:=create_unnamed_typesym(caller_def);
  1134. { add the unnamed sym to the list but only it was allocated manually }
  1135. if sym.owner=caller_def.owner then
  1136. begin
  1137. if not assigned(unnamed_syms) then
  1138. unnamed_syms:=tfplist.create;
  1139. unnamed_syms.add(sym);
  1140. end;
  1141. genericparams.add(target_key,sym);
  1142. end
  1143. else
  1144. genericparams.add(target_key,caller_def.typesym);
  1145. end;
  1146. { if the parameter counts match then the specialization is possible }
  1147. result:=genericparams.count=genericdef.genericparas.count;
  1148. { cleanup }
  1149. paras.free;
  1150. if not result then
  1151. genericparams.free;
  1152. end;
  1153. { make an ordered list of parameters from the caller }
  1154. function make_param_list(dummysym:tsym;para:tnode;var unnamed_syms:tfplist):tfplist;
  1155. var
  1156. pt : tcallparanode;
  1157. paradef : tdef;
  1158. sym : tsym;
  1159. i : integer;
  1160. begin
  1161. result:=tfplist.create;
  1162. pt:=tcallparanode(para);
  1163. while assigned(pt) do
  1164. begin
  1165. paradef:=pt.paravalue.resultdef;
  1166. { unnamed parameter types can not be specialized }
  1167. if not assigned(paradef.typesym) then
  1168. begin
  1169. sym:=create_unnamed_typesym(paradef);
  1170. result.insert(0,sym);
  1171. { add the unnamed sym to the list but only if it was allocated manually }
  1172. if sym.owner=paradef.owner then
  1173. begin
  1174. if not assigned(unnamed_syms) then
  1175. unnamed_syms:=tfplist.create;
  1176. unnamed_syms.add(sym);
  1177. end;
  1178. end
  1179. else
  1180. result.insert(0,paradef.typesym);
  1181. pt:=tcallparanode(pt.nextpara);
  1182. end;
  1183. end;
  1184. var
  1185. i,j,k : integer;
  1186. srsym : tprocsym;
  1187. callerparams : tfplist;
  1188. pd : tprocdef;
  1189. dummysym : tprocsym;
  1190. genericparams : tfphashlist;
  1191. spezcontext : tspecializationcontext;
  1192. pd_unnamed_syms : tfplist;
  1193. begin
  1194. result:=false;
  1195. spezcontext:=nil;
  1196. genericparams:=nil;
  1197. dummysym:=tprocsym(sym);
  1198. callerparams:=make_param_list(dummysym,para,unnamed_syms);
  1199. { failed to build the parameter list }
  1200. if not assigned(callerparams) then
  1201. exit;
  1202. for i:=0 to dummysym.genprocsymovlds.count-1 do
  1203. begin
  1204. srsym:=tprocsym(dummysym.genprocsymovlds[i]);
  1205. for j:=0 to srsym.ProcdefList.Count-1 do
  1206. begin
  1207. pd:=tprocdef(srsym.ProcdefList[j]);
  1208. if is_possible_specialization(callerparams,pd,pd_unnamed_syms,genericparams) then
  1209. begin
  1210. generate_implicit_specialization(spezcontext,pd,genericparams);
  1211. genericparams.free;
  1212. { finalize the specialization so it can be added to the list of overloads }
  1213. if not finalize_specialization(pd,spezcontext) then
  1214. begin
  1215. spezcontext.free;
  1216. continue;
  1217. end;
  1218. { handle unnamed syms used by the specialization }
  1219. if pd_unnamed_syms<>nil then
  1220. begin
  1221. transfer_unnamed_symbols(pd.owner,pd_unnamed_syms);
  1222. pd_unnamed_syms.free;
  1223. end;
  1224. pdoverloadlist.add(pd);
  1225. spezcontext.free;
  1226. if po_overload in pd.procoptions then
  1227. hasoverload:=true;
  1228. { store first procsym found }
  1229. if not assigned(first_procsym) then
  1230. first_procsym:=srsym;
  1231. result:=true;
  1232. end
  1233. else
  1234. begin
  1235. { the specialization was not chosen so clean up any unnamed syms }
  1236. if pd_unnamed_syms<>nil then
  1237. begin
  1238. for k:=0 to pd_unnamed_syms.count-1 do
  1239. tsym(pd_unnamed_syms[k]).free;
  1240. pd_unnamed_syms.free;
  1241. end;
  1242. end;
  1243. end;
  1244. end;
  1245. callerparams.free;
  1246. end;
  1247. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean):tdef;
  1248. var
  1249. dummypos : tfileposinfo;
  1250. {$push}
  1251. {$warn 5036 off}
  1252. begin
  1253. result:=generate_specialization_phase1(context,genericdef,enforce_unit,nil,'',nil,dummypos);
  1254. end;
  1255. {$pop}
  1256. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;const symname:string;symtable:tsymtable):tdef;
  1257. var
  1258. dummypos : tfileposinfo;
  1259. {$push}
  1260. {$warn 5036 off}
  1261. begin
  1262. result:=generate_specialization_phase1(context,genericdef,enforce_unit,nil,symname,symtable,dummypos);
  1263. end;
  1264. {$pop}
  1265. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;parsedtype:tdef;const symname:string;symtable:tsymtable;parsedpos:tfileposinfo):tdef;
  1266. var
  1267. found,
  1268. err : boolean;
  1269. i,
  1270. gencount : longint;
  1271. countstr,genname,ugenname,prettygenname: string;
  1272. tmpstack : tfpobjectlist;
  1273. symowner : tsymtable;
  1274. hmodule : tmodule;
  1275. begin
  1276. context:=nil;
  1277. result:=nil;
  1278. { either symname must be given or genericdef needs to be valid }
  1279. if (symname='') and
  1280. (not assigned(genericdef) or
  1281. (
  1282. (genericdef.typ<>procdef) and
  1283. (
  1284. not assigned(genericdef.typesym) or
  1285. (genericdef.typesym.typ<>typesym)
  1286. ) and
  1287. (
  1288. (genericdef.typ<>objectdef) or
  1289. not (oo_is_forward in tobjectdef(genericdef).objectoptions)
  1290. )
  1291. ) or
  1292. (
  1293. (genericdef.typ=procdef) and
  1294. (
  1295. not assigned(tprocdef(genericdef).procsym) or
  1296. (tprocdef(genericdef).procsym.typ<>procsym)
  1297. )
  1298. )
  1299. ) then
  1300. begin
  1301. internalerror(2019112401);
  1302. end;
  1303. if not assigned(parsedtype) and not try_to_consume(_LT) then
  1304. begin
  1305. consume(_LSHARPBRACKET);
  1306. { handle "<>" }
  1307. if (token=_GT) or (token=_RSHARPBRACKET) then
  1308. begin
  1309. Message(type_e_type_id_expected);
  1310. if not try_to_consume(_GT) then
  1311. try_to_consume(_RSHARPBRACKET);
  1312. result:=generrordef;
  1313. exit;
  1314. end;
  1315. end;
  1316. context:=tspecializationcontext.create;
  1317. { Parse type parameters }
  1318. err:=not parse_generic_specialization_types_internal(context.paramlist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  1319. if err then
  1320. begin
  1321. if not try_to_consume(_GT) then
  1322. try_to_consume(_RSHARPBRACKET);
  1323. context.free;
  1324. context:=nil;
  1325. result:=generrordef;
  1326. exit;
  1327. end;
  1328. { use the name of the symbol as procvars return a user friendly version
  1329. of the name }
  1330. if symname='' then
  1331. begin
  1332. if genericdef.typ=procdef then
  1333. genname:=tprocdef(genericdef).procsym.realname
  1334. else if assigned(genericdef.typesym) then
  1335. genname:=ttypesym(genericdef.typesym).realname
  1336. else if (genericdef.typ=objectdef) and (oo_is_forward in tobjectdef(genericdef).objectoptions) then
  1337. genname:=tobjectdef(genericdef).objrealname^
  1338. else
  1339. internalerror(2020071201);
  1340. end
  1341. else
  1342. genname:=symname;
  1343. { in case of non-Delphi mode the type name could already be a generic
  1344. def (but maybe the wrong one) }
  1345. if assigned(genericdef) and
  1346. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  1347. begin
  1348. { remove the type count suffix from the generic's name }
  1349. for i:=Length(genname) downto 1 do
  1350. if genname[i]='$' then
  1351. begin
  1352. genname:=copy(genname,1,i-1);
  1353. break;
  1354. end;
  1355. { in case of a specialization we've only reached the specialization
  1356. checksum yet }
  1357. if df_specialization in genericdef.defoptions then
  1358. for i:=length(genname) downto 1 do
  1359. if genname[i]='$' then
  1360. begin
  1361. genname:=copy(genname,1,i-1);
  1362. break;
  1363. end;
  1364. end
  1365. else
  1366. begin
  1367. split_generic_name(genname,ugenname,gencount);
  1368. if genname<>ugenname then
  1369. genname:=ugenname;
  1370. end;
  1371. { search a generic with the given count of params }
  1372. countstr:='';
  1373. str(context.paramlist.Count,countstr);
  1374. prettygenname:=genname;
  1375. genname:=genname+'$'+countstr;
  1376. ugenname:=upper(genname);
  1377. context.genname:=genname;
  1378. if assigned(genericdef) then
  1379. symowner:=genericdef.owner
  1380. else
  1381. symowner:=symtable;
  1382. if assigned(symowner) and (symowner.symtabletype in [objectsymtable,recordsymtable]) then
  1383. begin
  1384. if symowner.symtabletype = objectsymtable then
  1385. found:=searchsym_in_class(tobjectdef(symowner.defowner),tobjectdef(symowner.defowner),ugenname,context.sym,context.symtable,[])
  1386. else
  1387. found:=searchsym_in_record(tabstractrecorddef(symowner.defowner),ugenname,context.sym,context.symtable);
  1388. if not found then
  1389. found:=searchsym(ugenname,context.sym,context.symtable);
  1390. end
  1391. else if enforce_unit then
  1392. begin
  1393. if not assigned(symowner) then
  1394. internalerror(2022102101);
  1395. if not (symowner.symtabletype in [globalsymtable,recordsymtable]) then
  1396. internalerror(2022102102);
  1397. hmodule:=find_module_from_symtable(symowner);
  1398. if not assigned(hmodule) then
  1399. internalerror(2022102103);
  1400. found:=searchsym_in_module(hmodule,ugenname,context.sym,context.symtable);
  1401. end
  1402. else
  1403. found:=searchsym(ugenname,context.sym,context.symtable);
  1404. if found and (context.sym.typ=absolutevarsym) and
  1405. (vo_is_funcret in tabstractvarsym(context.sym).varoptions) then
  1406. begin
  1407. { we found the function result alias of a generic function; go up the
  1408. symbol stack *before* this alias was inserted, so that we can
  1409. (hopefully) find the correct generic symbol }
  1410. tmpstack:=tfpobjectlist.create(false);
  1411. while assigned(symtablestack.top) do
  1412. begin
  1413. tmpstack.Add(symtablestack.top);
  1414. symtablestack.pop(symtablestack.top);
  1415. if tmpstack.Last=context.symtable then
  1416. break;
  1417. end;
  1418. if not assigned(symtablestack.top) then
  1419. internalerror(2019123001);
  1420. found:=searchsym(ugenname,context.sym,context.symtable);
  1421. for i:=tmpstack.count-1 downto 0 do
  1422. symtablestack.push(tsymtable(tmpstack[i]));
  1423. tmpstack.free;
  1424. end;
  1425. if not found or not (context.sym.typ in [typesym,procsym]) then
  1426. begin
  1427. identifier_not_found(prettygenname);
  1428. if not try_to_consume(_GT) then
  1429. try_to_consume(_RSHARPBRACKET);
  1430. context.free;
  1431. context:=nil;
  1432. result:=generrordef;
  1433. exit;
  1434. end;
  1435. { we've found the correct def }
  1436. if context.sym.typ=typesym then
  1437. result:=tstoreddef(ttypesym(context.sym).typedef)
  1438. else
  1439. begin
  1440. if tprocsym(context.sym).procdeflist.count=0 then
  1441. internalerror(2015061203);
  1442. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  1443. end;
  1444. if not try_to_consume(_GT) then
  1445. consume(_RSHARPBRACKET);
  1446. end;
  1447. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;const _prettyname:ansistring):tdef;
  1448. procedure unset_forwarddef(def: tdef);
  1449. var
  1450. st : TSymtable;
  1451. i : longint;
  1452. begin
  1453. case def.typ of
  1454. procdef:
  1455. tprocdef(def).forwarddef:=false;
  1456. objectdef,
  1457. recorddef:
  1458. begin
  1459. st:=def.getsymtable(gs_record);
  1460. for i:=0 to st.deflist.count-1 do
  1461. unset_forwarddef(tdef(st.deflist[i]));
  1462. end;
  1463. else
  1464. ;
  1465. end;
  1466. end;
  1467. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  1468. var
  1469. i : longint;
  1470. begin
  1471. gendef:=nil;
  1472. psym:=nil;
  1473. case sym.typ of
  1474. typesym:
  1475. begin
  1476. gendef:=ttypesym(sym).typedef
  1477. end;
  1478. procsym:
  1479. begin
  1480. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  1481. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  1482. begin
  1483. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  1484. break;
  1485. end;
  1486. psym:=sym;
  1487. end
  1488. else
  1489. internalerror(200710171);
  1490. end;
  1491. end;
  1492. function find_in_hierarchy(def:tdef;generictypelist:tfphashobjectlist):tdef;
  1493. var
  1494. paramdef1,
  1495. paramdef2 : tdef;
  1496. allequal : boolean;
  1497. i : longint;
  1498. begin
  1499. result:=nil;
  1500. while assigned(def) do
  1501. begin
  1502. if (df_generic in def.defoptions) and (def=genericdef) then
  1503. begin
  1504. result:=def;
  1505. break;
  1506. end;
  1507. { the following happens when a routine with its parent struct
  1508. as parameter is specialized as a parameter or result of a
  1509. generic function }
  1510. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  1511. begin
  1512. if tstoreddef(def).genericparas.count=generictypelist.count then
  1513. begin
  1514. allequal:=true;
  1515. for i:=0 to generictypelist.count-1 do
  1516. begin
  1517. if tsym(generictypelist[i]).typ<>tsym(tstoreddef(def).genericparas[i]).typ then
  1518. begin
  1519. allequal:=false;
  1520. break;
  1521. end;
  1522. if tsym(generictypelist[i]).typ=constsym then
  1523. paramdef1:=tconstsym(generictypelist[i]).constdef
  1524. else
  1525. paramdef1:=ttypesym(generictypelist[i]).typedef;
  1526. if tsym(tstoreddef(def).genericparas[i]).typ=constsym then
  1527. paramdef2:=tconstsym(tstoreddef(def).genericparas[i]).constdef
  1528. else
  1529. paramdef2:=ttypesym(tstoreddef(def).genericparas[i]).typedef;
  1530. if not equal_defs(paramdef1,paramdef2) then
  1531. begin
  1532. allequal:=false;
  1533. break;
  1534. end;
  1535. if (tsym(generictypelist[i]).typ=constsym) and
  1536. (
  1537. (tconstsym(generictypelist[i]).consttyp<>tconstsym(tstoreddef(def).genericparas[i]).consttyp) or
  1538. not same_constvalue(tconstsym(generictypelist[i]).consttyp,tconstsym(generictypelist[i]).value,tconstsym(tstoreddef(def).genericparas[i]).value)
  1539. ) then
  1540. begin
  1541. allequal:=false;
  1542. break;
  1543. end;
  1544. end;
  1545. if allequal then
  1546. begin
  1547. result:=def;
  1548. break;
  1549. end;
  1550. end;
  1551. end;
  1552. if assigned(def.owner) then
  1553. def:=tstoreddef(def.owner.defowner)
  1554. else
  1555. def:=nil;
  1556. end;
  1557. end;
  1558. var
  1559. finalspecializename,
  1560. ufinalspecializename : tidstring;
  1561. hierarchy,
  1562. prettyname : ansistring;
  1563. generictypelist : tfphashobjectlist;
  1564. srsymtable,
  1565. specializest : tsymtable;
  1566. hashedid : thashedidstring;
  1567. tempst : tglobalsymtable;
  1568. tsrsym : ttypesym;
  1569. psym,
  1570. srsym : tsym;
  1571. flags : thccflags;
  1572. paramdef1,
  1573. paramdef2,
  1574. def : tdef;
  1575. old_block_type : tblock_type;
  1576. state : tspecializationstate;
  1577. old_current_structdef : tabstractrecorddef;
  1578. old_current_specializedef,
  1579. old_current_genericdef : tstoreddef;
  1580. old_current_procinfo : tprocinfo;
  1581. old_module_procinfo : tobject;
  1582. hmodule : tmodule;
  1583. oldcurrent_filepos : tfileposinfo;
  1584. recordbuf : tdynamicarray;
  1585. hadtypetoken : boolean;
  1586. i,
  1587. replaydepth : longint;
  1588. item : tobject;
  1589. allequal,
  1590. hintsprocessed : boolean;
  1591. pd : tprocdef;
  1592. pdflags : tpdflags;
  1593. ppflags : tparse_proc_flags;
  1594. begin
  1595. if not assigned(context) then
  1596. internalerror(2015052203);
  1597. result:=nil;
  1598. pd:=nil;
  1599. hmodule:=nil;
  1600. if not check_generic_constraints(genericdef,context.paramlist,context.poslist) then
  1601. begin
  1602. { the parameters didn't fit the constraints, so don't continue with the
  1603. specialization }
  1604. result:=generrordef;
  1605. exit;
  1606. end;
  1607. { build the new type's name }
  1608. hierarchy:=genericdef.ownerhierarchyname;
  1609. if assigned(genericdef.owner) then
  1610. begin
  1611. hmodule:=find_module_from_symtable(genericdef.owner);
  1612. if not assigned(hmodule) then
  1613. internalerror(2022102801);
  1614. if hierarchy<>'' then
  1615. hierarchy:='.'+hierarchy;
  1616. hierarchy:=hmodule.modulename^+hierarchy;
  1617. end;
  1618. finalspecializename:=generate_generic_name(context.genname,context.specializename,hierarchy);
  1619. ufinalspecializename:=upper(finalspecializename);
  1620. if genericdef.typ=procdef then
  1621. prettyname:=tprocdef(genericdef).procsym.prettyname
  1622. else
  1623. prettyname:=genericdef.typesym.prettyname;
  1624. prettyname:=prettyname+'<'+context.prettyname+'>';
  1625. generictypelist:=tfphashobjectlist.create(false);
  1626. { build the list containing the types for the generic params }
  1627. if not assigned(genericdef.genericparas) then
  1628. internalerror(2013092601);
  1629. if context.paramlist.count<>genericdef.genericparas.count then
  1630. internalerror(2013092603);
  1631. for i:=0 to genericdef.genericparas.Count-1 do
  1632. begin
  1633. srsym:=tsym(genericdef.genericparas[i]);
  1634. if not (sp_generic_para in srsym.symoptions) then
  1635. internalerror(2013092602);
  1636. generictypelist.add(srsym.realname,context.paramlist[i]);
  1637. end;
  1638. { Special case if we are referencing the current defined object }
  1639. if assigned(current_structdef) and
  1640. (current_structdef.objname^=ufinalspecializename) then
  1641. result:=current_structdef;
  1642. { Can we reuse an already specialized type? }
  1643. { for this first check whether we are currently specializing a nested
  1644. type of the current (main) specialization (this is necessary, because
  1645. during that time the symbol of the main specialization will still
  1646. contain a reference to an errordef) }
  1647. if not assigned(result) and assigned(current_specializedef) then
  1648. begin
  1649. def:=current_specializedef;
  1650. repeat
  1651. if def.typ in [objectdef,recorddef] then
  1652. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  1653. result:=def;
  1654. break;
  1655. end;
  1656. if assigned(def.owner) then
  1657. def:=tstoreddef(def.owner.defowner)
  1658. else
  1659. { this can happen when specializing a generic function }
  1660. def:=nil;
  1661. until not assigned(def) or not (df_specialization in def.defoptions);
  1662. end;
  1663. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  1664. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  1665. parsing anyway), so for now we treat those still as generic defs without doing a partial
  1666. specialization }
  1667. if not assigned(result) then
  1668. begin
  1669. def:=current_genericdef;
  1670. if def=genericdef then
  1671. result:=def
  1672. else if assigned(current_genericdef) then
  1673. result:=find_in_hierarchy(current_genericdef,generictypelist);
  1674. if not assigned(result) and assigned(current_specializedef) then
  1675. result:=find_in_hierarchy(current_specializedef,generictypelist);
  1676. end;
  1677. { decide in which symtable to put the specialization }
  1678. if assigned(context.forwarddef) then
  1679. begin
  1680. specializest:=context.forwarddef.owner;
  1681. end
  1682. else if parse_generic and not assigned(result) then
  1683. begin
  1684. srsymtable:=symtablestack.top;
  1685. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  1686. { if we are currently specializing a routine we need to specialize into
  1687. the routine's local- or parasymtable so that they are correctly
  1688. registered should the specialization be finalized }
  1689. specializest:=srsymtable
  1690. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  1691. { if we are parsing the definition of a method we specialize into
  1692. the local symtable of it }
  1693. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  1694. else
  1695. begin
  1696. if not assigned(current_genericdef) then
  1697. internalerror(2014050901);
  1698. { we specialize the partial specialization into the symtable of the currently parsed
  1699. generic }
  1700. case current_genericdef.typ of
  1701. procvardef:
  1702. specializest:=current_genericdef.getsymtable(gs_para);
  1703. procdef:
  1704. specializest:=current_genericdef.getsymtable(gs_local);
  1705. objectdef,
  1706. recorddef:
  1707. specializest:=current_genericdef.getsymtable(gs_record);
  1708. arraydef:
  1709. specializest:=tarraydef(current_genericdef).symtable;
  1710. else
  1711. internalerror(2014050902);
  1712. end;
  1713. end;
  1714. end
  1715. else
  1716. if current_module.is_unit and current_module.in_interface then
  1717. specializest:=current_module.globalsymtable
  1718. else
  1719. specializest:=current_module.localsymtable;
  1720. if not assigned(specializest) then
  1721. internalerror(2014050910);
  1722. { now check whether there is a specialization somewhere else }
  1723. psym:=nil;
  1724. if not assigned(result) then
  1725. begin
  1726. hashedid.id:=ufinalspecializename;
  1727. if (specializest.symtabletype=objectsymtable) and not assigned(context.forwarddef) then
  1728. begin
  1729. { search also in parent classes }
  1730. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  1731. internalerror(2016112901);
  1732. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  1733. srsym:=nil;
  1734. end
  1735. else
  1736. srsym:=tsym(specializest.findwithhash(hashedid));
  1737. if assigned(context.forwarddef) then
  1738. begin
  1739. { just do a few sanity checks }
  1740. if not assigned(srsym) or not (srsym.typ=typesym) then
  1741. internalerror(2020070306);
  1742. if ttypesym(srsym).typedef<>context.forwarddef then
  1743. internalerror(2020070307);
  1744. end
  1745. else if assigned(srsym) then
  1746. begin
  1747. retrieve_genericdef_or_procsym(srsym,result,psym);
  1748. end
  1749. else
  1750. { the generic could have been specialized in the globalsymtable
  1751. already, so search there as well }
  1752. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  1753. begin
  1754. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  1755. if assigned(srsym) then
  1756. begin
  1757. retrieve_genericdef_or_procsym(srsym,result,psym);
  1758. end;
  1759. end;
  1760. end;
  1761. if not assigned(result) then
  1762. begin
  1763. specialization_init(genericdef,state);
  1764. { push a temporary global symtable so that the specialization is
  1765. added to the correct symtable; this symtable does not contain
  1766. any other symbols, so that the type resolution can not be
  1767. influenced by symbols in the current unit }
  1768. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  1769. symtablestack.push(tempst);
  1770. { Reparse the original type definition }
  1771. begin
  1772. old_current_specializedef:=nil;
  1773. old_current_genericdef:=nil;
  1774. old_current_structdef:=nil;
  1775. old_current_procinfo:=current_procinfo;
  1776. old_module_procinfo:=current_module.procinfo;
  1777. current_procinfo:=nil;
  1778. current_module.procinfo:=nil;
  1779. if parse_class_parent then
  1780. begin
  1781. old_current_structdef:=current_structdef;
  1782. old_current_genericdef:=current_genericdef;
  1783. old_current_specializedef:=current_specializedef;
  1784. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  1785. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  1786. else
  1787. current_structdef:=nil;
  1788. current_genericdef:=nil;
  1789. current_specializedef:=nil;
  1790. end;
  1791. maybe_add_waiting_unit(genericdef);
  1792. { First a new sym so we can reuse this specialization and
  1793. references to this specialization can be handled }
  1794. if genericdef.typ=procdef then
  1795. if assigned(psym) then
  1796. srsym:=psym
  1797. else
  1798. srsym:=cprocsym.create(finalspecializename)
  1799. else
  1800. srsym:=ctypesym.create(finalspecializename,generrordef);
  1801. { insert the symbol only if we don't know already that we have
  1802. a procsym to add it to and we aren't dealing with a forwarddef }
  1803. if not assigned(psym) and not assigned(context.forwarddef) then
  1804. specializest.insertsym(srsym);
  1805. { specializations are declarations as such it is the wisest to
  1806. declare set the blocktype to "type"; otherwise we'll
  1807. experience unexpected side effects like the addition of
  1808. classrefdefs if we have a generic that's derived from another
  1809. generic }
  1810. old_block_type:=block_type;
  1811. block_type:=bt_type;
  1812. if (
  1813. (genericdef.typ=procdef) and
  1814. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  1815. ) or (
  1816. (genericdef.typ<>procdef) and
  1817. not assigned(genericdef.generictokenbuf)
  1818. ) then
  1819. internalerror(200511171);
  1820. if hmodule=nil then
  1821. internalerror(2012051202);
  1822. oldcurrent_filepos:=current_filepos;
  1823. { use the index the module got from the current compilation process }
  1824. current_filepos.moduleindex:=hmodule.unit_index;
  1825. current_tokenpos:=current_filepos;
  1826. if parse_generic then
  1827. begin
  1828. recordbuf:=current_scanner.recordtokenbuf;
  1829. current_scanner.recordtokenbuf:=nil;
  1830. end
  1831. else
  1832. recordbuf:=nil;
  1833. replaydepth:=current_scanner.replay_stack_depth;
  1834. if genericdef.typ=procdef then
  1835. begin
  1836. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf,hmodule.change_endian);
  1837. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,[],genericdef,generictypelist,pd);
  1838. if assigned(pd) then
  1839. begin
  1840. if assigned(psym) then
  1841. pd.procsym:=psym
  1842. else
  1843. pd.procsym:=srsym;
  1844. ppflags:=[];
  1845. if po_classmethod in tprocdef(genericdef).procoptions then
  1846. include(ppflags,ppf_classmethod);
  1847. parse_proc_dec_finish(pd,ppflags,tprocdef(genericdef).struct);
  1848. end;
  1849. result:=pd;
  1850. end
  1851. else
  1852. begin
  1853. current_scanner.startreplaytokens(genericdef.generictokenbuf,hmodule.change_endian);
  1854. if assigned(context.forwarddef) then
  1855. begin
  1856. tsrsym:=nil;
  1857. result:=parse_forward_declaration(context.forwarddef.typesym,ufinalspecializename,finalspecializename,genericdef,generictypelist,tsrsym);
  1858. srsym:=tsrsym;
  1859. end
  1860. else
  1861. begin
  1862. hadtypetoken:=false;
  1863. { ensure a pretty name for error messages, might be chanced below }
  1864. if _prettyname<>'' then
  1865. ttypesym(srsym).fprettyname:=_prettyname
  1866. else
  1867. ttypesym(srsym).fprettyname:=prettyname;
  1868. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  1869. ttypesym(srsym).typedef:=result;
  1870. result.typesym:=srsym;
  1871. end;
  1872. if _prettyname<>'' then
  1873. ttypesym(result.typesym).fprettyname:=_prettyname
  1874. else
  1875. ttypesym(result.typesym).fprettyname:=prettyname;
  1876. end;
  1877. current_filepos:=oldcurrent_filepos;
  1878. { Note regarding hint directives:
  1879. There is no need to remove the flags for them from the
  1880. specialized generic symbol, because hint directives that
  1881. follow the specialization are handled by the code in
  1882. pdecl.types_dec and added to the type symbol.
  1883. E.g.: TFoo = TBar<Blubb> deprecated;
  1884. Here the symbol TBar$1$Blubb will contain the
  1885. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1886. case result.typ of
  1887. { Build VMT indexes for classes and read hint directives }
  1888. objectdef:
  1889. begin
  1890. if replaydepth<current_scanner.replay_stack_depth then
  1891. begin
  1892. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1893. if replaydepth<current_scanner.replay_stack_depth then
  1894. consume(_SEMICOLON);
  1895. end;
  1896. if oo_is_forward in tobjectdef(result).objectoptions then
  1897. add_forward_generic_def(result,context)
  1898. else
  1899. build_vmt(tobjectdef(result));
  1900. end;
  1901. { handle params, calling convention, etc }
  1902. procvardef:
  1903. begin
  1904. hintsprocessed:=false;
  1905. if replaydepth<current_scanner.replay_stack_depth then
  1906. begin
  1907. if not check_proc_directive(true) then
  1908. begin
  1909. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1910. if replaydepth<current_scanner.replay_stack_depth then
  1911. consume(_SEMICOLON);
  1912. end
  1913. else
  1914. hintsprocessed:=true;
  1915. end;
  1916. if replaydepth<current_scanner.replay_stack_depth then
  1917. parse_proctype_directives(tprocvardef(result));
  1918. if po_is_function_ref in tprocvardef(result).procoptions then
  1919. adjust_funcref(result,srsym,nil);
  1920. if result.typ=procvardef then
  1921. flags:=hcc_default_actions_intf
  1922. else
  1923. flags:=hcc_default_actions_intf_struct;
  1924. handle_calling_convention(result,flags);
  1925. if not hintsprocessed and (replaydepth<current_scanner.replay_stack_depth) then
  1926. begin
  1927. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1928. if replaydepth<current_scanner.replay_stack_depth then
  1929. consume(_SEMICOLON);
  1930. end;
  1931. end;
  1932. procdef:
  1933. begin
  1934. pdflags:=[];
  1935. if genericdef.owner.symtabletype=objectsymtable then
  1936. include(pdflags,pd_object)
  1937. else if genericdef.owner.symtabletype=recordsymtable then
  1938. include(pdflags,pd_record);
  1939. parse_proc_directives(pd,pdflags);
  1940. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1941. consume(_SEMICOLON);
  1942. if parse_generic then
  1943. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1944. else
  1945. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1946. proc_add_definition(tprocdef(result));
  1947. { for partial specializations we implicitely declare the routine as
  1948. having its implementation although we'll not specialize it in reality }
  1949. if parse_generic then
  1950. unset_forwarddef(result);
  1951. end;
  1952. else
  1953. { parse hint directives for records and arrays }
  1954. if replaydepth<current_scanner.replay_stack_depth then begin
  1955. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1956. if replaydepth<current_scanner.replay_stack_depth then
  1957. consume(_SEMICOLON);
  1958. end;
  1959. end;
  1960. { Consume the remainder of the buffer }
  1961. while current_scanner.replay_stack_depth>replaydepth do
  1962. consume(token);
  1963. if assigned(recordbuf) then
  1964. begin
  1965. if assigned(current_scanner.recordtokenbuf) then
  1966. internalerror(2014050909);
  1967. current_scanner.recordtokenbuf:=recordbuf;
  1968. end;
  1969. block_type:=old_block_type;
  1970. current_procinfo:=old_current_procinfo;
  1971. current_module.procinfo:=old_module_procinfo;
  1972. if parse_class_parent then
  1973. begin
  1974. current_structdef:=old_current_structdef;
  1975. current_genericdef:=old_current_genericdef;
  1976. current_specializedef:=old_current_specializedef;
  1977. end;
  1978. end;
  1979. { extract all created symbols and defs from the temporary symtable
  1980. and add them to the specializest }
  1981. for i:=tempst.SymList.Count-1 downto 0 do
  1982. begin
  1983. item:=tempst.SymList.Items[i];
  1984. { using changeowner the symbol is automatically added to the
  1985. new symtable }
  1986. tsym(item).ChangeOwner(specializest);
  1987. end;
  1988. for i:=tempst.DefList.Count-1 downto 0 do
  1989. begin
  1990. item:=tempst.DefList.Items[i];
  1991. { using changeowner the def is automatically added to the new
  1992. symtable }
  1993. tdef(item).ChangeOwner(specializest);
  1994. { for partial specializations we implicitely declare any methods as having their
  1995. implementations although we'll not specialize them in reality }
  1996. if parse_generic then
  1997. unset_forwarddef(tdef(item));
  1998. end;
  1999. { if a generic was declared during the specialization we need to
  2000. flag the specialize symtable accordingly }
  2001. if sto_has_generic in tempst.tableoptions then
  2002. specializest.includeoption(sto_has_generic);
  2003. tempst.free;
  2004. specialization_done(state);
  2005. { procdefs are only added once we know which overload we use }
  2006. if not parse_generic and (result.typ<>procdef) then
  2007. current_module.pendingspecializations.add(result.typename,result);
  2008. end;
  2009. generictypelist.free;
  2010. if assigned(genericdef) then
  2011. begin
  2012. { check the hints of the found generic symbol }
  2013. if genericdef.typ=procdef then
  2014. srsym:=tprocdef(genericdef).procsym
  2015. else
  2016. srsym:=genericdef.typesym;
  2017. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2018. end;
  2019. end;
  2020. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string;parsedtype:tdef;const symname:string;parsedpos:tfileposinfo);
  2021. var
  2022. context : tspecializationcontext;
  2023. genericdef : tstoreddef;
  2024. begin
  2025. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,enforce_unit,parsedtype,symname,nil,parsedpos));
  2026. if genericdef<>generrordef then
  2027. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  2028. tt:=genericdef;
  2029. if assigned(context) then
  2030. context.free;
  2031. end;
  2032. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  2033. var
  2034. generictype : tstoredsym;
  2035. i,firstidx,const_list_index : longint;
  2036. srsymtable : tsymtable;
  2037. basedef,def : tdef;
  2038. defname : tidstring;
  2039. allowconst,
  2040. allowconstructor,
  2041. is_const,
  2042. doconsume : boolean;
  2043. constraintdata : tgenericconstraintdata;
  2044. old_block_type : tblock_type;
  2045. fileinfo : tfileposinfo;
  2046. begin
  2047. result:=tfphashobjectlist.create(false);
  2048. firstidx:=0;
  2049. const_list_index:=0;
  2050. old_block_type:=block_type;
  2051. block_type:=bt_type;
  2052. allowconst:=true;
  2053. is_const:=false;
  2054. repeat
  2055. if allowconst and try_to_consume(_CONST) then
  2056. begin
  2057. allowconst:=false;
  2058. is_const:=true;
  2059. const_list_index:=result.count;
  2060. end;
  2061. if token=_ID then
  2062. begin
  2063. if is_const then
  2064. generictype:=cconstsym.create_undefined(orgpattern,cundefinedtype)
  2065. else
  2066. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  2067. { type parameters need to be added as strict private }
  2068. generictype.visibility:=vis_strictprivate;
  2069. include(generictype.symoptions,sp_generic_para);
  2070. result.add(orgpattern,generictype);
  2071. end;
  2072. consume(_ID);
  2073. fileinfo:=current_tokenpos;
  2074. { const restriction }
  2075. if is_const and try_to_consume(_COLON) then
  2076. begin
  2077. def:=nil;
  2078. { parse the type and assign the const type to generictype }
  2079. single_type(def,[]);
  2080. for i:=const_list_index to result.count-1 do
  2081. begin
  2082. { finalize constant information once type is known }
  2083. if assigned(def) and (def.typ in tgeneric_param_const_types) then
  2084. begin
  2085. case def.typ of
  2086. orddef,
  2087. enumdef:
  2088. tconstsym(result[i]).consttyp:=constord;
  2089. stringdef:
  2090. tconstsym(result[i]).consttyp:=conststring;
  2091. floatdef:
  2092. tconstsym(result[i]).consttyp:=constreal;
  2093. setdef:
  2094. tconstsym(result[i]).consttyp:=constset;
  2095. { pointer always refers to nil with constants }
  2096. pointerdef:
  2097. tconstsym(result[i]).consttyp:=constnil;
  2098. else
  2099. internalerror(2020011402);
  2100. end;
  2101. tconstsym(result[i]).constdef:=def;
  2102. end
  2103. else
  2104. Message1(type_e_generic_const_type_not_allowed,def.fulltypename);
  2105. end;
  2106. { after type restriction const list terminates }
  2107. is_const:=false;
  2108. end
  2109. { type restriction }
  2110. else if try_to_consume(_COLON) then
  2111. begin
  2112. if not allowconstraints then
  2113. Message(parser_e_generic_constraints_not_allowed_here);
  2114. { construct a name which can be used for a type specification }
  2115. constraintdata:=tgenericconstraintdata.create;
  2116. constraintdata.fileinfo:=fileinfo;
  2117. defname:='';
  2118. str(current_module.deflist.count,defname);
  2119. defname:='$gendef'+defname;
  2120. allowconstructor:=m_delphi in current_settings.modeswitches;
  2121. basedef:=generrordef;
  2122. repeat
  2123. doconsume:=true;
  2124. case token of
  2125. _CONSTRUCTOR:
  2126. begin
  2127. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  2128. Message(parser_e_illegal_expression);
  2129. include(constraintdata.flags,gcf_constructor);
  2130. allowconstructor:=false;
  2131. end;
  2132. _CLASS:
  2133. begin
  2134. if gcf_class in constraintdata.flags then
  2135. Message(parser_e_illegal_expression);
  2136. if basedef=generrordef then
  2137. include(constraintdata.flags,gcf_class)
  2138. else
  2139. Message(parser_e_illegal_expression);
  2140. end;
  2141. _RECORD:
  2142. begin
  2143. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  2144. or (constraintdata.interfaces.count>0) then
  2145. Message(parser_e_illegal_expression)
  2146. else
  2147. begin
  2148. srsymtable:=trecordsymtable.create(defname,0,1);
  2149. basedef:=crecorddef.create(defname,srsymtable);
  2150. include(constraintdata.flags,gcf_record);
  2151. allowconstructor:=false;
  2152. end;
  2153. end;
  2154. else
  2155. begin
  2156. { after single_type "token" is the trailing ",", ";" or
  2157. ">"! }
  2158. doconsume:=false;
  2159. { def is already set to a class or record }
  2160. if gcf_record in constraintdata.flags then
  2161. Message(parser_e_illegal_expression);
  2162. single_type(def, [stoAllowSpecialization]);
  2163. { only types that are inheritable are allowed }
  2164. if (def.typ<>objectdef) or
  2165. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  2166. Message1(type_e_class_or_interface_type_expected,def.typename)
  2167. else
  2168. case tobjectdef(def).objecttype of
  2169. odt_class,
  2170. odt_javaclass:
  2171. begin
  2172. if gcf_class in constraintdata.flags then
  2173. { "class" + concrete class is not allowed }
  2174. Message(parser_e_illegal_expression)
  2175. else
  2176. { do we already have a concrete class? }
  2177. if basedef<>generrordef then
  2178. Message(parser_e_illegal_expression)
  2179. else
  2180. basedef:=def;
  2181. end;
  2182. odt_interfacecom,
  2183. odt_interfacecorba,
  2184. odt_interfacejava,
  2185. odt_dispinterface:
  2186. constraintdata.interfaces.add(def);
  2187. else
  2188. ;
  2189. end;
  2190. end;
  2191. end;
  2192. if doconsume then
  2193. consume(token);
  2194. until not try_to_consume(_COMMA);
  2195. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  2196. (constraintdata.interfaces.count>1) or
  2197. (
  2198. (basedef.typ=objectdef) and
  2199. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  2200. ) then
  2201. begin
  2202. if basedef.typ=errordef then
  2203. { don't pass an errordef as a parent to a tobjectdef }
  2204. basedef:=class_tobject
  2205. else
  2206. if (basedef.typ<>objectdef) or
  2207. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  2208. internalerror(2012101101);
  2209. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  2210. for i:=0 to constraintdata.interfaces.count-1 do
  2211. tobjectdef(basedef).register_implemented_interface(tobjectdef(constraintdata.interfaces[i]),false);
  2212. end
  2213. else
  2214. if constraintdata.interfaces.count=1 then
  2215. begin
  2216. if basedef.typ<>errordef then
  2217. internalerror(2013021601);
  2218. def:=tdef(constraintdata.interfaces[0]);
  2219. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  2220. constraintdata.interfaces.delete(0);
  2221. end;
  2222. if basedef.typ<>errordef then
  2223. with tstoreddef(basedef) do
  2224. begin
  2225. genconstraintdata:=tgenericconstraintdata.create;
  2226. genconstraintdata.flags:=constraintdata.flags;
  2227. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  2228. genconstraintdata.fileinfo:=constraintdata.fileinfo;
  2229. include(defoptions,df_genconstraint);
  2230. end;
  2231. for i:=firstidx to result.count-1 do
  2232. ttypesym(result[i]).typedef:=basedef;
  2233. { we need a typesym in case we do a Delphi-mode inline
  2234. specialization with this parameter; so just use the first sym }
  2235. if not assigned(basedef.typesym) then
  2236. basedef.typesym:=ttypesym(result[firstidx]);
  2237. firstidx:=result.count;
  2238. constraintdata.free;
  2239. end
  2240. else
  2241. begin
  2242. if token=_SEMICOLON then
  2243. begin
  2244. { two different typeless parameters are considered as incompatible }
  2245. for i:=firstidx to result.count-1 do
  2246. if tsym(result[i]).typ<>constsym then
  2247. begin
  2248. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  2249. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  2250. end;
  2251. { a semicolon terminates a type parameter group }
  2252. firstidx:=result.count;
  2253. end;
  2254. end;
  2255. if token=_SEMICOLON then
  2256. begin
  2257. is_const:=false;
  2258. allowconst:=true;
  2259. end;
  2260. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  2261. { if the constant parameter is not terminated then the type restriction was
  2262. not specified and we need to give an error }
  2263. if is_const then
  2264. consume(_COLON);
  2265. { two different typeless parameters are considered as incompatible }
  2266. for i:=firstidx to result.count-1 do
  2267. if tsym(result[i]).typ<>constsym then
  2268. begin
  2269. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  2270. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  2271. end;
  2272. block_type:=old_block_type;
  2273. end;
  2274. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist;isfwd:boolean);
  2275. var
  2276. i : longint;
  2277. generictype,
  2278. fwdparam : tstoredsym;
  2279. generictypedef : tdef;
  2280. sym : tsym;
  2281. st : tsymtable;
  2282. fwdok : boolean;
  2283. conv : tconverttype;
  2284. op : tprocdef;
  2285. begin
  2286. def.genericdef:=genericdef;
  2287. if not assigned(genericlist) then
  2288. exit;
  2289. if assigned(genericdef) then
  2290. include(def.defoptions,df_specialization)
  2291. else
  2292. if genericlist.count>0 then
  2293. include(def.defoptions,df_generic);
  2294. case def.typ of
  2295. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  2296. arraydef: st:=tarraydef(def).symtable;
  2297. procvardef,procdef: st:=tabstractprocdef(def).parast;
  2298. else
  2299. internalerror(201101020);
  2300. end;
  2301. { if we have a forwarddef we check whether the generic parameters are
  2302. equal and otherwise ignore the list }
  2303. if isfwd then
  2304. begin
  2305. fwdok:=true;
  2306. if (genericlist.count>0) and
  2307. (
  2308. not assigned(def.genericparas)
  2309. or (def.genericparas.count<>genericlist.count)
  2310. ) then
  2311. fwdok:=false
  2312. else
  2313. begin
  2314. for i:=0 to genericlist.count-1 do
  2315. begin
  2316. if def.genericparas.nameofindex(i)<>genericlist.nameofindex(i) then
  2317. begin
  2318. fwdok:=false;
  2319. break;
  2320. end;
  2321. generictype:=tstoredsym(genericlist[i]);
  2322. fwdparam:=tstoredsym(def.genericparas[i]);
  2323. op:=nil;
  2324. conv:=tc_equal;
  2325. if generictype.typ<>fwdparam.typ then
  2326. fwdok:=false
  2327. else if (generictype.typ=typesym) then
  2328. begin
  2329. if compare_defs_ext(ttypesym(generictype).typedef,ttypesym(fwdparam).typedef,nothingn,conv,op,[cdo_strict_genconstraint_check])<te_exact then
  2330. fwdok:=false;
  2331. end
  2332. else if (generictype.typ=constsym) then
  2333. begin
  2334. if (tconstsym(generictype).consttyp<>tconstsym(fwdparam).consttyp) or
  2335. (compare_defs_ext(tconstsym(generictype).constdef,tconstsym(fwdparam).constdef,nothingn,conv,op,[cdo_strict_genconstraint_check])<te_exact) then
  2336. fwdok:=false;
  2337. end
  2338. else
  2339. internalerror(2020070101);
  2340. if not fwdok then
  2341. break;
  2342. end;
  2343. end;
  2344. if not fwdok then
  2345. Message(parser_e_forward_mismatch);
  2346. exit;
  2347. end;
  2348. if (genericlist.count>0) and not assigned(def.genericparas) then
  2349. def.genericparas:=tfphashobjectlist.create(false);
  2350. for i:=0 to genericlist.count-1 do
  2351. begin
  2352. generictype:=tstoredsym(genericlist[i]);
  2353. if assigned(generictype.owner) then
  2354. begin
  2355. if generictype.typ=typesym then
  2356. sym:=ctypesym.create(genericlist.nameofindex(i),ttypesym(generictype).typedef)
  2357. else if generictype.typ=constsym then
  2358. { generictype is a constsym that was created in create_generic_constsym
  2359. during phase 1 so we pass this directly without copying }
  2360. begin
  2361. sym:=generictype;
  2362. { the sym name is still undefined so we set it to match
  2363. the generic param name so it's accessible }
  2364. sym.realname:=genericlist.nameofindex(i);
  2365. include(sym.symoptions,sp_generic_const);
  2366. end
  2367. else
  2368. internalerror(2019021602);
  2369. { type parameters need to be added as strict private }
  2370. sym.visibility:=vis_strictprivate;
  2371. st.insertsym(sym);
  2372. include(sym.symoptions,sp_generic_para);
  2373. end
  2374. else
  2375. begin
  2376. if generictype.typ=typesym then
  2377. begin
  2378. generictypedef:=ttypesym(generictype).typedef;
  2379. if (generictypedef.typ=undefineddef) and (generictypedef<>cundefinedtype) then
  2380. begin
  2381. { the generic parameters were parsed before the genericdef existed thus the
  2382. undefineddefs were added as part of the parent symtable }
  2383. if assigned(generictypedef.owner) then
  2384. generictypedef.owner.DefList.Extract(generictypedef);
  2385. generictypedef.changeowner(st);
  2386. end;
  2387. end;
  2388. st.insertsym(generictype);
  2389. include(generictype.symoptions,sp_generic_para);
  2390. end;
  2391. def.genericparas.add(genericlist.nameofindex(i),generictype);
  2392. end;
  2393. end;
  2394. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  2395. var
  2396. gensym : ttypesym;
  2397. begin
  2398. { for generics in non-Delphi modes we insert a private type symbol
  2399. that has the same base name as the currently parsed generic and
  2400. that references this defs }
  2401. if not (m_delphi in current_settings.modeswitches) and
  2402. (
  2403. (
  2404. parse_generic and
  2405. assigned(genericlist) and
  2406. (genericlist.count>0)
  2407. ) or
  2408. (
  2409. assigned(current_specializedef) and
  2410. assigned(current_structdef.genericdef) and
  2411. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  2412. (pos('$',name)>0)
  2413. )
  2414. ) then
  2415. begin
  2416. { we need to pass nil as def here, because the constructor wants
  2417. to set the typesym of the def which is not what we want }
  2418. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  2419. gensym.typedef:=current_structdef;
  2420. include(gensym.symoptions,sp_internal);
  2421. { the symbol should be only visible to the generic class
  2422. itself }
  2423. gensym.visibility:=vis_strictprivate;
  2424. symtablestack.top.insertsym(gensym);
  2425. end;
  2426. end;
  2427. function generate_generic_name(const name:tidstring;const specializename:ansistring;const owner_hierarchy:ansistring):tidstring;
  2428. var
  2429. crc : cardinal;
  2430. begin
  2431. if specializename='' then
  2432. internalerror(2012061901);
  2433. { build the new type's name }
  2434. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  2435. result:=name+'$crc'+hexstr(crc,8);
  2436. if owner_hierarchy<>'' then
  2437. begin
  2438. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  2439. result:=result+'_crc'+hexstr(crc,8);
  2440. end;
  2441. end;
  2442. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  2443. var
  2444. i,code : longint;
  2445. countstr : string;
  2446. begin
  2447. for i:=length(name) downto 1 do
  2448. if name[i]='$' then
  2449. begin
  2450. nongeneric:=copy(name,1,i-1);
  2451. countstr:=copy(name,i+1,length(name)-i);
  2452. val(countstr,count,code);
  2453. if code<>0 then
  2454. break;
  2455. exit;
  2456. end;
  2457. nongeneric:=name;
  2458. count:=0;
  2459. end;
  2460. procedure add_generic_dummysym(sym:tsym);
  2461. var
  2462. list: TFPObjectList;
  2463. srsym : tsym;
  2464. srsymtable : tsymtable;
  2465. entry : tgenericdummyentry;
  2466. begin
  2467. if sp_generic_dummy in sym.symoptions then
  2468. begin
  2469. { did we already search for a generic with that name? }
  2470. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  2471. if not assigned(list) then
  2472. begin
  2473. list:=tfpobjectlist.create(true);
  2474. current_module.genericdummysyms.add(sym.name,list);
  2475. end;
  2476. { is the dummy sym still "dummy"? }
  2477. if (sym.typ=typesym) and
  2478. (
  2479. { dummy sym defined in mode Delphi }
  2480. (ttypesym(sym).typedef.typ=undefineddef) or
  2481. { dummy sym defined in non-Delphi mode }
  2482. (tstoreddef(ttypesym(sym).typedef).is_generic)
  2483. ) then
  2484. begin
  2485. { do we have a non-generic type of the same name
  2486. available? }
  2487. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  2488. srsym:=nil;
  2489. end
  2490. else if sym.typ=procsym then
  2491. srsym:=sym
  2492. else
  2493. { dummy symbol is already not so dummy anymore }
  2494. srsym:=nil;
  2495. if assigned(srsym) then
  2496. begin
  2497. entry:=tgenericdummyentry.create;
  2498. entry.resolvedsym:=srsym;
  2499. entry.dummysym:=sym;
  2500. list.add(entry);
  2501. end;
  2502. end;
  2503. end;
  2504. function resolve_generic_dummysym(const name:tidstring):tsym;
  2505. var
  2506. list : tfpobjectlist;
  2507. begin
  2508. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  2509. if assigned(list) and (list.count>0) then
  2510. result:=tgenericdummyentry(list.last).resolvedsym
  2511. else
  2512. result:=nil;
  2513. end;
  2514. function could_be_generic(const name:tidstring):boolean;
  2515. begin
  2516. result:=(name<>'') and
  2517. (current_module.genericdummysyms.findindexof(name)>=0);
  2518. end;
  2519. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  2520. var
  2521. pu : tused_unit;
  2522. hmodule : tmodule;
  2523. unitsyms : TFPHashObjectList;
  2524. sym : tsym;
  2525. i : Integer;
  2526. n : string;
  2527. begin
  2528. if not assigned(genericdef) then
  2529. internalerror(200705151);
  2530. { Setup symtablestack at definition time
  2531. to get types right, however this is not perfect, we should probably record
  2532. the resolved symbols }
  2533. state.oldsymtablestack:=symtablestack;
  2534. state.oldextendeddefs:=current_module.extendeddefs;
  2535. state.oldgenericdummysyms:=current_module.genericdummysyms;
  2536. current_module.extendeddefs:=TFPHashObjectList.create(true);
  2537. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  2538. symtablestack:=tdefawaresymtablestack.create;
  2539. if not assigned(genericdef.owner) then
  2540. hmodule:=current_module
  2541. else
  2542. hmodule:=find_module_from_symtable(genericdef.owner);
  2543. if hmodule=nil then
  2544. internalerror(200705152);
  2545. { collect all unit syms in the generic's unit as we need to establish
  2546. their unitsym.module link again so that unit identifiers can be used }
  2547. unitsyms:=tfphashobjectlist.create(false);
  2548. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  2549. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  2550. begin
  2551. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  2552. if sym.typ=unitsym then
  2553. begin
  2554. n:=sym.realname;
  2555. if (Copy(n,1,7)='$hidden') then
  2556. Delete(n,1,7);
  2557. unitsyms.add(upper(n),sym);
  2558. end;
  2559. end;
  2560. { add all units if we are specializing inside the current unit (as the
  2561. generic could have been declared in the implementation part), but load
  2562. only interface units, if we are in a different unit as then the generic
  2563. needs to be in the interface section }
  2564. pu:=tused_unit(hmodule.used_units.first);
  2565. while assigned(pu) do
  2566. begin
  2567. if not assigned(pu.u.globalsymtable) then
  2568. { in certain circular, but valid unit constellations it can happen
  2569. that we specialize a generic in a different unit that was used
  2570. in the implementation section of the generic's unit and were the
  2571. interface is still being parsed and thus the localsymtable is in
  2572. reality the global symtable }
  2573. if pu.u.in_interface then
  2574. begin
  2575. {
  2576. MVC: The case where localsymtable is also nil can appear in complex cases and still produce valid code.
  2577. In order to allow people in this case to continue, SKIP_INTERNAL20231102 can be defined.
  2578. Default behaviour is to raise an internal error.
  2579. See also
  2580. https://gitlab.com/freepascal.org/fpc/source/-/issues/40502
  2581. }
  2582. {$IFDEF SKIP_INTERNAL20231102}
  2583. if (pu.u.localsymtable<>Nil) then
  2584. {$ELSE}
  2585. if (pu.u.localsymtable=Nil) then
  2586. internalerror(20231102);
  2587. {$ENDIF}
  2588. symtablestack.push(pu.u.localsymtable);
  2589. end
  2590. else
  2591. internalerror(200705153)
  2592. else
  2593. symtablestack.push(pu.u.globalsymtable);
  2594. sym:=tsym(unitsyms.find(pu.u.modulename^));
  2595. if assigned(sym) and not assigned(tunitsym(sym).module) then
  2596. tunitsym(sym).module:=pu.u;
  2597. pu:=tused_unit(pu.next);
  2598. end;
  2599. unitsyms.free;
  2600. if assigned(hmodule.globalsymtable) then
  2601. symtablestack.push(hmodule.globalsymtable);
  2602. { push the localsymtable if needed }
  2603. if ((hmodule<>current_module) or not current_module.in_interface)
  2604. and assigned(hmodule.localsymtable) then
  2605. symtablestack.push(hmodule.localsymtable);
  2606. end;
  2607. procedure specialization_done(var state: tspecializationstate);
  2608. begin
  2609. { Restore symtablestack }
  2610. current_module.extendeddefs.free;
  2611. current_module.extendeddefs:=state.oldextendeddefs;
  2612. current_module.genericdummysyms.free;
  2613. current_module.genericdummysyms:=state.oldgenericdummysyms;
  2614. symtablestack.free;
  2615. symtablestack:=state.oldsymtablestack;
  2616. { clear the state record to be on the safe side }
  2617. fillchar(state, sizeof(state), 0);
  2618. end;
  2619. {****************************************************************************
  2620. SPECIALIZATION BODY GENERATION
  2621. ****************************************************************************}
  2622. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  2623. var
  2624. oldcurrent_filepos : tfileposinfo;
  2625. begin
  2626. if assigned(def.genericdef) and
  2627. (def.genericdef.typ=procdef) and
  2628. assigned(tprocdef(def.genericdef).generictokenbuf) then
  2629. begin
  2630. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  2631. internalerror(2015061902);
  2632. oldcurrent_filepos:=current_filepos;
  2633. current_filepos:=tprocdef(def.genericdef).fileinfo;
  2634. { use the index the module got from the current compilation process }
  2635. current_filepos.moduleindex:=hmodule.unit_index;
  2636. current_tokenpos:=current_filepos;
  2637. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf,hmodule.change_endian);
  2638. read_proc_body(def);
  2639. current_filepos:=oldcurrent_filepos;
  2640. end
  2641. { synthetic routines will be implemented afterwards }
  2642. else if def.synthetickind=tsk_none then
  2643. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  2644. end;
  2645. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  2646. var
  2647. i : longint;
  2648. hp : tdef;
  2649. hmodule : tmodule;
  2650. begin
  2651. result:=true;
  2652. hmodule:=nil;
  2653. if assigned(def.genericdef) then
  2654. hmodule:=find_module_from_symtable(def.genericdef.owner)
  2655. else if not (df_internal in def.defoptions) then
  2656. internalerror(201202041);
  2657. for i:=0 to def.symtable.DefList.Count-1 do
  2658. begin
  2659. hp:=tdef(def.symtable.DefList[i]);
  2660. if hp.typ=procdef then
  2661. begin
  2662. { only generate the code if we need a body }
  2663. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  2664. continue;
  2665. { and the body is available already (which is implicitely the
  2666. case if the generic routine is part of another unit) }
  2667. if (
  2668. not assigned(hmodule) or
  2669. (hmodule=current_module) or
  2670. (hmodule.state=ms_compile)
  2671. ) and
  2672. { may not be assigned in case it's a synthetic procdef that
  2673. still needs to be generated }
  2674. (assigned(tprocdef(hp).genericdef) and
  2675. tprocdef(tprocdef(hp).genericdef).forwarddef)
  2676. { when the implementation of the module was not yet parsed, it will not yet have a generictokenbuf }
  2677. or not assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf) then
  2678. begin
  2679. result:=false;
  2680. continue;
  2681. end;
  2682. process_procdef(tprocdef(hp),hmodule);
  2683. end
  2684. else
  2685. if hp.typ in [objectdef,recorddef] then
  2686. { generate code for subtypes as well }
  2687. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  2688. end;
  2689. end;
  2690. procedure generate_specialization_procs;
  2691. var
  2692. i : longint;
  2693. list,
  2694. readdlist : tfpobjectlist;
  2695. def : tstoreddef;
  2696. state : tspecializationstate;
  2697. hmodule : tmodule;
  2698. mstate : tmodulestate;
  2699. begin
  2700. { first copy all entries and then work with that list to ensure that
  2701. we don't get an infinite recursion }
  2702. list:=tfpobjectlist.create(false);
  2703. readdlist:=tfpobjectlist.create(false);
  2704. list.Capacity:=current_module.pendingspecializations.Count;
  2705. for i:=0 to current_module.pendingspecializations.Count-1 do
  2706. list.add(current_module.pendingspecializations.Items[i]);
  2707. current_module.pendingspecializations.clear;
  2708. for i:=0 to list.count-1 do
  2709. begin
  2710. def:=tstoreddef(list[i]);
  2711. if not tstoreddef(def).is_specialization then
  2712. continue;
  2713. case def.typ of
  2714. procdef:
  2715. begin
  2716. { the use of forwarddef should not backfire as the
  2717. specialization always belongs to the current module }
  2718. if not tprocdef(def).forwarddef then
  2719. continue;
  2720. if not assigned(def.genericdef) then
  2721. internalerror(2015061903);
  2722. hmodule:=find_module_from_symtable(def.genericdef.owner);
  2723. if hmodule=nil then
  2724. internalerror(2015061904);
  2725. { we need to check for a forward declaration only if the
  2726. generic was declared in the same unit (otherwise there
  2727. should be one) }
  2728. mstate:=hmodule.state;
  2729. if ((hmodule=current_module) or (hmodule.state<ms_compiling_waitfinish)) and tprocdef(def.genericdef).forwarddef then
  2730. begin
  2731. readdlist.add(def);
  2732. continue;
  2733. end;
  2734. specialization_init(tstoreddef(def).genericdef,state);
  2735. process_procdef(tprocdef(def),hmodule);
  2736. specialization_done(state);
  2737. end;
  2738. recorddef,
  2739. objectdef:
  2740. begin
  2741. specialization_init(tstoreddef(def).genericdef,state);
  2742. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  2743. readdlist.add(def);
  2744. specialization_done(state);
  2745. end;
  2746. else
  2747. ;
  2748. end;
  2749. end;
  2750. { add those defs back to the pending list for which we don't yet have
  2751. all method bodies }
  2752. for i:=0 to readdlist.count-1 do
  2753. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  2754. readdlist.free;
  2755. list.free;
  2756. end;
  2757. procedure generate_specializations_for_forwarddef(def:tdef);
  2758. var
  2759. list : tfpobjectlist;
  2760. idx,
  2761. i : longint;
  2762. context : tspecializationcontext;
  2763. begin
  2764. if not tstoreddef(def).is_generic then
  2765. internalerror(2020070304);
  2766. idx:=current_module.forwardgenericdefs.findindexof(def.fulltypename);
  2767. if idx<0 then
  2768. exit;
  2769. list:=tfpobjectlist(current_module.forwardgenericdefs.items[idx]);
  2770. if not assigned(list) then
  2771. internalerror(2020070305);
  2772. for i:=0 to list.count-1 do begin
  2773. context:=tspecializationcontext(list[i]);
  2774. generate_specialization_phase2(context,tstoreddef(def),false,'');
  2775. end;
  2776. current_module.forwardgenericdefs.delete(idx);
  2777. end;
  2778. procedure maybe_add_pending_specialization(def:tdef;unnamed_syms: tfplist);
  2779. var
  2780. hmodule : tmodule;
  2781. st : tsymtable;
  2782. i : integer;
  2783. begin
  2784. if parse_generic then
  2785. exit;
  2786. { transfer ownership of any unnamed syms to be the specialization }
  2787. if unnamed_syms<>nil then
  2788. transfer_unnamed_symbols(tprocdef(def).parast,unnamed_syms);
  2789. st:=def.owner;
  2790. while st.symtabletype in [localsymtable] do
  2791. st:=st.defowner.owner;
  2792. hmodule:=find_module_from_symtable(st);
  2793. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  2794. current_module.pendingspecializations.add(def.typename,def);
  2795. end;
  2796. function determine_generic_def(const name:tidstring):tstoreddef;
  2797. var
  2798. hashedid : THashedIDString;
  2799. pd : tprocdef;
  2800. sym : tsym;
  2801. begin
  2802. result:=nil;
  2803. { check whether this is a declaration of a type inside a
  2804. specialization }
  2805. if assigned(current_structdef) and
  2806. (df_specialization in current_structdef.defoptions) then
  2807. begin
  2808. if not assigned(current_structdef.genericdef) or
  2809. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  2810. internalerror(2011052301);
  2811. hashedid.id:=name;
  2812. { we could be inside a method of the specialization
  2813. instead of its declaration, so check that first (as
  2814. local nested types aren't allowed we don't need to
  2815. walk the symtablestack to find the localsymtable) }
  2816. if symtablestack.top.symtabletype=localsymtable then
  2817. begin
  2818. { we are in a method }
  2819. if not assigned(symtablestack.top.defowner) or
  2820. (symtablestack.top.defowner.typ<>procdef) then
  2821. internalerror(2011120701);
  2822. pd:=tprocdef(symtablestack.top.defowner);
  2823. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  2824. internalerror(2011120702);
  2825. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  2826. end
  2827. else
  2828. sym:=nil;
  2829. if not assigned(sym) or not (sym.typ=typesym) then
  2830. begin
  2831. { now search in the declaration of the generic }
  2832. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  2833. if not assigned(sym) or not (sym.typ=typesym) then
  2834. internalerror(2011052302);
  2835. end;
  2836. { use the corresponding type in the generic's symtable as
  2837. genericdef for the specialized type }
  2838. result:=tstoreddef(ttypesym(sym).typedef);
  2839. end;
  2840. end;
  2841. end.