pgenutil.pas 127 KB

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