pgenutil.pas 119 KB

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