pgenutil.pas 121 KB

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