pgenutil.pas 127 KB

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