pgenutil.pas 128 KB

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