pgenutil.pas 126 KB

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