pgenutil.pas 126 KB

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