pgenutil.pas 123 KB

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