pgenutil.pas 123 KB

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