pgenutil.pas 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  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. include(newtype.symoptions,sp_generic_unnamed_type);
  720. newtype.owner:=def.owner;
  721. { ensure that there's no warning }
  722. newtype.refs:=1;
  723. end;
  724. if not assigned(newtype) then
  725. internalerror(2021020904);
  726. result:=newtype;
  727. end;
  728. { searches for the generic param in specializations }
  729. function find_param_in_specialization(owner:tprocdef;genericparam:ttypesym;def:tstoreddef):boolean;
  730. var
  731. parasym: ttypesym;
  732. k, i: integer;
  733. begin
  734. result:=false;
  735. for i:=0 to def.genericparas.count-1 do
  736. begin
  737. parasym:=ttypesym(def.genericparas[i]);
  738. { the generic param must have a named typesym }
  739. if not assigned(parasym.typedef.typesym) then
  740. internalerror(2021020907);
  741. { recurse into inline specialization }
  742. if tstoreddef(parasym.typedef).is_specialization then
  743. begin
  744. result:=find_param_in_specialization(owner,genericparam,tstoreddef(parasym.typedef));
  745. if result then
  746. exit;
  747. end
  748. else if (genericparam=parasym.typedef.typesym) and owner.is_generic_param(parasym.typedef) then
  749. exit(true);
  750. end;
  751. end;
  752. { searches for the generic param in arrays }
  753. function find_param_in_array(owner:tprocdef;genericparam:ttypesym;def:tarraydef):boolean;
  754. var
  755. elementdef:tstoreddef;
  756. begin
  757. elementdef:=tstoreddef(def.elementdef);
  758. { recurse into multi-dimensional array }
  759. if elementdef.typ=arraydef then
  760. result:=find_param_in_array(owner,genericparam,tarraydef(elementdef))
  761. { something went wrong during parsing and the element is invalid }
  762. else if elementdef.typ=errordef then
  763. result:=false
  764. else
  765. begin
  766. { the element must have a named typesym }
  767. if not assigned(elementdef.typesym) then
  768. internalerror(2021020906);
  769. result:=(genericparam=elementdef.typesym) and owner.is_generic_param(elementdef);
  770. end;
  771. end;
  772. { tests if the generic param is used in the parameter list }
  773. function is_generic_param_used(owner:tprocdef;genericparam:ttypesym;paras:tfplist):boolean;
  774. var
  775. paravar:tparavarsym;
  776. i: integer;
  777. begin
  778. result:=false;
  779. for i:=0 to paras.count-1 do
  780. begin
  781. paravar:=tparavarsym(paras[i]);
  782. { handle array types by using element types (for example: array of T) }
  783. if paravar.vardef.typ=arraydef then
  784. result:=find_param_in_array(owner,genericparam,tarraydef(paravar.vardef))
  785. { for specializations check search in generic params }
  786. else if tstoreddef(paravar.vardef).is_specialization then
  787. result:=find_param_in_specialization(owner,genericparam,tstoreddef(paravar.vardef))
  788. { something went wrong during parsing and the parameter is invalid }
  789. else if paravar.vardef.typ=errordef then
  790. exit(false)
  791. else
  792. begin
  793. if not assigned(paravar.vardef.typesym) then
  794. internalerror(2021020905);
  795. result:=(genericparam=paravar.vardef.typesym) and owner.is_generic_param(paravar.vardef)
  796. end;
  797. { exit if we find a used parameter }
  798. if result then
  799. exit;
  800. end;
  801. end;
  802. { handle generic specializations by using generic params from caller
  803. to specialize the target. for example "TRec<Integer>" can use "Integer"
  804. to specialize "TRec<T>" with "Integer" for "T". }
  805. procedure handle_specializations(genericparams:tfphashlist;target_def,caller_def:tstoreddef);
  806. var
  807. i,
  808. index : integer;
  809. key : string;
  810. target_param,
  811. caller_param : ttypesym;
  812. begin
  813. { the target and the caller must the same generic def
  814. with the same set of generic parameters }
  815. if target_def.genericdef<>caller_def.genericdef then
  816. internalerror(2021020909);
  817. for i:=0 to target_def.genericparas.count-1 do
  818. begin
  819. target_param:=ttypesym(target_def.genericparas[i]);
  820. caller_param:=ttypesym(caller_def.genericparas[i]);
  821. { reject generics with constants }
  822. if (target_param.typ=constsym) or (caller_param.typ=constsym) then
  823. exit;
  824. key:=generic_param_hash(target_param.typedef);
  825. { the generic param is already used }
  826. index:=genericparams.findindexof(key);
  827. if index>=0 then
  828. continue;
  829. { add the type to the generic params }
  830. genericparams.add(key,caller_param);
  831. end;
  832. end;
  833. { specialize arrays by using element types but arrays may be multi-dimensional
  834. so we need to examine the caller/target pairs recursively in order to
  835. verify the dimensionality is equal }
  836. function handle_arrays(owner:tprocdef;target_def,caller_def:tarraydef;out target_element,caller_element:tdef):boolean;
  837. begin
  838. { the target and the caller are both arrays and the target is a
  839. specialization so we can recurse into the targets element def }
  840. if is_array_literal(target_def.elementdef) and
  841. is_array_literal(caller_def.elementdef) and
  842. target_def.is_specialization then
  843. result:=handle_arrays(owner,tarraydef(target_def.elementdef),tarraydef(caller_def.elementdef),target_element,caller_element)
  844. else
  845. begin
  846. { the caller is an array which means the dimensionality is unbalanced
  847. and thus the arrays are compatible }
  848. if is_array_literal(caller_def.elementdef) then
  849. exit(false);
  850. { if the element is a generic param then return this type
  851. along with the caller element type at the same level }
  852. result:=owner.is_generic_param(target_def.elementdef);
  853. if result then
  854. begin
  855. target_element:=target_def.elementdef;
  856. caller_element:=caller_def.elementdef;
  857. end;
  858. end;
  859. end;
  860. { handle procvars by using the parameters from the caller to specialize
  861. the parameters of the target generic procedure specialization. for example:
  862. type generic TProc<S> = procedure(value: S);
  863. generic procedure Run<T>(proc: specialize TProc<T>);
  864. procedure DoCallback(value: integer);
  865. Run(@DoCallback);
  866. will specialize as Run<integer> because the signature
  867. of DoCallback() matches TProc<S> so we can specialize "S"
  868. with "integer", as they are both parameter #1
  869. }
  870. function handle_procvars(genericparams:tfphashlist;callerparams:tfplist;target_def:tdef;caller_def:tdef):boolean;
  871. var
  872. newparams : tfphashlist;
  873. procedure handle_generic_param(targetparadef,callerparadef:tdef);
  874. var
  875. key : string;
  876. index : integer;
  877. begin
  878. if not assigned(callerparadef.typesym) then
  879. internalerror(2021020908);
  880. key:=generic_param_hash(targetparadef);
  881. { the generic param must not already be used }
  882. index:=genericparams.findindexof(key);
  883. if index<0 then
  884. begin
  885. { add the type to the list }
  886. index:=newparams.findindexof(key);
  887. if index<0 then
  888. newparams.add(key,callerparadef.typesym);
  889. end;
  890. end;
  891. var
  892. i,j : integer;
  893. paravar : tparavarsym;
  894. target_proc,
  895. caller_proc : tprocvardef;
  896. target_proc_para,
  897. caller_proc_para : tparavarsym;
  898. valid_params : integer;
  899. begin
  900. result := false;
  901. target_proc:=tprocvardef(target_def);
  902. caller_proc:=tprocvardef(caller_def);
  903. { parameter count must match exactly
  904. currently default values are not considered }
  905. if target_proc.paras.count<>caller_proc.paras.count then
  906. exit;
  907. { a mixture of functions and procedures is not allowed }
  908. if (not assigned(target_proc.returndef) or is_void(target_proc.returndef)) xor
  909. (not assigned(caller_proc.returndef) or is_void(caller_proc.returndef)) then
  910. exit;
  911. { reject generics with constants }
  912. for i:=0 to target_proc.genericdef.genericparas.count-1 do
  913. if tsym(target_proc.genericdef.genericparas[i]).typ=constsym then
  914. exit;
  915. newparams:=tfphashlist.create;
  916. valid_params:=0;
  917. for i:=0 to target_proc.paras.count-1 do
  918. begin
  919. target_proc_para:=tparavarsym(target_proc.paras[i]);
  920. caller_proc_para:=tparavarsym(caller_proc.paras[i]);
  921. { the parameters are not compatible }
  922. if compare_defs(caller_proc_para.vardef,target_proc_para.vardef,nothingn)=te_incompatible then
  923. begin
  924. newparams.free;
  925. exit(false);
  926. end;
  927. if sp_generic_para in target_proc_para.vardef.typesym.symoptions then
  928. begin
  929. paravar:=tparavarsym(tprocvardef(target_proc.genericdef).paras[i]);
  930. { find the generic param name in the generic def parameters }
  931. j:=target_proc.genericdef.genericparas.findindexof(paravar.vardef.typesym.name);
  932. handle_generic_param(ttypesym(target_proc.genericparas[j]).typedef,caller_proc_para.vardef);
  933. end;
  934. inc(valid_params);
  935. end;
  936. if assigned(target_proc.returndef) and not is_void(target_proc.returndef) then
  937. begin
  938. { or check for exact? }
  939. if compare_defs(caller_proc.returndef,target_proc.returndef,nothingn)<te_equal then
  940. begin
  941. newparams.free;
  942. exit(false);
  943. end;
  944. if sp_generic_para in target_proc.returndef.typesym.symoptions then
  945. begin
  946. handle_generic_param(target_proc.returndef,caller_proc.returndef);
  947. end;
  948. end;
  949. { if the count of valid params matches the target then
  950. transfer the temporary params to the actual params }
  951. result:=valid_params=target_proc.paras.count;
  952. if result then
  953. for i := 0 to newparams.count-1 do
  954. genericparams.add(newparams.nameofindex(i),newparams[i]);
  955. newparams.free;
  956. end;
  957. function maybe_inherited_specialization(givendef,desireddef:tstoreddef;out basedef:tstoreddef):boolean;
  958. begin
  959. result:=false;
  960. basedef:=nil;
  961. if givendef.typ<>objectdef then
  962. begin
  963. result:=givendef.is_specialization and (givendef.genericdef=desireddef.genericdef);
  964. if result then
  965. basedef:=givendef;
  966. end
  967. else
  968. begin
  969. while assigned(givendef) do
  970. begin
  971. if givendef.is_specialization and (givendef.genericdef=desireddef.genericdef) then
  972. begin
  973. basedef:=givendef;
  974. result:=true;
  975. break;
  976. end;
  977. givendef:=tobjectdef(givendef).childof;
  978. end;
  979. end;
  980. end;
  981. { compare generic parameters <T> with call node parameters. }
  982. function is_possible_specialization(callerparams:tfplist;genericdef:tprocdef;out unnamed_syms:tfplist;out genericparams:tfphashlist):boolean;
  983. var
  984. i,j,
  985. count : integer;
  986. paravar : tparavarsym;
  987. base_def : tstoreddef;
  988. target_def,
  989. caller_def : tdef;
  990. target_key : string;
  991. index : integer;
  992. paras : tfplist;
  993. target_element,
  994. caller_element : tdef;
  995. required_param_count : integer;
  996. adef : tarraydef;
  997. begin
  998. result:=false;
  999. paras:=nil;
  1000. genericparams:=nil;
  1001. required_param_count:=0;
  1002. unnamed_syms:=nil;
  1003. { first perform a check to reject generics with constants }
  1004. for i:=0 to genericdef.genericparas.count-1 do
  1005. if tsym(genericdef.genericparas[i]).typ=constsym then
  1006. exit;
  1007. { build list of visible target function parameters }
  1008. paras:=tfplist.create;
  1009. for i:=0 to genericdef.paras.count-1 do
  1010. begin
  1011. paravar:=tparavarsym(genericdef.paras[i]);
  1012. { ignore hidden parameters }
  1013. if vo_is_hidden_para in paravar.varoptions then
  1014. continue;
  1015. paras.add(paravar);
  1016. { const non-default parameters are required }
  1017. if not assigned(paravar.defaultconstsym) then
  1018. inc(required_param_count);
  1019. end;
  1020. { not enough parameters were supplied }
  1021. if callerparams.count<required_param_count then
  1022. begin
  1023. paras.free;
  1024. exit;
  1025. end;
  1026. { check to make sure the generic parameters are all used
  1027. at least once in the caller parameters. }
  1028. count:=0;
  1029. for i:=0 to genericdef.genericparas.count-1 do
  1030. if is_generic_param_used(genericdef,ttypesym(genericdef.genericparas[i]),paras) then
  1031. inc(count);
  1032. if count<genericdef.genericparas.count then
  1033. begin
  1034. paras.free;
  1035. exit;
  1036. end;
  1037. genericparams:=tfphashlist.create;
  1038. for i:=0 to callerparams.count-1 do
  1039. begin
  1040. caller_def:=ttypesym(callerparams[i]).typedef;
  1041. { caller parameter exceeded the possible parameters }
  1042. if i=paras.count then
  1043. begin
  1044. genericparams.free;
  1045. paras.free;
  1046. exit;
  1047. end;
  1048. target_def:=tparavarsym(paras[i]).vardef;
  1049. target_key:='';
  1050. { strings are compatible with "array of T" so we
  1051. need to use the element type for specialization }
  1052. if is_stringlike(caller_def) and
  1053. is_array_literal(target_def) and
  1054. genericdef.is_generic_param(tarraydef(target_def).elementdef) then
  1055. begin
  1056. target_def:=tarraydef(target_def).elementdef;
  1057. target_key:=generic_param_hash(target_def);
  1058. caller_def:=tstringdef(caller_def).get_default_char_type;
  1059. end
  1060. { non-uniform array constructors (i.e. array of const) are not compatible
  1061. with normal arrays like "array of T" so we reject them }
  1062. else if is_array_literal(target_def) and
  1063. (caller_def.typ=arraydef) and
  1064. (ado_IsConstructor in tarraydef(caller_def).arrayoptions) and
  1065. (ado_IsArrayOfConst in tarraydef(caller_def).arrayoptions) then
  1066. begin
  1067. continue;
  1068. end
  1069. { handle generic arrays }
  1070. else if is_array_literal(caller_def) and
  1071. is_array_literal(target_def) and
  1072. handle_arrays(genericdef,tarraydef(target_def),tarraydef(caller_def),target_element,caller_element) then
  1073. begin
  1074. target_def:=target_element;
  1075. caller_def:=caller_element;
  1076. target_key:=generic_param_hash(target_def);
  1077. end
  1078. { handle generic procvars }
  1079. else if (caller_def.typ=procvardef) and
  1080. (target_def.typ=procvardef) and
  1081. tprocvardef(target_def).is_specialization and
  1082. handle_procvars(genericparams,callerparams,target_def,caller_def) then
  1083. begin
  1084. continue;
  1085. end
  1086. { handle specialized objects by taking the base class as the type to specialize }
  1087. else if is_class_or_object(caller_def) and
  1088. is_class_or_object(target_def) and
  1089. genericdef.is_generic_param(target_def) then
  1090. begin
  1091. target_key:=generic_param_hash(target_def);
  1092. target_def:=tobjectdef(target_def).childof;
  1093. end
  1094. { handle generic specializations }
  1095. else if tstoreddef(target_def).is_specialization and
  1096. maybe_inherited_specialization(tstoreddef(caller_def),tstoreddef(target_def),base_def) then
  1097. begin
  1098. handle_specializations(genericparams,tstoreddef(target_def),base_def);
  1099. continue;
  1100. end
  1101. { handle all other generic params }
  1102. else if target_def.typ=undefineddef then
  1103. target_key:=generic_param_hash(target_def);
  1104. { the param doesn't have a generic key which means we don't need to consider it }
  1105. if target_key='' then
  1106. continue;
  1107. { the generic param is already used }
  1108. index:=genericparams.findindexof(target_key);
  1109. if index>=0 then
  1110. continue;
  1111. { the caller type may not have a typesym so we need to create an unnamed one }
  1112. if not assigned(caller_def.typesym) then
  1113. begin
  1114. sym:=create_unnamed_typesym(caller_def);
  1115. { add the unnamed sym to the list but only it was allocated manually }
  1116. if sym.owner=caller_def.owner then
  1117. begin
  1118. if not assigned(unnamed_syms) then
  1119. unnamed_syms:=tfplist.create;
  1120. unnamed_syms.add(sym);
  1121. end;
  1122. genericparams.add(target_key,sym);
  1123. end
  1124. else
  1125. genericparams.add(target_key,caller_def.typesym);
  1126. end;
  1127. { if the parameter counts match then the specialization is possible }
  1128. result:=genericparams.count=genericdef.genericparas.count;
  1129. { cleanup }
  1130. paras.free;
  1131. if not result then
  1132. genericparams.free;
  1133. end;
  1134. { make an ordered list of parameters from the caller }
  1135. function make_param_list(dummysym:tsym;para:tnode;var unnamed_syms:tfplist):tfplist;
  1136. var
  1137. pt : tcallparanode;
  1138. paradef : tdef;
  1139. sym : tsym;
  1140. i : integer;
  1141. begin
  1142. result:=tfplist.create;
  1143. pt:=tcallparanode(para);
  1144. while assigned(pt) do
  1145. begin
  1146. paradef:=pt.paravalue.resultdef;
  1147. { unnamed parameter types can not be specialized }
  1148. if not assigned(paradef.typesym) then
  1149. begin
  1150. sym:=create_unnamed_typesym(paradef);
  1151. result.insert(0,sym);
  1152. { add the unnamed sym to the list but only if it was allocated manually }
  1153. if sym.owner=paradef.owner then
  1154. begin
  1155. if not assigned(unnamed_syms) then
  1156. unnamed_syms:=tfplist.create;
  1157. unnamed_syms.add(sym);
  1158. end;
  1159. end
  1160. else
  1161. result.insert(0,paradef.typesym);
  1162. pt:=tcallparanode(pt.nextpara);
  1163. end;
  1164. end;
  1165. var
  1166. i,j,k : integer;
  1167. srsym : tprocsym;
  1168. callerparams : tfplist;
  1169. pd : tprocdef;
  1170. dummysym : tprocsym;
  1171. genericparams : tfphashlist;
  1172. spezcontext : tspecializationcontext;
  1173. pd_unnamed_syms : tfplist;
  1174. begin
  1175. result:=false;
  1176. spezcontext:=nil;
  1177. genericparams:=nil;
  1178. dummysym:=tprocsym(sym);
  1179. callerparams:=make_param_list(dummysym,para,unnamed_syms);
  1180. { failed to build the parameter list }
  1181. if not assigned(callerparams) then
  1182. exit;
  1183. for i:=0 to dummysym.genprocsymovlds.count-1 do
  1184. begin
  1185. srsym:=tprocsym(dummysym.genprocsymovlds[i]);
  1186. for j:=0 to srsym.ProcdefList.Count-1 do
  1187. begin
  1188. pd:=tprocdef(srsym.ProcdefList[j]);
  1189. if is_possible_specialization(callerparams,pd,pd_unnamed_syms,genericparams) then
  1190. begin
  1191. generate_implicit_specialization(spezcontext,pd,genericparams);
  1192. genericparams.free;
  1193. { finalize the specialization so it can be added to the list of overloads }
  1194. if not finalize_specialization(pd,spezcontext) then
  1195. begin
  1196. spezcontext.free;
  1197. continue;
  1198. end;
  1199. { handle unnamed syms used by the specialization }
  1200. if pd_unnamed_syms<>nil then
  1201. begin
  1202. transfer_unnamed_symbols(pd.owner,pd_unnamed_syms);
  1203. pd_unnamed_syms.free;
  1204. end;
  1205. pdoverloadlist.add(pd);
  1206. spezcontext.free;
  1207. if po_overload in pd.procoptions then
  1208. hasoverload:=true;
  1209. { store first procsym found }
  1210. if not assigned(first_procsym) then
  1211. first_procsym:=srsym;
  1212. result:=true;
  1213. end
  1214. else
  1215. begin
  1216. { the specialization was not chosen so clean up any unnamed syms }
  1217. if pd_unnamed_syms<>nil then
  1218. begin
  1219. for k:=0 to pd_unnamed_syms.count-1 do
  1220. tsym(pd_unnamed_syms[k]).free;
  1221. pd_unnamed_syms.free;
  1222. end;
  1223. end;
  1224. end;
  1225. end;
  1226. callerparams.free;
  1227. end;
  1228. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;
  1229. var
  1230. dummypos : tfileposinfo;
  1231. {$push}
  1232. {$warn 5036 off}
  1233. begin
  1234. result:=generate_specialization_phase1(context,genericdef,nil,'',nil,dummypos);
  1235. end;
  1236. {$pop}
  1237. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;const symname:string;symtable:tsymtable):tdef;
  1238. var
  1239. dummypos : tfileposinfo;
  1240. {$push}
  1241. {$warn 5036 off}
  1242. begin
  1243. result:=generate_specialization_phase1(context,genericdef,nil,symname,symtable,dummypos);
  1244. end;
  1245. {$pop}
  1246. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;const symname:string;symtable:tsymtable;parsedpos:tfileposinfo):tdef;
  1247. var
  1248. found,
  1249. err : boolean;
  1250. i,
  1251. gencount : longint;
  1252. countstr,genname,ugenname : string;
  1253. tmpstack : tfpobjectlist;
  1254. symowner : tsymtable;
  1255. begin
  1256. context:=nil;
  1257. result:=nil;
  1258. { either symname must be given or genericdef needs to be valid }
  1259. if (symname='') and
  1260. (not assigned(genericdef) or
  1261. (
  1262. (genericdef.typ<>procdef) and
  1263. (
  1264. not assigned(genericdef.typesym) or
  1265. (genericdef.typesym.typ<>typesym)
  1266. ) and
  1267. (
  1268. (genericdef.typ<>objectdef) or
  1269. not (oo_is_forward in tobjectdef(genericdef).objectoptions)
  1270. )
  1271. ) or
  1272. (
  1273. (genericdef.typ=procdef) and
  1274. (
  1275. not assigned(tprocdef(genericdef).procsym) or
  1276. (tprocdef(genericdef).procsym.typ<>procsym)
  1277. )
  1278. )
  1279. ) then
  1280. begin
  1281. internalerror(2019112401);
  1282. end;
  1283. if not assigned(parsedtype) and not try_to_consume(_LT) then
  1284. begin
  1285. consume(_LSHARPBRACKET);
  1286. { handle "<>" }
  1287. if (token=_GT) or (token=_RSHARPBRACKET) then
  1288. begin
  1289. Message(type_e_type_id_expected);
  1290. if not try_to_consume(_GT) then
  1291. try_to_consume(_RSHARPBRACKET);
  1292. result:=generrordef;
  1293. exit;
  1294. end;
  1295. end;
  1296. context:=tspecializationcontext.create;
  1297. { Parse type parameters }
  1298. err:=not parse_generic_specialization_types_internal(context.paramlist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  1299. if err then
  1300. begin
  1301. if not try_to_consume(_GT) then
  1302. try_to_consume(_RSHARPBRACKET);
  1303. context.free;
  1304. context:=nil;
  1305. result:=generrordef;
  1306. exit;
  1307. end;
  1308. { use the name of the symbol as procvars return a user friendly version
  1309. of the name }
  1310. if symname='' then
  1311. begin
  1312. if genericdef.typ=procdef then
  1313. genname:=tprocdef(genericdef).procsym.realname
  1314. else if assigned(genericdef.typesym) then
  1315. genname:=ttypesym(genericdef.typesym).realname
  1316. else if (genericdef.typ=objectdef) and (oo_is_forward in tobjectdef(genericdef).objectoptions) then
  1317. genname:=tobjectdef(genericdef).objrealname^
  1318. else
  1319. internalerror(2020071201);
  1320. end
  1321. else
  1322. genname:=symname;
  1323. { in case of non-Delphi mode the type name could already be a generic
  1324. def (but maybe the wrong one) }
  1325. if assigned(genericdef) and
  1326. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  1327. begin
  1328. { remove the type count suffix from the generic's name }
  1329. for i:=Length(genname) downto 1 do
  1330. if genname[i]='$' then
  1331. begin
  1332. genname:=copy(genname,1,i-1);
  1333. break;
  1334. end;
  1335. { in case of a specialization we've only reached the specialization
  1336. checksum yet }
  1337. if df_specialization in genericdef.defoptions then
  1338. for i:=length(genname) downto 1 do
  1339. if genname[i]='$' then
  1340. begin
  1341. genname:=copy(genname,1,i-1);
  1342. break;
  1343. end;
  1344. end
  1345. else
  1346. begin
  1347. split_generic_name(genname,ugenname,gencount);
  1348. if genname<>ugenname then
  1349. genname:=ugenname;
  1350. end;
  1351. { search a generic with the given count of params }
  1352. countstr:='';
  1353. str(context.paramlist.Count,countstr);
  1354. genname:=genname+'$'+countstr;
  1355. ugenname:=upper(genname);
  1356. context.genname:=genname;
  1357. if assigned(genericdef) then
  1358. symowner:=genericdef.owner
  1359. else
  1360. symowner:=symtable;
  1361. if assigned(symowner) and (symowner.symtabletype in [objectsymtable,recordsymtable]) then
  1362. begin
  1363. if symowner.symtabletype = objectsymtable then
  1364. found:=searchsym_in_class(tobjectdef(symowner.defowner),tobjectdef(symowner.defowner),ugenname,context.sym,context.symtable,[])
  1365. else
  1366. found:=searchsym_in_record(tabstractrecorddef(symowner.defowner),ugenname,context.sym,context.symtable);
  1367. if not found then
  1368. found:=searchsym(ugenname,context.sym,context.symtable);
  1369. end
  1370. else
  1371. found:=searchsym(ugenname,context.sym,context.symtable);
  1372. if found and (context.sym.typ=absolutevarsym) and
  1373. (vo_is_funcret in tabstractvarsym(context.sym).varoptions) then
  1374. begin
  1375. { we found the function result alias of a generic function; go up the
  1376. symbol stack *before* this alias was inserted, so that we can
  1377. (hopefully) find the correct generic symbol }
  1378. tmpstack:=tfpobjectlist.create(false);
  1379. while assigned(symtablestack.top) do
  1380. begin
  1381. tmpstack.Add(symtablestack.top);
  1382. symtablestack.pop(symtablestack.top);
  1383. if tmpstack.Last=context.symtable then
  1384. break;
  1385. end;
  1386. if not assigned(symtablestack.top) then
  1387. internalerror(2019123001);
  1388. found:=searchsym(ugenname,context.sym,context.symtable);
  1389. for i:=tmpstack.count-1 downto 0 do
  1390. symtablestack.push(tsymtable(tmpstack[i]));
  1391. tmpstack.free;
  1392. end;
  1393. if not found or not (context.sym.typ in [typesym,procsym]) then
  1394. begin
  1395. identifier_not_found(genname);
  1396. if not try_to_consume(_GT) then
  1397. try_to_consume(_RSHARPBRACKET);
  1398. context.free;
  1399. context:=nil;
  1400. result:=generrordef;
  1401. exit;
  1402. end;
  1403. { we've found the correct def }
  1404. if context.sym.typ=typesym then
  1405. result:=tstoreddef(ttypesym(context.sym).typedef)
  1406. else
  1407. begin
  1408. if tprocsym(context.sym).procdeflist.count=0 then
  1409. internalerror(2015061203);
  1410. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  1411. end;
  1412. if not try_to_consume(_GT) then
  1413. consume(_RSHARPBRACKET);
  1414. end;
  1415. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;const _prettyname:ansistring):tdef;
  1416. procedure unset_forwarddef(def: tdef);
  1417. var
  1418. st : TSymtable;
  1419. i : longint;
  1420. begin
  1421. case def.typ of
  1422. procdef:
  1423. tprocdef(def).forwarddef:=false;
  1424. objectdef,
  1425. recorddef:
  1426. begin
  1427. st:=def.getsymtable(gs_record);
  1428. for i:=0 to st.deflist.count-1 do
  1429. unset_forwarddef(tdef(st.deflist[i]));
  1430. end;
  1431. else
  1432. ;
  1433. end;
  1434. end;
  1435. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  1436. var
  1437. i : longint;
  1438. begin
  1439. gendef:=nil;
  1440. psym:=nil;
  1441. case sym.typ of
  1442. typesym:
  1443. begin
  1444. gendef:=ttypesym(sym).typedef
  1445. end;
  1446. procsym:
  1447. begin
  1448. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  1449. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  1450. begin
  1451. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  1452. break;
  1453. end;
  1454. psym:=sym;
  1455. end
  1456. else
  1457. internalerror(200710171);
  1458. end;
  1459. end;
  1460. var
  1461. finalspecializename,
  1462. ufinalspecializename : tidstring;
  1463. prettyname : ansistring;
  1464. generictypelist : tfphashobjectlist;
  1465. srsymtable,
  1466. specializest : tsymtable;
  1467. hashedid : thashedidstring;
  1468. tempst : tglobalsymtable;
  1469. tsrsym : ttypesym;
  1470. psym,
  1471. srsym : tsym;
  1472. paramdef1,
  1473. paramdef2,
  1474. def : tdef;
  1475. old_block_type : tblock_type;
  1476. state : tspecializationstate;
  1477. old_current_structdef : tabstractrecorddef;
  1478. old_current_specializedef,
  1479. old_current_genericdef : tstoreddef;
  1480. old_current_procinfo : tprocinfo;
  1481. old_module_procinfo : tobject;
  1482. hmodule : tmodule;
  1483. oldcurrent_filepos : tfileposinfo;
  1484. recordbuf : tdynamicarray;
  1485. hadtypetoken : boolean;
  1486. i,
  1487. replaydepth : longint;
  1488. item : tobject;
  1489. allequal,
  1490. hintsprocessed : boolean;
  1491. pd : tprocdef;
  1492. pdflags : tpdflags;
  1493. ppflags : tparse_proc_flags;
  1494. begin
  1495. if not assigned(context) then
  1496. internalerror(2015052203);
  1497. result:=nil;
  1498. pd:=nil;
  1499. if not check_generic_constraints(genericdef,context.paramlist,context.poslist) then
  1500. begin
  1501. { the parameters didn't fit the constraints, so don't continue with the
  1502. specialization }
  1503. result:=generrordef;
  1504. exit;
  1505. end;
  1506. { build the new type's name }
  1507. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  1508. ufinalspecializename:=upper(finalspecializename);
  1509. if genericdef.typ=procdef then
  1510. prettyname:=tprocdef(genericdef).procsym.prettyname
  1511. else
  1512. prettyname:=genericdef.typesym.prettyname;
  1513. prettyname:=prettyname+'<'+context.prettyname+'>';
  1514. generictypelist:=tfphashobjectlist.create(false);
  1515. { build the list containing the types for the generic params }
  1516. if not assigned(genericdef.genericparas) then
  1517. internalerror(2013092601);
  1518. if context.paramlist.count<>genericdef.genericparas.count then
  1519. internalerror(2013092603);
  1520. for i:=0 to genericdef.genericparas.Count-1 do
  1521. begin
  1522. srsym:=tsym(genericdef.genericparas[i]);
  1523. if not (sp_generic_para in srsym.symoptions) then
  1524. internalerror(2013092602);
  1525. generictypelist.add(srsym.realname,context.paramlist[i]);
  1526. end;
  1527. { Special case if we are referencing the current defined object }
  1528. if assigned(current_structdef) and
  1529. (current_structdef.objname^=ufinalspecializename) then
  1530. result:=current_structdef;
  1531. { Can we reuse an already specialized type? }
  1532. { for this first check whether we are currently specializing a nested
  1533. type of the current (main) specialization (this is necessary, because
  1534. during that time the symbol of the main specialization will still
  1535. contain a reference to an errordef) }
  1536. if not assigned(result) and assigned(current_specializedef) then
  1537. begin
  1538. def:=current_specializedef;
  1539. repeat
  1540. if def.typ in [objectdef,recorddef] then
  1541. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  1542. result:=def;
  1543. break;
  1544. end;
  1545. if assigned(def.owner) then
  1546. def:=tstoreddef(def.owner.defowner)
  1547. else
  1548. { this can happen when specializing a generic function }
  1549. def:=nil;
  1550. until not assigned(def) or not (df_specialization in def.defoptions);
  1551. end;
  1552. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  1553. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  1554. parsing anyway), so for now we treat those still as generic defs without doing a partial
  1555. specialization }
  1556. if not assigned(result) then
  1557. begin
  1558. def:=current_genericdef;
  1559. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  1560. begin
  1561. if (df_generic in def.defoptions) and (def=genericdef) then
  1562. begin
  1563. result:=def;
  1564. break;
  1565. end;
  1566. { the following happens when a routine with its parent struct
  1567. as parameter is specialized as a parameter or result of a
  1568. generic function }
  1569. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  1570. begin
  1571. if tstoreddef(def).genericparas.count=generictypelist.count then
  1572. begin
  1573. allequal:=true;
  1574. for i:=0 to generictypelist.count-1 do
  1575. begin
  1576. if tsym(generictypelist[i]).typ<>tsym(tstoreddef(def).genericparas[i]).typ then
  1577. begin
  1578. allequal:=false;
  1579. break;
  1580. end;
  1581. if tsym(generictypelist[i]).typ=constsym then
  1582. paramdef1:=tconstsym(generictypelist[i]).constdef
  1583. else
  1584. paramdef1:=ttypesym(generictypelist[i]).typedef;
  1585. if tsym(tstoreddef(def).genericparas[i]).typ=constsym then
  1586. paramdef2:=tconstsym(tstoreddef(def).genericparas[i]).constdef
  1587. else
  1588. paramdef2:=ttypesym(tstoreddef(def).genericparas[i]).typedef;
  1589. if not equal_defs(paramdef1,paramdef2) then
  1590. begin
  1591. allequal:=false;
  1592. break;
  1593. end;
  1594. if (tsym(generictypelist[i]).typ=constsym) and
  1595. (
  1596. (tconstsym(generictypelist[i]).consttyp<>tconstsym(tstoreddef(def).genericparas[i]).consttyp) or
  1597. not same_constvalue(tconstsym(generictypelist[i]).consttyp,tconstsym(generictypelist[i]).value,tconstsym(tstoreddef(def).genericparas[i]).value)
  1598. ) then
  1599. begin
  1600. allequal:=false;
  1601. break;
  1602. end;
  1603. end;
  1604. if allequal then
  1605. begin
  1606. result:=def;
  1607. break;
  1608. end;
  1609. end;
  1610. end;
  1611. def:=tstoreddef(def.owner.defowner);
  1612. end;
  1613. end;
  1614. { decide in which symtable to put the specialization }
  1615. if assigned(context.forwarddef) then
  1616. begin
  1617. specializest:=context.forwarddef.owner;
  1618. end
  1619. else if parse_generic and not assigned(result) then
  1620. begin
  1621. srsymtable:=symtablestack.top;
  1622. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  1623. { if we are currently specializing a routine we need to specialize into
  1624. the routine's local- or parasymtable so that they are correctly
  1625. registered should the specialization be finalized }
  1626. specializest:=srsymtable
  1627. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  1628. { if we are parsing the definition of a method we specialize into
  1629. the local symtable of it }
  1630. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  1631. else
  1632. begin
  1633. if not assigned(current_genericdef) then
  1634. internalerror(2014050901);
  1635. { we specialize the partial specialization into the symtable of the currently parsed
  1636. generic }
  1637. case current_genericdef.typ of
  1638. procvardef:
  1639. specializest:=current_genericdef.getsymtable(gs_para);
  1640. procdef:
  1641. specializest:=current_genericdef.getsymtable(gs_local);
  1642. objectdef,
  1643. recorddef:
  1644. specializest:=current_genericdef.getsymtable(gs_record);
  1645. arraydef:
  1646. specializest:=tarraydef(current_genericdef).symtable;
  1647. else
  1648. internalerror(2014050902);
  1649. end;
  1650. end;
  1651. end
  1652. else
  1653. if current_module.is_unit and current_module.in_interface then
  1654. specializest:=current_module.globalsymtable
  1655. else
  1656. specializest:=current_module.localsymtable;
  1657. if not assigned(specializest) then
  1658. internalerror(2014050910);
  1659. { now check whether there is a specialization somewhere else }
  1660. psym:=nil;
  1661. if not assigned(result) then
  1662. begin
  1663. hashedid.id:=ufinalspecializename;
  1664. if (specializest.symtabletype=objectsymtable) and not assigned(context.forwarddef) then
  1665. begin
  1666. { search also in parent classes }
  1667. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  1668. internalerror(2016112901);
  1669. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  1670. srsym:=nil;
  1671. end
  1672. else
  1673. srsym:=tsym(specializest.findwithhash(hashedid));
  1674. if assigned(context.forwarddef) then
  1675. begin
  1676. { just do a few sanity checks }
  1677. if not assigned(srsym) or not (srsym.typ=typesym) then
  1678. internalerror(2020070306);
  1679. if ttypesym(srsym).typedef<>context.forwarddef then
  1680. internalerror(2020070307);
  1681. end
  1682. else if assigned(srsym) then
  1683. begin
  1684. retrieve_genericdef_or_procsym(srsym,result,psym);
  1685. end
  1686. else
  1687. { the generic could have been specialized in the globalsymtable
  1688. already, so search there as well }
  1689. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  1690. begin
  1691. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  1692. if assigned(srsym) then
  1693. begin
  1694. retrieve_genericdef_or_procsym(srsym,result,psym);
  1695. end;
  1696. end;
  1697. end;
  1698. if not assigned(result) then
  1699. begin
  1700. specialization_init(genericdef,state);
  1701. { push a temporary global symtable so that the specialization is
  1702. added to the correct symtable; this symtable does not contain
  1703. any other symbols, so that the type resolution can not be
  1704. influenced by symbols in the current unit }
  1705. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  1706. symtablestack.push(tempst);
  1707. { Reparse the original type definition }
  1708. begin
  1709. old_current_specializedef:=nil;
  1710. old_current_genericdef:=nil;
  1711. old_current_structdef:=nil;
  1712. old_current_procinfo:=current_procinfo;
  1713. old_module_procinfo:=current_module.procinfo;
  1714. current_procinfo:=nil;
  1715. current_module.procinfo:=nil;
  1716. if parse_class_parent then
  1717. begin
  1718. old_current_structdef:=current_structdef;
  1719. old_current_genericdef:=current_genericdef;
  1720. old_current_specializedef:=current_specializedef;
  1721. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  1722. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  1723. else
  1724. current_structdef:=nil;
  1725. current_genericdef:=nil;
  1726. current_specializedef:=nil;
  1727. end;
  1728. maybe_add_waiting_unit(genericdef);
  1729. { First a new sym so we can reuse this specialization and
  1730. references to this specialization can be handled }
  1731. if genericdef.typ=procdef then
  1732. if assigned(psym) then
  1733. srsym:=psym
  1734. else
  1735. srsym:=cprocsym.create(finalspecializename)
  1736. else
  1737. srsym:=ctypesym.create(finalspecializename,generrordef);
  1738. { insert the symbol only if we don't know already that we have
  1739. a procsym to add it to and we aren't dealing with a forwarddef }
  1740. if not assigned(psym) and not assigned(context.forwarddef) then
  1741. specializest.insertsym(srsym);
  1742. { specializations are declarations as such it is the wisest to
  1743. declare set the blocktype to "type"; otherwise we'll
  1744. experience unexpected side effects like the addition of
  1745. classrefdefs if we have a generic that's derived from another
  1746. generic }
  1747. old_block_type:=block_type;
  1748. block_type:=bt_type;
  1749. if (
  1750. (genericdef.typ=procdef) and
  1751. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  1752. ) or (
  1753. (genericdef.typ<>procdef) and
  1754. not assigned(genericdef.generictokenbuf)
  1755. ) then
  1756. internalerror(200511171);
  1757. hmodule:=find_module_from_symtable(genericdef.owner);
  1758. if hmodule=nil then
  1759. internalerror(2012051202);
  1760. oldcurrent_filepos:=current_filepos;
  1761. { use the index the module got from the current compilation process }
  1762. current_filepos.moduleindex:=hmodule.unit_index;
  1763. current_tokenpos:=current_filepos;
  1764. if parse_generic then
  1765. begin
  1766. recordbuf:=current_scanner.recordtokenbuf;
  1767. current_scanner.recordtokenbuf:=nil;
  1768. end
  1769. else
  1770. recordbuf:=nil;
  1771. replaydepth:=current_scanner.replay_stack_depth;
  1772. if genericdef.typ=procdef then
  1773. begin
  1774. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf,hmodule.change_endian);
  1775. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,[],genericdef,generictypelist,pd);
  1776. if assigned(pd) then
  1777. begin
  1778. if assigned(psym) then
  1779. pd.procsym:=psym
  1780. else
  1781. pd.procsym:=srsym;
  1782. ppflags:=[];
  1783. if po_classmethod in tprocdef(genericdef).procoptions then
  1784. include(ppflags,ppf_classmethod);
  1785. parse_proc_dec_finish(pd,ppflags,tprocdef(genericdef).struct);
  1786. end;
  1787. result:=pd;
  1788. end
  1789. else
  1790. begin
  1791. current_scanner.startreplaytokens(genericdef.generictokenbuf,hmodule.change_endian);
  1792. if assigned(context.forwarddef) then
  1793. begin
  1794. tsrsym:=nil;
  1795. result:=parse_forward_declaration(context.forwarddef.typesym,ufinalspecializename,finalspecializename,genericdef,generictypelist,tsrsym);
  1796. srsym:=tsrsym;
  1797. end
  1798. else
  1799. begin
  1800. hadtypetoken:=false;
  1801. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  1802. ttypesym(srsym).typedef:=result;
  1803. result.typesym:=srsym;
  1804. end;
  1805. if _prettyname<>'' then
  1806. ttypesym(result.typesym).fprettyname:=_prettyname
  1807. else
  1808. ttypesym(result.typesym).fprettyname:=prettyname;
  1809. end;
  1810. current_filepos:=oldcurrent_filepos;
  1811. { Note regarding hint directives:
  1812. There is no need to remove the flags for them from the
  1813. specialized generic symbol, because hint directives that
  1814. follow the specialization are handled by the code in
  1815. pdecl.types_dec and added to the type symbol.
  1816. E.g.: TFoo = TBar<Blubb> deprecated;
  1817. Here the symbol TBar$1$Blubb will contain the
  1818. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1819. case result.typ of
  1820. { Build VMT indexes for classes and read hint directives }
  1821. objectdef:
  1822. begin
  1823. if replaydepth>current_scanner.replay_stack_depth then
  1824. begin
  1825. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1826. if replaydepth>current_scanner.replay_stack_depth then
  1827. consume(_SEMICOLON);
  1828. end;
  1829. if oo_is_forward in tobjectdef(result).objectoptions then
  1830. add_forward_generic_def(result,context)
  1831. else
  1832. build_vmt(tobjectdef(result));
  1833. end;
  1834. { handle params, calling convention, etc }
  1835. procvardef:
  1836. begin
  1837. hintsprocessed:=false;
  1838. if replaydepth>current_scanner.replay_stack_depth then
  1839. begin
  1840. if not check_proc_directive(true) then
  1841. begin
  1842. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1843. if replaydepth>current_scanner.replay_stack_depth then
  1844. consume(_SEMICOLON);
  1845. end
  1846. else
  1847. hintsprocessed:=true;
  1848. end;
  1849. if replaydepth>current_scanner.replay_stack_depth then
  1850. parse_proctype_directives(tprocvardef(result));
  1851. handle_calling_convention(tprocvardef(result),hcc_default_actions_intf);
  1852. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  1853. begin
  1854. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1855. if replaydepth>current_scanner.replay_stack_depth then
  1856. consume(_SEMICOLON);
  1857. end;
  1858. end;
  1859. procdef:
  1860. begin
  1861. pdflags:=[];
  1862. if genericdef.owner.symtabletype=objectsymtable then
  1863. include(pdflags,pd_object)
  1864. else if genericdef.owner.symtabletype=recordsymtable then
  1865. include(pdflags,pd_record);
  1866. parse_proc_directives(pd,pdflags);
  1867. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1868. consume(_SEMICOLON);
  1869. if parse_generic then
  1870. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1871. else
  1872. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1873. proc_add_definition(tprocdef(result));
  1874. { for partial specializations we implicitely declare the routine as
  1875. having its implementation although we'll not specialize it in reality }
  1876. if parse_generic then
  1877. unset_forwarddef(result);
  1878. end;
  1879. else
  1880. { parse hint directives for records and arrays }
  1881. if replaydepth>current_scanner.replay_stack_depth then begin
  1882. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1883. if replaydepth>current_scanner.replay_stack_depth then
  1884. consume(_SEMICOLON);
  1885. end;
  1886. end;
  1887. { Consume the remainder of the buffer }
  1888. while current_scanner.replay_stack_depth>replaydepth do
  1889. consume(token);
  1890. if assigned(recordbuf) then
  1891. begin
  1892. if assigned(current_scanner.recordtokenbuf) then
  1893. internalerror(2014050909);
  1894. current_scanner.recordtokenbuf:=recordbuf;
  1895. end;
  1896. block_type:=old_block_type;
  1897. current_procinfo:=old_current_procinfo;
  1898. current_module.procinfo:=old_module_procinfo;
  1899. if parse_class_parent then
  1900. begin
  1901. current_structdef:=old_current_structdef;
  1902. current_genericdef:=old_current_genericdef;
  1903. current_specializedef:=old_current_specializedef;
  1904. end;
  1905. end;
  1906. { extract all created symbols and defs from the temporary symtable
  1907. and add them to the specializest }
  1908. for i:=tempst.SymList.Count-1 downto 0 do
  1909. begin
  1910. item:=tempst.SymList.Items[i];
  1911. { using changeowner the symbol is automatically added to the
  1912. new symtable }
  1913. tsym(item).ChangeOwner(specializest);
  1914. end;
  1915. for i:=tempst.DefList.Count-1 downto 0 do
  1916. begin
  1917. item:=tempst.DefList.Items[i];
  1918. { using changeowner the def is automatically added to the new
  1919. symtable }
  1920. tdef(item).ChangeOwner(specializest);
  1921. { for partial specializations we implicitely declare any methods as having their
  1922. implementations although we'll not specialize them in reality }
  1923. if parse_generic then
  1924. unset_forwarddef(tdef(item));
  1925. end;
  1926. { if a generic was declared during the specialization we need to
  1927. flag the specialize symtable accordingly }
  1928. if sto_has_generic in tempst.tableoptions then
  1929. specializest.includeoption(sto_has_generic);
  1930. tempst.free;
  1931. specialization_done(state);
  1932. { procdefs are only added once we know which overload we use }
  1933. if not parse_generic and (result.typ<>procdef) then
  1934. current_module.pendingspecializations.add(result.typename,result);
  1935. end;
  1936. generictypelist.free;
  1937. if assigned(genericdef) then
  1938. begin
  1939. { check the hints of the found generic symbol }
  1940. if genericdef.typ=procdef then
  1941. srsym:=tprocdef(genericdef).procsym
  1942. else
  1943. srsym:=genericdef.typesym;
  1944. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1945. end;
  1946. end;
  1947. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;const _prettyname:string;parsedtype:tdef;const symname:string;parsedpos:tfileposinfo);
  1948. var
  1949. context : tspecializationcontext;
  1950. genericdef : tstoreddef;
  1951. begin
  1952. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,nil,parsedpos));
  1953. if genericdef<>generrordef then
  1954. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1955. tt:=genericdef;
  1956. if assigned(context) then
  1957. context.free;
  1958. end;
  1959. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1960. var
  1961. generictype : tstoredsym;
  1962. i,firstidx,const_list_index : longint;
  1963. srsymtable : tsymtable;
  1964. basedef,def : tdef;
  1965. defname : tidstring;
  1966. allowconst,
  1967. allowconstructor,
  1968. is_const,
  1969. doconsume : boolean;
  1970. constraintdata : tgenericconstraintdata;
  1971. old_block_type : tblock_type;
  1972. fileinfo : tfileposinfo;
  1973. begin
  1974. result:=tfphashobjectlist.create(false);
  1975. firstidx:=0;
  1976. const_list_index:=0;
  1977. old_block_type:=block_type;
  1978. block_type:=bt_type;
  1979. allowconst:=true;
  1980. is_const:=false;
  1981. repeat
  1982. if allowconst and try_to_consume(_CONST) then
  1983. begin
  1984. allowconst:=false;
  1985. is_const:=true;
  1986. const_list_index:=result.count;
  1987. end;
  1988. if token=_ID then
  1989. begin
  1990. if is_const then
  1991. generictype:=cconstsym.create_undefined(orgpattern,cundefinedtype)
  1992. else
  1993. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  1994. { type parameters need to be added as strict private }
  1995. generictype.visibility:=vis_strictprivate;
  1996. include(generictype.symoptions,sp_generic_para);
  1997. result.add(orgpattern,generictype);
  1998. end;
  1999. consume(_ID);
  2000. fileinfo:=current_tokenpos;
  2001. { const restriction }
  2002. if is_const and try_to_consume(_COLON) then
  2003. begin
  2004. def:=nil;
  2005. { parse the type and assign the const type to generictype }
  2006. single_type(def,[]);
  2007. for i:=const_list_index to result.count-1 do
  2008. begin
  2009. { finalize constant information once type is known }
  2010. if assigned(def) and (def.typ in tgeneric_param_const_types) then
  2011. begin
  2012. case def.typ of
  2013. orddef,
  2014. enumdef:
  2015. tconstsym(result[i]).consttyp:=constord;
  2016. stringdef:
  2017. tconstsym(result[i]).consttyp:=conststring;
  2018. floatdef:
  2019. tconstsym(result[i]).consttyp:=constreal;
  2020. setdef:
  2021. tconstsym(result[i]).consttyp:=constset;
  2022. { pointer always refers to nil with constants }
  2023. pointerdef:
  2024. tconstsym(result[i]).consttyp:=constnil;
  2025. else
  2026. internalerror(2020011402);
  2027. end;
  2028. tconstsym(result[i]).constdef:=def;
  2029. end
  2030. else
  2031. Message1(type_e_generic_const_type_not_allowed,def.fulltypename);
  2032. end;
  2033. { after type restriction const list terminates }
  2034. is_const:=false;
  2035. end
  2036. { type restriction }
  2037. else if try_to_consume(_COLON) then
  2038. begin
  2039. if not allowconstraints then
  2040. Message(parser_e_generic_constraints_not_allowed_here);
  2041. { construct a name which can be used for a type specification }
  2042. constraintdata:=tgenericconstraintdata.create;
  2043. constraintdata.fileinfo:=fileinfo;
  2044. defname:='';
  2045. str(current_module.deflist.count,defname);
  2046. defname:='$gendef'+defname;
  2047. allowconstructor:=m_delphi in current_settings.modeswitches;
  2048. basedef:=generrordef;
  2049. repeat
  2050. doconsume:=true;
  2051. case token of
  2052. _CONSTRUCTOR:
  2053. begin
  2054. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  2055. Message(parser_e_illegal_expression);
  2056. include(constraintdata.flags,gcf_constructor);
  2057. allowconstructor:=false;
  2058. end;
  2059. _CLASS:
  2060. begin
  2061. if gcf_class in constraintdata.flags then
  2062. Message(parser_e_illegal_expression);
  2063. if basedef=generrordef then
  2064. include(constraintdata.flags,gcf_class)
  2065. else
  2066. Message(parser_e_illegal_expression);
  2067. end;
  2068. _RECORD:
  2069. begin
  2070. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  2071. or (constraintdata.interfaces.count>0) then
  2072. Message(parser_e_illegal_expression)
  2073. else
  2074. begin
  2075. srsymtable:=trecordsymtable.create(defname,0,1);
  2076. basedef:=crecorddef.create(defname,srsymtable);
  2077. include(constraintdata.flags,gcf_record);
  2078. allowconstructor:=false;
  2079. end;
  2080. end;
  2081. else
  2082. begin
  2083. { after single_type "token" is the trailing ",", ";" or
  2084. ">"! }
  2085. doconsume:=false;
  2086. { def is already set to a class or record }
  2087. if gcf_record in constraintdata.flags then
  2088. Message(parser_e_illegal_expression);
  2089. single_type(def, [stoAllowSpecialization]);
  2090. { only types that are inheritable are allowed }
  2091. if (def.typ<>objectdef) or
  2092. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  2093. Message1(type_e_class_or_interface_type_expected,def.typename)
  2094. else
  2095. case tobjectdef(def).objecttype of
  2096. odt_class,
  2097. odt_javaclass:
  2098. begin
  2099. if gcf_class in constraintdata.flags then
  2100. { "class" + concrete class is not allowed }
  2101. Message(parser_e_illegal_expression)
  2102. else
  2103. { do we already have a concrete class? }
  2104. if basedef<>generrordef then
  2105. Message(parser_e_illegal_expression)
  2106. else
  2107. basedef:=def;
  2108. end;
  2109. odt_interfacecom,
  2110. odt_interfacecorba,
  2111. odt_interfacejava,
  2112. odt_dispinterface:
  2113. constraintdata.interfaces.add(def);
  2114. else
  2115. ;
  2116. end;
  2117. end;
  2118. end;
  2119. if doconsume then
  2120. consume(token);
  2121. until not try_to_consume(_COMMA);
  2122. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  2123. (constraintdata.interfaces.count>1) or
  2124. (
  2125. (basedef.typ=objectdef) and
  2126. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  2127. ) then
  2128. begin
  2129. if basedef.typ=errordef then
  2130. { don't pass an errordef as a parent to a tobjectdef }
  2131. basedef:=class_tobject
  2132. else
  2133. if (basedef.typ<>objectdef) or
  2134. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  2135. internalerror(2012101101);
  2136. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  2137. for i:=0 to constraintdata.interfaces.count-1 do
  2138. tobjectdef(basedef).implementedinterfaces.add(
  2139. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  2140. end
  2141. else
  2142. if constraintdata.interfaces.count=1 then
  2143. begin
  2144. if basedef.typ<>errordef then
  2145. internalerror(2013021601);
  2146. def:=tdef(constraintdata.interfaces[0]);
  2147. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  2148. constraintdata.interfaces.delete(0);
  2149. end;
  2150. if basedef.typ<>errordef then
  2151. with tstoreddef(basedef) do
  2152. begin
  2153. genconstraintdata:=tgenericconstraintdata.create;
  2154. genconstraintdata.flags:=constraintdata.flags;
  2155. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  2156. genconstraintdata.fileinfo:=constraintdata.fileinfo;
  2157. include(defoptions,df_genconstraint);
  2158. end;
  2159. for i:=firstidx to result.count-1 do
  2160. ttypesym(result[i]).typedef:=basedef;
  2161. { we need a typesym in case we do a Delphi-mode inline
  2162. specialization with this parameter; so just use the first sym }
  2163. if not assigned(basedef.typesym) then
  2164. basedef.typesym:=ttypesym(result[firstidx]);
  2165. firstidx:=result.count;
  2166. constraintdata.free;
  2167. end
  2168. else
  2169. begin
  2170. if token=_SEMICOLON then
  2171. begin
  2172. { two different typeless parameters are considered as incompatible }
  2173. for i:=firstidx to result.count-1 do
  2174. if tsym(result[i]).typ<>constsym then
  2175. begin
  2176. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  2177. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  2178. end;
  2179. { a semicolon terminates a type parameter group }
  2180. firstidx:=result.count;
  2181. end;
  2182. end;
  2183. if token=_SEMICOLON then
  2184. begin
  2185. is_const:=false;
  2186. allowconst:=true;
  2187. end;
  2188. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  2189. { if the constant parameter is not terminated then the type restriction was
  2190. not specified and we need to give an error }
  2191. if is_const then
  2192. consume(_COLON);
  2193. { two different typeless parameters are considered as incompatible }
  2194. for i:=firstidx to result.count-1 do
  2195. if tsym(result[i]).typ<>constsym then
  2196. begin
  2197. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  2198. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  2199. end;
  2200. block_type:=old_block_type;
  2201. end;
  2202. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist;isfwd:boolean);
  2203. var
  2204. i : longint;
  2205. generictype,
  2206. fwdparam : tstoredsym;
  2207. generictypedef : tdef;
  2208. sym : tsym;
  2209. st : tsymtable;
  2210. fwdok : boolean;
  2211. conv : tconverttype;
  2212. op : tprocdef;
  2213. begin
  2214. def.genericdef:=genericdef;
  2215. if not assigned(genericlist) then
  2216. exit;
  2217. if assigned(genericdef) then
  2218. include(def.defoptions,df_specialization)
  2219. else
  2220. if genericlist.count>0 then
  2221. include(def.defoptions,df_generic);
  2222. case def.typ of
  2223. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  2224. arraydef: st:=tarraydef(def).symtable;
  2225. procvardef,procdef: st:=tabstractprocdef(def).parast;
  2226. else
  2227. internalerror(201101020);
  2228. end;
  2229. { if we have a forwarddef we check whether the generic parameters are
  2230. equal and otherwise ignore the list }
  2231. if isfwd then
  2232. begin
  2233. fwdok:=true;
  2234. if (genericlist.count>0) and
  2235. (
  2236. not assigned(def.genericparas)
  2237. or (def.genericparas.count<>genericlist.count)
  2238. ) then
  2239. fwdok:=false
  2240. else
  2241. begin
  2242. for i:=0 to genericlist.count-1 do
  2243. begin
  2244. if def.genericparas.nameofindex(i)<>genericlist.nameofindex(i) then
  2245. begin
  2246. fwdok:=false;
  2247. break;
  2248. end;
  2249. generictype:=tstoredsym(genericlist[i]);
  2250. fwdparam:=tstoredsym(def.genericparas[i]);
  2251. op:=nil;
  2252. conv:=tc_equal;
  2253. if generictype.typ<>fwdparam.typ then
  2254. fwdok:=false
  2255. else if (generictype.typ=typesym) then
  2256. begin
  2257. if compare_defs_ext(ttypesym(generictype).typedef,ttypesym(fwdparam).typedef,nothingn,conv,op,[cdo_strict_genconstraint_check])<te_exact then
  2258. fwdok:=false;
  2259. end
  2260. else if (generictype.typ=constsym) then
  2261. begin
  2262. if (tconstsym(generictype).consttyp<>tconstsym(fwdparam).consttyp) or
  2263. (compare_defs_ext(tconstsym(generictype).constdef,tconstsym(fwdparam).constdef,nothingn,conv,op,[cdo_strict_genconstraint_check])<te_exact) then
  2264. fwdok:=false;
  2265. end
  2266. else
  2267. internalerror(2020070101);
  2268. if not fwdok then
  2269. break;
  2270. end;
  2271. end;
  2272. if not fwdok then
  2273. Message(parser_e_forward_mismatch);
  2274. exit;
  2275. end;
  2276. if (genericlist.count>0) and not assigned(def.genericparas) then
  2277. def.genericparas:=tfphashobjectlist.create(false);
  2278. for i:=0 to genericlist.count-1 do
  2279. begin
  2280. generictype:=tstoredsym(genericlist[i]);
  2281. if assigned(generictype.owner) then
  2282. begin
  2283. if generictype.typ=typesym then
  2284. sym:=ctypesym.create(genericlist.nameofindex(i),ttypesym(generictype).typedef)
  2285. else if generictype.typ=constsym then
  2286. { generictype is a constsym that was created in create_generic_constsym
  2287. during phase 1 so we pass this directly without copying }
  2288. begin
  2289. sym:=generictype;
  2290. { the sym name is still undefined so we set it to match
  2291. the generic param name so it's accessible }
  2292. sym.realname:=genericlist.nameofindex(i);
  2293. include(sym.symoptions,sp_generic_const);
  2294. end
  2295. else
  2296. internalerror(2019021602);
  2297. { type parameters need to be added as strict private }
  2298. sym.visibility:=vis_strictprivate;
  2299. st.insertsym(sym);
  2300. include(sym.symoptions,sp_generic_para);
  2301. end
  2302. else
  2303. begin
  2304. if generictype.typ=typesym then
  2305. begin
  2306. generictypedef:=ttypesym(generictype).typedef;
  2307. if (generictypedef.typ=undefineddef) and (generictypedef<>cundefinedtype) then
  2308. begin
  2309. { the generic parameters were parsed before the genericdef existed thus the
  2310. undefineddefs were added as part of the parent symtable }
  2311. if assigned(generictypedef.owner) then
  2312. generictypedef.owner.DefList.Extract(generictypedef);
  2313. generictypedef.changeowner(st);
  2314. end;
  2315. end;
  2316. st.insertsym(generictype);
  2317. include(generictype.symoptions,sp_generic_para);
  2318. end;
  2319. def.genericparas.add(genericlist.nameofindex(i),generictype);
  2320. end;
  2321. end;
  2322. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  2323. var
  2324. gensym : ttypesym;
  2325. begin
  2326. { for generics in non-Delphi modes we insert a private type symbol
  2327. that has the same base name as the currently parsed generic and
  2328. that references this defs }
  2329. if not (m_delphi in current_settings.modeswitches) and
  2330. (
  2331. (
  2332. parse_generic and
  2333. assigned(genericlist) and
  2334. (genericlist.count>0)
  2335. ) or
  2336. (
  2337. assigned(current_specializedef) and
  2338. assigned(current_structdef.genericdef) and
  2339. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  2340. (pos('$',name)>0)
  2341. )
  2342. ) then
  2343. begin
  2344. { we need to pass nil as def here, because the constructor wants
  2345. to set the typesym of the def which is not what we want }
  2346. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  2347. gensym.typedef:=current_structdef;
  2348. include(gensym.symoptions,sp_internal);
  2349. { the symbol should be only visible to the generic class
  2350. itself }
  2351. gensym.visibility:=vis_strictprivate;
  2352. symtablestack.top.insertsym(gensym);
  2353. end;
  2354. end;
  2355. function generate_generic_name(const name:tidstring;const specializename:ansistring;const owner_hierarchy:string):tidstring;
  2356. var
  2357. crc : cardinal;
  2358. begin
  2359. if specializename='' then
  2360. internalerror(2012061901);
  2361. { build the new type's name }
  2362. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  2363. result:=name+'$crc'+hexstr(crc,8);
  2364. if owner_hierarchy<>'' then
  2365. begin
  2366. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  2367. result:=result+'$crc'+hexstr(crc,8);
  2368. end;
  2369. end;
  2370. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  2371. var
  2372. i,code : longint;
  2373. countstr : string;
  2374. begin
  2375. for i:=length(name) downto 1 do
  2376. if name[i]='$' then
  2377. begin
  2378. nongeneric:=copy(name,1,i-1);
  2379. countstr:=copy(name,i+1,length(name)-i);
  2380. val(countstr,count,code);
  2381. if code<>0 then
  2382. break;
  2383. exit;
  2384. end;
  2385. nongeneric:=name;
  2386. count:=0;
  2387. end;
  2388. procedure add_generic_dummysym(sym:tsym);
  2389. var
  2390. list: TFPObjectList;
  2391. srsym : tsym;
  2392. srsymtable : tsymtable;
  2393. entry : tgenericdummyentry;
  2394. begin
  2395. if sp_generic_dummy in sym.symoptions then
  2396. begin
  2397. { did we already search for a generic with that name? }
  2398. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  2399. if not assigned(list) then
  2400. begin
  2401. list:=tfpobjectlist.create(true);
  2402. current_module.genericdummysyms.add(sym.name,list);
  2403. end;
  2404. { is the dummy sym still "dummy"? }
  2405. if (sym.typ=typesym) and
  2406. (
  2407. { dummy sym defined in mode Delphi }
  2408. (ttypesym(sym).typedef.typ=undefineddef) or
  2409. { dummy sym defined in non-Delphi mode }
  2410. (tstoreddef(ttypesym(sym).typedef).is_generic)
  2411. ) then
  2412. begin
  2413. { do we have a non-generic type of the same name
  2414. available? }
  2415. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  2416. srsym:=nil;
  2417. end
  2418. else if sym.typ=procsym then
  2419. srsym:=sym
  2420. else
  2421. { dummy symbol is already not so dummy anymore }
  2422. srsym:=nil;
  2423. if assigned(srsym) then
  2424. begin
  2425. entry:=tgenericdummyentry.create;
  2426. entry.resolvedsym:=srsym;
  2427. entry.dummysym:=sym;
  2428. list.add(entry);
  2429. end;
  2430. end;
  2431. end;
  2432. function resolve_generic_dummysym(const name:tidstring):tsym;
  2433. var
  2434. list : tfpobjectlist;
  2435. begin
  2436. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  2437. if assigned(list) and (list.count>0) then
  2438. result:=tgenericdummyentry(list.last).resolvedsym
  2439. else
  2440. result:=nil;
  2441. end;
  2442. function could_be_generic(const name:tidstring):boolean;
  2443. begin
  2444. result:=(name<>'') and
  2445. (current_module.genericdummysyms.findindexof(name)>=0);
  2446. end;
  2447. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  2448. var
  2449. pu : tused_unit;
  2450. hmodule : tmodule;
  2451. unitsyms : TFPHashObjectList;
  2452. sym : tsym;
  2453. i : Integer;
  2454. begin
  2455. if not assigned(genericdef) then
  2456. internalerror(200705151);
  2457. { Setup symtablestack at definition time
  2458. to get types right, however this is not perfect, we should probably record
  2459. the resolved symbols }
  2460. state.oldsymtablestack:=symtablestack;
  2461. state.oldextendeddefs:=current_module.extendeddefs;
  2462. state.oldgenericdummysyms:=current_module.genericdummysyms;
  2463. current_module.extendeddefs:=TFPHashObjectList.create(true);
  2464. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  2465. symtablestack:=tdefawaresymtablestack.create;
  2466. hmodule:=find_module_from_symtable(genericdef.owner);
  2467. if hmodule=nil then
  2468. internalerror(200705152);
  2469. { collect all unit syms in the generic's unit as we need to establish
  2470. their unitsym.module link again so that unit identifiers can be used }
  2471. unitsyms:=tfphashobjectlist.create(false);
  2472. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  2473. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  2474. begin
  2475. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  2476. if sym.typ=unitsym then
  2477. unitsyms.add(upper(sym.realname),sym);
  2478. end;
  2479. { add all units if we are specializing inside the current unit (as the
  2480. generic could have been declared in the implementation part), but load
  2481. only interface units, if we are in a different unit as then the generic
  2482. needs to be in the interface section }
  2483. pu:=tused_unit(hmodule.used_units.first);
  2484. while assigned(pu) do
  2485. begin
  2486. if not assigned(pu.u.globalsymtable) then
  2487. { in certain circular, but valid unit constellations it can happen
  2488. that we specialize a generic in a different unit that was used
  2489. in the implementation section of the generic's unit and were the
  2490. interface is still being parsed and thus the localsymtable is in
  2491. reality the global symtable }
  2492. if pu.u.in_interface then
  2493. symtablestack.push(pu.u.localsymtable)
  2494. else
  2495. internalerror(200705153)
  2496. else
  2497. symtablestack.push(pu.u.globalsymtable);
  2498. sym:=tsym(unitsyms.find(pu.u.modulename^));
  2499. if assigned(sym) and not assigned(tunitsym(sym).module) then
  2500. tunitsym(sym).module:=pu.u;
  2501. pu:=tused_unit(pu.next);
  2502. end;
  2503. unitsyms.free;
  2504. if assigned(hmodule.globalsymtable) then
  2505. symtablestack.push(hmodule.globalsymtable);
  2506. { push the localsymtable if needed }
  2507. if ((hmodule<>current_module) or not current_module.in_interface)
  2508. and assigned(hmodule.localsymtable) then
  2509. symtablestack.push(hmodule.localsymtable);
  2510. end;
  2511. procedure specialization_done(var state: tspecializationstate);
  2512. begin
  2513. { Restore symtablestack }
  2514. current_module.extendeddefs.free;
  2515. current_module.extendeddefs:=state.oldextendeddefs;
  2516. current_module.genericdummysyms.free;
  2517. current_module.genericdummysyms:=state.oldgenericdummysyms;
  2518. symtablestack.free;
  2519. symtablestack:=state.oldsymtablestack;
  2520. { clear the state record to be on the safe side }
  2521. fillchar(state, sizeof(state), 0);
  2522. end;
  2523. {****************************************************************************
  2524. SPECIALIZATION BODY GENERATION
  2525. ****************************************************************************}
  2526. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  2527. var
  2528. oldcurrent_filepos : tfileposinfo;
  2529. begin
  2530. if assigned(def.genericdef) and
  2531. (def.genericdef.typ=procdef) and
  2532. assigned(tprocdef(def.genericdef).generictokenbuf) then
  2533. begin
  2534. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  2535. internalerror(2015061902);
  2536. oldcurrent_filepos:=current_filepos;
  2537. current_filepos:=tprocdef(def.genericdef).fileinfo;
  2538. { use the index the module got from the current compilation process }
  2539. current_filepos.moduleindex:=hmodule.unit_index;
  2540. current_tokenpos:=current_filepos;
  2541. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf,hmodule.change_endian);
  2542. read_proc_body(def);
  2543. current_filepos:=oldcurrent_filepos;
  2544. end
  2545. { synthetic routines will be implemented afterwards }
  2546. else if def.synthetickind=tsk_none then
  2547. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  2548. end;
  2549. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  2550. var
  2551. i : longint;
  2552. hp : tdef;
  2553. hmodule : tmodule;
  2554. begin
  2555. result:=true;
  2556. hmodule:=find_module_from_symtable(def.genericdef.owner);
  2557. if hmodule=nil then
  2558. internalerror(201202041);
  2559. for i:=0 to def.symtable.DefList.Count-1 do
  2560. begin
  2561. hp:=tdef(def.symtable.DefList[i]);
  2562. if hp.typ=procdef then
  2563. begin
  2564. { only generate the code if we need a body }
  2565. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  2566. continue;
  2567. { and the body is available already (which is implicitely the
  2568. case if the generic routine is part of another unit) }
  2569. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and
  2570. { may not be assigned in case it's a synthetic procdef that
  2571. still needs to be generated }
  2572. assigned(tprocdef(hp).genericdef) and
  2573. tprocdef(tprocdef(hp).genericdef).forwarddef then
  2574. begin
  2575. result:=false;
  2576. continue;
  2577. end;
  2578. process_procdef(tprocdef(hp),hmodule);
  2579. end
  2580. else
  2581. if hp.typ in [objectdef,recorddef] then
  2582. { generate code for subtypes as well }
  2583. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  2584. end;
  2585. end;
  2586. procedure generate_specialization_procs;
  2587. var
  2588. i : longint;
  2589. list,
  2590. readdlist : tfpobjectlist;
  2591. def : tstoreddef;
  2592. state : tspecializationstate;
  2593. hmodule : tmodule;
  2594. begin
  2595. { first copy all entries and then work with that list to ensure that
  2596. we don't get an infinite recursion }
  2597. list:=tfpobjectlist.create(false);
  2598. readdlist:=tfpobjectlist.create(false);
  2599. for i:=0 to current_module.pendingspecializations.Count-1 do
  2600. list.add(current_module.pendingspecializations.Items[i]);
  2601. current_module.pendingspecializations.clear;
  2602. for i:=0 to list.count-1 do
  2603. begin
  2604. def:=tstoreddef(list[i]);
  2605. if not tstoreddef(def).is_specialization then
  2606. continue;
  2607. case def.typ of
  2608. procdef:
  2609. begin
  2610. { the use of forwarddef should not backfire as the
  2611. specialization always belongs to the current module }
  2612. if not tprocdef(def).forwarddef then
  2613. continue;
  2614. if not assigned(def.genericdef) then
  2615. internalerror(2015061903);
  2616. hmodule:=find_module_from_symtable(def.genericdef.owner);
  2617. if hmodule=nil then
  2618. internalerror(2015061904);
  2619. { we need to check for a forward declaration only if the
  2620. generic was declared in the same unit (otherwise there
  2621. should be one) }
  2622. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  2623. begin
  2624. readdlist.add(def);
  2625. continue;
  2626. end;
  2627. specialization_init(tstoreddef(def).genericdef,state);
  2628. process_procdef(tprocdef(def),hmodule);
  2629. specialization_done(state);
  2630. end;
  2631. recorddef,
  2632. objectdef:
  2633. begin
  2634. specialization_init(tstoreddef(def).genericdef,state);
  2635. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  2636. readdlist.add(def);
  2637. specialization_done(state);
  2638. end;
  2639. else
  2640. ;
  2641. end;
  2642. end;
  2643. { add those defs back to the pending list for which we don't yet have
  2644. all method bodies }
  2645. for i:=0 to readdlist.count-1 do
  2646. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  2647. readdlist.free;
  2648. list.free;
  2649. end;
  2650. procedure generate_specializations_for_forwarddef(def:tdef);
  2651. var
  2652. list : tfpobjectlist;
  2653. idx,
  2654. i : longint;
  2655. context : tspecializationcontext;
  2656. begin
  2657. if not tstoreddef(def).is_generic then
  2658. internalerror(2020070304);
  2659. idx:=current_module.forwardgenericdefs.findindexof(def.fulltypename);
  2660. if idx<0 then
  2661. exit;
  2662. list:=tfpobjectlist(current_module.forwardgenericdefs.items[idx]);
  2663. if not assigned(list) then
  2664. internalerror(2020070305);
  2665. for i:=0 to list.count-1 do begin
  2666. context:=tspecializationcontext(list[i]);
  2667. generate_specialization_phase2(context,tstoreddef(def),false,'');
  2668. end;
  2669. current_module.forwardgenericdefs.delete(idx);
  2670. end;
  2671. procedure maybe_add_pending_specialization(def:tdef;unnamed_syms: tfplist);
  2672. var
  2673. hmodule : tmodule;
  2674. st : tsymtable;
  2675. i : integer;
  2676. begin
  2677. if parse_generic then
  2678. exit;
  2679. { transfer ownership of any unnamed syms to be the specialization }
  2680. if unnamed_syms<>nil then
  2681. transfer_unnamed_symbols(tprocdef(def).parast,unnamed_syms);
  2682. st:=def.owner;
  2683. while st.symtabletype in [localsymtable] do
  2684. st:=st.defowner.owner;
  2685. hmodule:=find_module_from_symtable(st);
  2686. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  2687. current_module.pendingspecializations.add(def.typename,def);
  2688. end;
  2689. function determine_generic_def(const name:tidstring):tstoreddef;
  2690. var
  2691. hashedid : THashedIDString;
  2692. pd : tprocdef;
  2693. sym : tsym;
  2694. begin
  2695. result:=nil;
  2696. { check whether this is a declaration of a type inside a
  2697. specialization }
  2698. if assigned(current_structdef) and
  2699. (df_specialization in current_structdef.defoptions) then
  2700. begin
  2701. if not assigned(current_structdef.genericdef) or
  2702. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  2703. internalerror(2011052301);
  2704. hashedid.id:=name;
  2705. { we could be inside a method of the specialization
  2706. instead of its declaration, so check that first (as
  2707. local nested types aren't allowed we don't need to
  2708. walk the symtablestack to find the localsymtable) }
  2709. if symtablestack.top.symtabletype=localsymtable then
  2710. begin
  2711. { we are in a method }
  2712. if not assigned(symtablestack.top.defowner) or
  2713. (symtablestack.top.defowner.typ<>procdef) then
  2714. internalerror(2011120701);
  2715. pd:=tprocdef(symtablestack.top.defowner);
  2716. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  2717. internalerror(2011120702);
  2718. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  2719. end
  2720. else
  2721. sym:=nil;
  2722. if not assigned(sym) or not (sym.typ=typesym) then
  2723. begin
  2724. { now search in the declaration of the generic }
  2725. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  2726. if not assigned(sym) or not (sym.typ=typesym) then
  2727. internalerror(2011052302);
  2728. end;
  2729. { use the corresponding type in the generic's symtable as
  2730. genericdef for the specialized type }
  2731. result:=tstoreddef(ttypesym(sym).typedef);
  2732. end;
  2733. end;
  2734. end.