pgenutil.pas 120 KB

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