pgenutil.pas 120 KB

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