pgenutil.pas 129 KB

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