ncnv.pas 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totypedef : tdef;
  29. totypedefderef : tderef;
  30. convtype : tconverttype;
  31. constructor create(node : tnode;def:tdef);virtual;
  32. constructor create_explicit(node : tnode;def:tdef);
  33. constructor create_internal(node : tnode;def:tdef);
  34. constructor create_proc_to_procvar(node : tnode);
  35. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  36. procedure ppuwrite(ppufile:tcompilerppufile);override;
  37. procedure buildderefimpl;override;
  38. procedure derefimpl;override;
  39. function dogetcopy : tnode;override;
  40. procedure printnodeinfo(var t : text);override;
  41. function pass_1 : tnode;override;
  42. function pass_typecheck:tnode;override;
  43. function simplify:tnode; override;
  44. procedure mark_write;override;
  45. function docompare(p: tnode) : boolean; override;
  46. function assign_allowed:boolean;
  47. procedure second_call_helper(c : tconverttype);
  48. private
  49. function typecheck_int_to_int : tnode;
  50. function typecheck_cord_to_pointer : tnode;
  51. function typecheck_chararray_to_string : tnode;
  52. function typecheck_string_to_chararray : tnode;
  53. function typecheck_string_to_string : tnode;
  54. function typecheck_char_to_string : tnode;
  55. function typecheck_char_to_chararray : tnode;
  56. function typecheck_int_to_real : tnode;
  57. function typecheck_real_to_real : tnode;
  58. function typecheck_real_to_currency : tnode;
  59. function typecheck_cchar_to_pchar : tnode;
  60. function typecheck_cstring_to_pchar : tnode;
  61. function typecheck_cstring_to_int : tnode;
  62. function typecheck_char_to_char : tnode;
  63. function typecheck_arrayconstructor_to_set : tnode;
  64. function typecheck_pchar_to_string : tnode;
  65. function typecheck_interface_to_guid : tnode;
  66. function typecheck_dynarray_to_openarray : tnode;
  67. function typecheck_pwchar_to_string : tnode;
  68. function typecheck_variant_to_dynarray : tnode;
  69. function typecheck_dynarray_to_variant : tnode;
  70. function typecheck_call_helper(c : tconverttype) : tnode;
  71. function typecheck_variant_to_enum : tnode;
  72. function typecheck_enum_to_variant : tnode;
  73. function typecheck_proc_to_procvar : tnode;
  74. function typecheck_variant_to_interface : tnode;
  75. function typecheck_interface_to_variant : tnode;
  76. function typecheck_array_2_dynarray : tnode;
  77. protected
  78. function first_int_to_int : tnode;virtual;
  79. function first_cstring_to_pchar : tnode;virtual;
  80. function first_cstring_to_int : tnode;virtual;
  81. function first_string_to_chararray : tnode;virtual;
  82. function first_char_to_string : tnode;virtual;
  83. function first_nothing : tnode;virtual;
  84. function first_array_to_pointer : tnode;virtual;
  85. function first_int_to_real : tnode;virtual;
  86. function first_real_to_real : tnode;virtual;
  87. function first_pointer_to_array : tnode;virtual;
  88. function first_cchar_to_pchar : tnode;virtual;
  89. function first_bool_to_int : tnode;virtual;
  90. function first_int_to_bool : tnode;virtual;
  91. function first_bool_to_bool : tnode;virtual;
  92. function first_proc_to_procvar : tnode;virtual;
  93. function first_load_smallset : tnode;virtual;
  94. function first_cord_to_pointer : tnode;virtual;
  95. function first_ansistring_to_pchar : tnode;virtual;
  96. function first_arrayconstructor_to_set : tnode;virtual;
  97. function first_class_to_intf : tnode;virtual;
  98. function first_char_to_char : tnode;virtual;
  99. function first_call_helper(c : tconverttype) : tnode;
  100. { these wrapper are necessary, because the first_* stuff is called }
  101. { through a table. Without the wrappers override wouldn't have }
  102. { any effect }
  103. function _first_int_to_int : tnode;
  104. function _first_cstring_to_pchar : tnode;
  105. function _first_cstring_to_int : tnode;
  106. function _first_string_to_chararray : tnode;
  107. function _first_char_to_string : tnode;
  108. function _first_nothing : tnode;
  109. function _first_array_to_pointer : tnode;
  110. function _first_int_to_real : tnode;
  111. function _first_real_to_real: tnode;
  112. function _first_pointer_to_array : tnode;
  113. function _first_cchar_to_pchar : tnode;
  114. function _first_bool_to_int : tnode;
  115. function _first_int_to_bool : tnode;
  116. function _first_bool_to_bool : tnode;
  117. function _first_proc_to_procvar : tnode;
  118. function _first_load_smallset : tnode;
  119. function _first_cord_to_pointer : tnode;
  120. function _first_ansistring_to_pchar : tnode;
  121. function _first_arrayconstructor_to_set : tnode;
  122. function _first_class_to_intf : tnode;
  123. function _first_char_to_char : tnode;
  124. procedure _second_int_to_int;virtual;
  125. procedure _second_string_to_string;virtual;
  126. procedure _second_cstring_to_pchar;virtual;
  127. procedure _second_cstring_to_int;virtual;
  128. procedure _second_string_to_chararray;virtual;
  129. procedure _second_array_to_pointer;virtual;
  130. procedure _second_pointer_to_array;virtual;
  131. procedure _second_chararray_to_string;virtual;
  132. procedure _second_char_to_string;virtual;
  133. procedure _second_int_to_real;virtual;
  134. procedure _second_real_to_real;virtual;
  135. procedure _second_cord_to_pointer;virtual;
  136. procedure _second_proc_to_procvar;virtual;
  137. procedure _second_bool_to_int;virtual;
  138. procedure _second_int_to_bool;virtual;
  139. procedure _second_bool_to_bool;virtual;
  140. procedure _second_load_smallset;virtual;
  141. procedure _second_ansistring_to_pchar;virtual;
  142. procedure _second_class_to_intf;virtual;
  143. procedure _second_char_to_char;virtual;
  144. procedure _second_nothing; virtual;
  145. procedure second_int_to_int;virtual;abstract;
  146. procedure second_string_to_string;virtual;abstract;
  147. procedure second_cstring_to_pchar;virtual;abstract;
  148. procedure second_cstring_to_int;virtual;abstract;
  149. procedure second_string_to_chararray;virtual;abstract;
  150. procedure second_array_to_pointer;virtual;abstract;
  151. procedure second_pointer_to_array;virtual;abstract;
  152. procedure second_chararray_to_string;virtual;abstract;
  153. procedure second_char_to_string;virtual;abstract;
  154. procedure second_int_to_real;virtual;abstract;
  155. procedure second_real_to_real;virtual;abstract;
  156. procedure second_cord_to_pointer;virtual;abstract;
  157. procedure second_proc_to_procvar;virtual;abstract;
  158. procedure second_bool_to_int;virtual;abstract;
  159. procedure second_int_to_bool;virtual;abstract;
  160. procedure second_bool_to_bool;virtual;abstract;
  161. procedure second_load_smallset;virtual;abstract;
  162. procedure second_ansistring_to_pchar;virtual;abstract;
  163. procedure second_class_to_intf;virtual;abstract;
  164. procedure second_char_to_char;virtual;abstract;
  165. procedure second_nothing; virtual;abstract;
  166. end;
  167. ttypeconvnodeclass = class of ttypeconvnode;
  168. tasnode = class(tbinarynode)
  169. constructor create(l,r : tnode);virtual;
  170. function pass_1 : tnode;override;
  171. function pass_typecheck:tnode;override;
  172. function dogetcopy: tnode;override;
  173. destructor destroy; override;
  174. call: tnode;
  175. end;
  176. tasnodeclass = class of tasnode;
  177. tisnode = class(tbinarynode)
  178. constructor create(l,r : tnode);virtual;
  179. function pass_1 : tnode;override;
  180. function pass_typecheck:tnode;override;
  181. procedure pass_generate_code;override;
  182. end;
  183. tisnodeclass = class of tisnode;
  184. var
  185. ctypeconvnode : ttypeconvnodeclass;
  186. casnode : tasnodeclass;
  187. cisnode : tisnodeclass;
  188. procedure inserttypeconv(var p:tnode;def:tdef);
  189. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  190. procedure arrayconstructor_to_set(var p : tnode);
  191. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  192. implementation
  193. uses
  194. cclasses,globtype,systems,
  195. cutils,verbose,globals,widestr,
  196. symconst,symdef,symsym,symbase,symtable,
  197. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  198. cgbase,procinfo,
  199. htypechk,pass_1,cpuinfo;
  200. {*****************************************************************************
  201. Helpers
  202. *****************************************************************************}
  203. procedure inserttypeconv(var p:tnode;def:tdef);
  204. begin
  205. if not assigned(p.resultdef) then
  206. begin
  207. typecheckpass(p);
  208. if codegenerror then
  209. exit;
  210. end;
  211. { don't insert obsolete type conversions }
  212. if equal_defs(p.resultdef,def) and
  213. not ((p.resultdef.typ=setdef) and
  214. (tsetdef(p.resultdef).settype <>
  215. tsetdef(def).settype)) then
  216. begin
  217. p.resultdef:=def;
  218. end
  219. else
  220. begin
  221. p:=ctypeconvnode.create(p,def);
  222. typecheckpass(p);
  223. end;
  224. end;
  225. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  226. begin
  227. if not assigned(p.resultdef) then
  228. begin
  229. typecheckpass(p);
  230. if codegenerror then
  231. exit;
  232. end;
  233. { don't insert obsolete type conversions }
  234. if equal_defs(p.resultdef,def) and
  235. not ((p.resultdef.typ=setdef) and
  236. (tsetdef(p.resultdef).settype <>
  237. tsetdef(def).settype)) then
  238. begin
  239. p.resultdef:=def;
  240. end
  241. else
  242. begin
  243. p:=ctypeconvnode.create_internal(p,def);
  244. typecheckpass(p);
  245. end;
  246. end;
  247. {*****************************************************************************
  248. Array constructor to Set Conversion
  249. *****************************************************************************}
  250. procedure arrayconstructor_to_set(var p : tnode);
  251. var
  252. constp : tsetconstnode;
  253. buildp,
  254. p2,p3,p4 : tnode;
  255. hdef : tdef;
  256. constset : Pconstset;
  257. constsetlo,
  258. constsethi : TConstExprInt;
  259. procedure update_constsethi(def:tdef);
  260. begin
  261. if ((def.typ=orddef) and
  262. (torddef(def).high>=constsethi)) then
  263. begin
  264. if torddef(def).ordtype=uwidechar then
  265. begin
  266. constsethi:=255;
  267. if hdef=nil then
  268. hdef:=def;
  269. end
  270. else
  271. begin
  272. constsethi:=torddef(def).high;
  273. if hdef=nil then
  274. begin
  275. if (constsethi>255) or
  276. (torddef(def).low<0) then
  277. hdef:=u8inttype
  278. else
  279. hdef:=def;
  280. end;
  281. if constsethi>255 then
  282. constsethi:=255;
  283. end;
  284. end
  285. else if ((def.typ=enumdef) and
  286. (tenumdef(def).max>=constsethi)) then
  287. begin
  288. if hdef=nil then
  289. hdef:=def;
  290. constsethi:=tenumdef(def).max;
  291. end;
  292. end;
  293. procedure do_set(pos : longint);
  294. begin
  295. if (pos and not $ff)<>0 then
  296. Message(parser_e_illegal_set_expr);
  297. if pos>constsethi then
  298. constsethi:=pos;
  299. if pos<constsetlo then
  300. constsetlo:=pos;
  301. if pos in constset^ then
  302. Message(parser_e_illegal_set_expr);
  303. include(constset^,pos);
  304. end;
  305. var
  306. l : Longint;
  307. lr,hr : TConstExprInt;
  308. hp : tarrayconstructornode;
  309. begin
  310. if p.nodetype<>arrayconstructorn then
  311. internalerror(200205105);
  312. new(constset);
  313. constset^:=[];
  314. hdef:=nil;
  315. constsetlo:=0;
  316. constsethi:=0;
  317. constp:=csetconstnode.create(nil,hdef);
  318. constp.value_set:=constset;
  319. buildp:=constp;
  320. hp:=tarrayconstructornode(p);
  321. if assigned(hp.left) then
  322. begin
  323. while assigned(hp) do
  324. begin
  325. p4:=nil; { will contain the tree to create the set }
  326. {split a range into p2 and p3 }
  327. if hp.left.nodetype=arrayconstructorrangen then
  328. begin
  329. p2:=tarrayconstructorrangenode(hp.left).left;
  330. p3:=tarrayconstructorrangenode(hp.left).right;
  331. tarrayconstructorrangenode(hp.left).left:=nil;
  332. tarrayconstructorrangenode(hp.left).right:=nil;
  333. end
  334. else
  335. begin
  336. p2:=hp.left;
  337. hp.left:=nil;
  338. p3:=nil;
  339. end;
  340. typecheckpass(p2);
  341. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  342. if assigned(p3) then
  343. begin
  344. typecheckpass(p3);
  345. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  346. end;
  347. if codegenerror then
  348. break;
  349. case p2.resultdef.typ of
  350. enumdef,
  351. orddef:
  352. begin
  353. getrange(p2.resultdef,lr,hr);
  354. if assigned(p3) then
  355. begin
  356. { this isn't good, you'll get problems with
  357. type t010 = 0..10;
  358. ts = set of t010;
  359. var s : ts;b : t010
  360. begin s:=[1,2,b]; end.
  361. if is_integer(p3^.resultdef) then
  362. begin
  363. inserttypeconv(p3,u8bitdef);
  364. end;
  365. }
  366. if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
  367. begin
  368. current_filepos:=p3.fileinfo;
  369. CGMessage(type_e_typeconflict_in_set);
  370. end
  371. else
  372. begin
  373. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  374. begin
  375. if not(is_integer(p3.resultdef)) then
  376. hdef:=p3.resultdef
  377. else
  378. begin
  379. inserttypeconv(p3,u8inttype);
  380. inserttypeconv(p2,u8inttype);
  381. end;
  382. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  383. do_set(l);
  384. p2.free;
  385. p3.free;
  386. end
  387. else
  388. begin
  389. update_constsethi(p2.resultdef);
  390. inserttypeconv(p2,hdef);
  391. update_constsethi(p3.resultdef);
  392. inserttypeconv(p3,hdef);
  393. if assigned(hdef) then
  394. inserttypeconv(p3,hdef)
  395. else
  396. inserttypeconv(p3,u8inttype);
  397. p4:=csetelementnode.create(p2,p3);
  398. end;
  399. end;
  400. end
  401. else
  402. begin
  403. { Single value }
  404. if p2.nodetype=ordconstn then
  405. begin
  406. if not(is_integer(p2.resultdef)) then
  407. begin
  408. { for constant set elements, delphi allows the usage of elements of enumerations which
  409. have value>255 if there is no element with a value > 255 used }
  410. if (m_delphi in current_settings.modeswitches) and (p2.resultdef.typ=enumdef) then
  411. begin
  412. if tordconstnode(p2).value>constsethi then
  413. constsethi:=tordconstnode(p2).value;
  414. if hdef=nil then
  415. hdef:=p2.resultdef;
  416. end
  417. else
  418. update_constsethi(p2.resultdef);
  419. end;
  420. if assigned(hdef) then
  421. inserttypeconv(p2,hdef)
  422. else
  423. inserttypeconv(p2,u8inttype);
  424. do_set(tordconstnode(p2).value);
  425. p2.free;
  426. end
  427. else
  428. begin
  429. update_constsethi(p2.resultdef);
  430. if assigned(hdef) then
  431. inserttypeconv(p2,hdef)
  432. else
  433. inserttypeconv(p2,u8inttype);
  434. p4:=csetelementnode.create(p2,nil);
  435. end;
  436. end;
  437. end;
  438. stringdef :
  439. begin
  440. { if we've already set elements which are constants }
  441. { throw an error }
  442. if ((hdef=nil) and assigned(buildp)) or
  443. not(is_char(hdef)) then
  444. CGMessage(type_e_typeconflict_in_set)
  445. else
  446. for l:=1 to length(pshortstring(tstringconstnode(p2).value_str)^) do
  447. do_set(ord(pshortstring(tstringconstnode(p2).value_str)^[l]));
  448. if hdef=nil then
  449. hdef:=cchartype;
  450. p2.free;
  451. end;
  452. else
  453. CGMessage(type_e_ordinal_expr_expected);
  454. end;
  455. { insert the set creation tree }
  456. if assigned(p4) then
  457. buildp:=caddnode.create(addn,buildp,p4);
  458. { load next and dispose current node }
  459. p2:=hp;
  460. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  461. tarrayconstructornode(p2).right:=nil;
  462. p2.free;
  463. end;
  464. if (hdef=nil) then
  465. hdef:=u8inttype;
  466. end
  467. else
  468. begin
  469. { empty set [], only remove node }
  470. p.free;
  471. end;
  472. { set the initial set type }
  473. constp.resultdef:=tsetdef.create(hdef,constsethi);
  474. { determine the resultdef for the tree }
  475. typecheckpass(buildp);
  476. { set the new tree }
  477. p:=buildp;
  478. end;
  479. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  480. begin
  481. if not(iscvarargs) and
  482. (p.nodetype=stringconstn) then
  483. p:=ctypeconvnode.create_internal(p,cansistringtype)
  484. else
  485. case p.resultdef.typ of
  486. enumdef :
  487. p:=ctypeconvnode.create_internal(p,s32inttype);
  488. arraydef :
  489. begin
  490. if is_chararray(p.resultdef) then
  491. p:=ctypeconvnode.create_internal(p,charpointertype)
  492. else
  493. if is_widechararray(p.resultdef) then
  494. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  495. else
  496. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  497. end;
  498. orddef :
  499. begin
  500. if is_integer(p.resultdef) and
  501. not(is_64bitint(p.resultdef)) then
  502. p:=ctypeconvnode.create(p,s32inttype)
  503. else if is_void(p.resultdef) then
  504. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  505. else if iscvarargs and
  506. is_currency(p.resultdef) then
  507. p:=ctypeconvnode.create(p,s64floattype);
  508. end;
  509. floatdef :
  510. if not(iscvarargs) then
  511. begin
  512. if not(is_currency(p.resultdef)) then
  513. p:=ctypeconvnode.create(p,pbestrealtype^);
  514. end
  515. else
  516. begin
  517. if is_constrealnode(p) and
  518. not(nf_explicit in p.flags) then
  519. MessagePos(p.fileinfo,type_w_double_c_varargs);
  520. if (tfloatdef(p.resultdef).floattype in [{$ifndef x86_64}s32real,{$endif}s64currency]) or
  521. { win64 requires the double type cast for singles as well }
  522. ((tfloatdef(p.resultdef).floattype=s32real) and (target_info.system=system_x86_64_win64)) or
  523. (is_constrealnode(p) and
  524. not(nf_explicit in p.flags)) then
  525. p:=ctypeconvnode.create(p,s64floattype);
  526. end;
  527. procvardef :
  528. p:=ctypeconvnode.create(p,voidpointertype);
  529. stringdef:
  530. if iscvarargs then
  531. p:=ctypeconvnode.create(p,charpointertype);
  532. variantdef:
  533. if iscvarargs then
  534. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  535. pointerdef:
  536. ;
  537. classrefdef:
  538. if iscvarargs then
  539. p:=ctypeconvnode.create(p,voidpointertype);
  540. objectdef :
  541. if iscvarargs or
  542. is_object(p.resultdef) then
  543. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  544. else
  545. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  546. end;
  547. typecheckpass(p);
  548. end;
  549. {*****************************************************************************
  550. TTYPECONVNODE
  551. *****************************************************************************}
  552. constructor ttypeconvnode.create(node : tnode;def:tdef);
  553. begin
  554. inherited create(typeconvn,node);
  555. convtype:=tc_none;
  556. totypedef:=def;
  557. if def=nil then
  558. internalerror(200103281);
  559. fileinfo:=node.fileinfo;
  560. end;
  561. constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
  562. begin
  563. self.create(node,def);
  564. include(flags,nf_explicit);
  565. end;
  566. constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
  567. begin
  568. self.create(node,def);
  569. { handle like explicit conversions }
  570. include(flags,nf_explicit);
  571. include(flags,nf_internal);
  572. end;
  573. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  574. begin
  575. self.create(node,voidtype);
  576. convtype:=tc_proc_2_procvar;
  577. end;
  578. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  579. begin
  580. inherited ppuload(t,ppufile);
  581. ppufile.getderef(totypedefderef);
  582. convtype:=tconverttype(ppufile.getbyte);
  583. end;
  584. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  585. begin
  586. inherited ppuwrite(ppufile);
  587. ppufile.putderef(totypedefderef);
  588. ppufile.putbyte(byte(convtype));
  589. end;
  590. procedure ttypeconvnode.buildderefimpl;
  591. begin
  592. inherited buildderefimpl;
  593. totypedefderef.build(totypedef);
  594. end;
  595. procedure ttypeconvnode.derefimpl;
  596. begin
  597. inherited derefimpl;
  598. totypedef:=tdef(totypedefderef.resolve);
  599. end;
  600. function ttypeconvnode.dogetcopy : tnode;
  601. var
  602. n : ttypeconvnode;
  603. begin
  604. n:=ttypeconvnode(inherited dogetcopy);
  605. n.convtype:=convtype;
  606. n.totypedef:=totypedef;
  607. dogetcopy:=n;
  608. end;
  609. procedure ttypeconvnode.printnodeinfo(var t : text);
  610. const
  611. convtyp2str : array[tconverttype] of pchar = (
  612. 'tc_none',
  613. 'tc_equal',
  614. 'tc_not_possible',
  615. 'tc_string_2_string',
  616. 'tc_char_2_string',
  617. 'tc_char_2_chararray',
  618. 'tc_pchar_2_string',
  619. 'tc_cchar_2_pchar',
  620. 'tc_cstring_2_pchar',
  621. 'tc_cstring_2_int',
  622. 'tc_ansistring_2_pchar',
  623. 'tc_string_2_chararray',
  624. 'tc_chararray_2_string',
  625. 'tc_array_2_pointer',
  626. 'tc_pointer_2_array',
  627. 'tc_int_2_int',
  628. 'tc_int_2_bool',
  629. 'tc_bool_2_bool',
  630. 'tc_bool_2_int',
  631. 'tc_real_2_real',
  632. 'tc_int_2_real',
  633. 'tc_real_2_currency',
  634. 'tc_proc_2_procvar',
  635. 'tc_arrayconstructor_2_set',
  636. 'tc_load_smallset',
  637. 'tc_cord_2_pointer',
  638. 'tc_intf_2_string',
  639. 'tc_intf_2_guid',
  640. 'tc_class_2_intf',
  641. 'tc_char_2_char',
  642. 'tc_normal_2_smallset',
  643. 'tc_dynarray_2_openarray',
  644. 'tc_pwchar_2_string',
  645. 'tc_variant_2_dynarray',
  646. 'tc_dynarray_2_variant',
  647. 'tc_variant_2_enum',
  648. 'tc_enum_2_variant',
  649. 'tc_interface_2_variant',
  650. 'tc_variant_2_interface',
  651. 'tc_array_2_dynarray'
  652. );
  653. begin
  654. inherited printnodeinfo(t);
  655. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  656. end;
  657. function ttypeconvnode.typecheck_cord_to_pointer : tnode;
  658. var
  659. t : tnode;
  660. begin
  661. result:=nil;
  662. if left.nodetype=ordconstn then
  663. begin
  664. { check if we have a valid pointer constant (JM) }
  665. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  666. if (sizeof(TConstPtrUInt) = 4) then
  667. begin
  668. if (tordconstnode(left).value < low(longint)) or
  669. (tordconstnode(left).value > high(cardinal)) then
  670. CGMessage(parser_e_range_check_error);
  671. end
  672. else if (sizeof(TConstPtrUInt) = 8) then
  673. begin
  674. if (tordconstnode(left).value < low(int64)) or
  675. (tordconstnode(left).value > high(qword)) then
  676. CGMessage(parser_e_range_check_error);
  677. end
  678. else
  679. internalerror(2001020801);
  680. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resultdef);
  681. result:=t;
  682. end
  683. else
  684. internalerror(200104023);
  685. end;
  686. function ttypeconvnode.typecheck_chararray_to_string : tnode;
  687. var
  688. chartype : string[8];
  689. begin
  690. if is_widechar(tarraydef(left.resultdef).elementdef) then
  691. chartype:='widechar'
  692. else
  693. chartype:='char';
  694. result := ccallnode.createinternres(
  695. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  696. ccallparanode.create(cordconstnode.create(
  697. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  698. ccallparanode.create(left,nil)),resultdef);
  699. left := nil;
  700. end;
  701. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  702. var
  703. arrsize : aint;
  704. chartype : string[8];
  705. begin
  706. result := nil;
  707. with tarraydef(resultdef) do
  708. begin
  709. if highrange<lowrange then
  710. internalerror(200501051);
  711. arrsize := highrange-lowrange+1;
  712. end;
  713. if (left.nodetype = stringconstn) and
  714. (tstringconstnode(left).cst_type=cst_conststring) then
  715. begin
  716. { if the array of char is large enough we can use the string
  717. constant directly. This is handled in ncgcnv }
  718. if (arrsize>=tstringconstnode(left).len) and
  719. is_char(tarraydef(resultdef).elementdef) then
  720. exit;
  721. { Convert to wide/short/ansistring and call default helper }
  722. if is_widechar(tarraydef(resultdef).elementdef) then
  723. inserttypeconv(left,cwidestringtype)
  724. else
  725. begin
  726. if tstringconstnode(left).len>255 then
  727. inserttypeconv(left,cansistringtype)
  728. else
  729. inserttypeconv(left,cshortstringtype);
  730. end;
  731. end;
  732. if is_widechar(tarraydef(resultdef).elementdef) then
  733. chartype:='widechar'
  734. else
  735. chartype:='char';
  736. result := ccallnode.createinternres(
  737. 'fpc_'+tstringdef(left.resultdef).stringtypname+
  738. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  739. cordconstnode.create(arrsize,s32inttype,true),nil)),resultdef);
  740. left := nil;
  741. end;
  742. function ttypeconvnode.typecheck_string_to_string : tnode;
  743. var
  744. procname: string[31];
  745. stringpara : tcallparanode;
  746. begin
  747. result:=nil;
  748. if left.nodetype=stringconstn then
  749. begin
  750. tstringconstnode(left).changestringtype(resultdef);
  751. result:=left;
  752. left:=nil;
  753. end
  754. else
  755. begin
  756. { get the correct procedure name }
  757. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  758. '_to_'+tstringdef(resultdef).stringtypname;
  759. { create parameter (and remove left node from typeconvnode }
  760. { since it's reused as parameter) }
  761. stringpara := ccallparanode.create(left,nil);
  762. left := nil;
  763. { when converting to shortstrings, we have to pass high(destination) too }
  764. if (tstringdef(resultdef).stringtype = st_shortstring) then
  765. stringpara.right := ccallparanode.create(cinlinenode.create(
  766. in_high_x,false,self.getcopy),nil);
  767. { and create the callnode }
  768. result := ccallnode.createinternres(procname,stringpara,resultdef);
  769. end;
  770. end;
  771. function ttypeconvnode.typecheck_char_to_string : tnode;
  772. var
  773. procname: string[31];
  774. para : tcallparanode;
  775. hp : tstringconstnode;
  776. ws : pcompilerwidestring;
  777. begin
  778. result:=nil;
  779. if left.nodetype=ordconstn then
  780. begin
  781. if tstringdef(resultdef).stringtype=st_widestring then
  782. begin
  783. initwidestring(ws);
  784. if torddef(left.resultdef).ordtype=uwidechar then
  785. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  786. else
  787. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  788. hp:=cstringconstnode.createwstr(ws);
  789. donewidestring(ws);
  790. end
  791. else
  792. begin
  793. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value));
  794. tstringconstnode(hp).changestringtype(resultdef);
  795. end;
  796. result:=hp;
  797. end
  798. else
  799. { shortstrings are handled 'inline' }
  800. if tstringdef(resultdef).stringtype <> st_shortstring then
  801. begin
  802. { create the parameter }
  803. para := ccallparanode.create(left,nil);
  804. left := nil;
  805. { and the procname }
  806. procname := 'fpc_char_to_' +tstringdef(resultdef).stringtypname;
  807. { and finally the call }
  808. result := ccallnode.createinternres(procname,para,resultdef);
  809. end
  810. else
  811. begin
  812. { create word(byte(char) shl 8 or 1) for litte endian machines }
  813. { and word(byte(char) or 256) for big endian machines }
  814. left := ctypeconvnode.create_internal(left,u8inttype);
  815. if (target_info.endian = endian_little) then
  816. left := caddnode.create(orn,
  817. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  818. cordconstnode.create(1,s32inttype,false))
  819. else
  820. left := caddnode.create(orn,left,
  821. cordconstnode.create(1 shl 8,s32inttype,false));
  822. left := ctypeconvnode.create_internal(left,u16inttype);
  823. typecheckpass(left);
  824. end;
  825. end;
  826. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  827. begin
  828. if resultdef.size <> 1 then
  829. begin
  830. { convert first to string, then to chararray }
  831. inserttypeconv(left,cshortstringtype);
  832. inserttypeconv(left,resultdef);
  833. result:=left;
  834. left := nil;
  835. exit;
  836. end;
  837. result := nil;
  838. end;
  839. function ttypeconvnode.typecheck_char_to_char : tnode;
  840. var
  841. hp : tordconstnode;
  842. begin
  843. result:=nil;
  844. if left.nodetype=ordconstn then
  845. begin
  846. if (torddef(resultdef).ordtype=uchar) and
  847. (torddef(left.resultdef).ordtype=uwidechar) then
  848. begin
  849. hp:=cordconstnode.create(
  850. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  851. cchartype,true);
  852. result:=hp;
  853. end
  854. else if (torddef(resultdef).ordtype=uwidechar) and
  855. (torddef(left.resultdef).ordtype=uchar) then
  856. begin
  857. hp:=cordconstnode.create(
  858. asciichar2unicode(chr(tordconstnode(left).value)),
  859. cwidechartype,true);
  860. result:=hp;
  861. end
  862. else
  863. internalerror(200105131);
  864. exit;
  865. end;
  866. end;
  867. function ttypeconvnode.typecheck_int_to_int : tnode;
  868. var
  869. v : TConstExprInt;
  870. begin
  871. result:=nil;
  872. if left.nodetype=ordconstn then
  873. begin
  874. v:=tordconstnode(left).value;
  875. if is_currency(resultdef) then
  876. v:=v*10000;
  877. if (resultdef.typ=pointerdef) then
  878. result:=cpointerconstnode.create(TConstPtrUInt(v),resultdef)
  879. else
  880. begin
  881. if is_currency(left.resultdef) then
  882. v:=v div 10000;
  883. result:=cordconstnode.create(v,resultdef,false);
  884. end;
  885. end
  886. else if left.nodetype=pointerconstn then
  887. begin
  888. v:=tpointerconstnode(left).value;
  889. if (resultdef.typ=pointerdef) then
  890. result:=cpointerconstnode.create(v,resultdef)
  891. else
  892. begin
  893. if is_currency(resultdef) then
  894. v:=v*10000;
  895. result:=cordconstnode.create(v,resultdef,false);
  896. end;
  897. end
  898. else
  899. begin
  900. { multiply by 10000 for currency. We need to use getcopy to pass
  901. the argument because the current node is always disposed. Only
  902. inserting the multiply in the left node is not possible because
  903. it'll get in an infinite loop to convert int->currency }
  904. if is_currency(resultdef) then
  905. begin
  906. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  907. include(result.flags,nf_is_currency);
  908. end
  909. else if is_currency(left.resultdef) then
  910. begin
  911. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  912. include(result.flags,nf_is_currency);
  913. end;
  914. end;
  915. end;
  916. function ttypeconvnode.typecheck_int_to_real : tnode;
  917. var
  918. rv : bestreal;
  919. begin
  920. result:=nil;
  921. if left.nodetype=ordconstn then
  922. begin
  923. rv:=tordconstnode(left).value;
  924. if is_currency(resultdef) then
  925. rv:=rv*10000.0
  926. else if is_currency(left.resultdef) then
  927. rv:=rv/10000.0;
  928. result:=crealconstnode.create(rv,resultdef);
  929. end
  930. else
  931. begin
  932. { multiply by 10000 for currency. We need to use getcopy to pass
  933. the argument because the current node is always disposed. Only
  934. inserting the multiply in the left node is not possible because
  935. it'll get in an infinite loop to convert int->currency }
  936. if is_currency(resultdef) then
  937. begin
  938. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  939. include(result.flags,nf_is_currency);
  940. end
  941. else if is_currency(left.resultdef) then
  942. begin
  943. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  944. include(result.flags,nf_is_currency);
  945. end;
  946. end;
  947. end;
  948. function ttypeconvnode.typecheck_real_to_currency : tnode;
  949. begin
  950. if not is_currency(resultdef) then
  951. internalerror(200304221);
  952. result:=nil;
  953. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  954. include(left.flags,nf_is_currency);
  955. typecheckpass(left);
  956. { Convert constants directly, else call Round() }
  957. if left.nodetype=realconstn then
  958. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  959. else
  960. result:=ccallnode.createinternres('fpc_round_real',
  961. ccallparanode.create(left,nil),resultdef);
  962. left:=nil;
  963. end;
  964. function ttypeconvnode.typecheck_real_to_real : tnode;
  965. begin
  966. result:=nil;
  967. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  968. begin
  969. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  970. include(left.flags,nf_is_currency);
  971. typecheckpass(left);
  972. end
  973. else
  974. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  975. begin
  976. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  977. include(left.flags,nf_is_currency);
  978. typecheckpass(left);
  979. end;
  980. end;
  981. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  982. begin
  983. result:=nil;
  984. if is_pwidechar(resultdef) then
  985. inserttypeconv(left,cwidestringtype)
  986. else
  987. inserttypeconv(left,cshortstringtype);
  988. { evaluate again, reset resultdef so the convert_typ
  989. will be calculated again and cstring_to_pchar will
  990. be used for futher conversion }
  991. convtype:=tc_none;
  992. result:=pass_typecheck;
  993. end;
  994. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  995. begin
  996. result:=nil;
  997. if is_pwidechar(resultdef) then
  998. inserttypeconv(left,cwidestringtype)
  999. else
  1000. if is_pchar(resultdef) and
  1001. is_widestring(left.resultdef) then
  1002. inserttypeconv(left,cansistringtype);
  1003. end;
  1004. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1005. var
  1006. fcc : cardinal;
  1007. pb : pbyte;
  1008. begin
  1009. result:=nil;
  1010. if left.nodetype<>stringconstn then
  1011. internalerror(200510012);
  1012. if tstringconstnode(left).len=4 then
  1013. begin
  1014. pb:=pbyte(tstringconstnode(left).value_str);
  1015. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1016. result:=cordconstnode.create(fcc,u32inttype,false);
  1017. end
  1018. else
  1019. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1020. end;
  1021. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1022. var
  1023. hp : tnode;
  1024. begin
  1025. result:=nil;
  1026. if left.nodetype<>arrayconstructorn then
  1027. internalerror(5546);
  1028. { remove typeconv node }
  1029. hp:=left;
  1030. left:=nil;
  1031. { create a set constructor tree }
  1032. arrayconstructor_to_set(hp);
  1033. result:=hp;
  1034. end;
  1035. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1036. begin
  1037. result := ccallnode.createinternres(
  1038. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1039. ccallparanode.create(left,nil),resultdef);
  1040. left := nil;
  1041. end;
  1042. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1043. begin
  1044. if assigned(tobjectdef(left.resultdef).iidguid) then
  1045. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1046. end;
  1047. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1048. begin
  1049. { a dynamic array is a pointer to an array, so to convert it to }
  1050. { an open array, we have to dereference it (JM) }
  1051. result := ctypeconvnode.create_internal(left,voidpointertype);
  1052. typecheckpass(result);
  1053. { left is reused }
  1054. left := nil;
  1055. result := cderefnode.create(result);
  1056. include(result.flags,nf_no_checkpointer);
  1057. result.resultdef := resultdef;
  1058. end;
  1059. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1060. begin
  1061. result := ccallnode.createinternres(
  1062. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1063. ccallparanode.create(left,nil),resultdef);
  1064. left := nil;
  1065. end;
  1066. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1067. begin
  1068. result := ccallnode.createinternres(
  1069. 'fpc_variant_to_dynarray',
  1070. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti)),
  1071. ccallparanode.create(left,nil)
  1072. ),resultdef);
  1073. typecheckpass(result);
  1074. left:=nil;
  1075. end;
  1076. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1077. begin
  1078. result := ccallnode.createinternres(
  1079. 'fpc_dynarray_to_variant',
  1080. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti)),
  1081. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1082. ),resultdef);
  1083. typecheckpass(result);
  1084. left:=nil;
  1085. end;
  1086. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1087. begin
  1088. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1089. result := ccallnode.createinternres(
  1090. 'fpc_variant_to_idispatch',
  1091. ccallparanode.create(left,nil)
  1092. ,resultdef)
  1093. else
  1094. result := ccallnode.createinternres(
  1095. 'fpc_variant_to_interface',
  1096. ccallparanode.create(left,nil)
  1097. ,resultdef);
  1098. typecheckpass(result);
  1099. left:=nil;
  1100. end;
  1101. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1102. begin
  1103. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1104. result := ccallnode.createinternres(
  1105. 'fpc_idispatch_to_variant',
  1106. ccallparanode.create(left,nil)
  1107. ,resultdef)
  1108. else
  1109. result := ccallnode.createinternres(
  1110. 'fpc_interface_to_variant',
  1111. ccallparanode.create(left,nil)
  1112. ,resultdef);
  1113. typecheckpass(result);
  1114. left:=nil;
  1115. end;
  1116. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1117. begin
  1118. result := ctypeconvnode.create_internal(left,sinttype);
  1119. result := ctypeconvnode.create_internal(result,resultdef);
  1120. typecheckpass(result);
  1121. { left is reused }
  1122. left := nil;
  1123. end;
  1124. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1125. begin
  1126. result := ctypeconvnode.create_internal(left,sinttype);
  1127. result := ctypeconvnode.create_internal(result,cvarianttype);
  1128. typecheckpass(result);
  1129. { left is reused }
  1130. left := nil;
  1131. end;
  1132. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1133. var
  1134. newstatement : tstatementnode;
  1135. temp : ttempcreatenode;
  1136. temp2 : ttempcreatenode;
  1137. begin
  1138. { create statements with call to getmem+initialize }
  1139. result:=internalstatements(newstatement);
  1140. { create temp for result }
  1141. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1142. addstatement(newstatement,temp);
  1143. { get temp for array of lengths }
  1144. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1145. addstatement(newstatement,temp2);
  1146. { one dimensional }
  1147. addstatement(newstatement,cassignmentnode.create(
  1148. ctemprefnode.create_offset(temp2,0),
  1149. cordconstnode.create
  1150. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1151. { create call to fpc_dynarr_setlength }
  1152. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1153. ccallparanode.create(caddrnode.create_internal
  1154. (ctemprefnode.create(temp2)),
  1155. ccallparanode.create(cordconstnode.create
  1156. (1,s32inttype,true),
  1157. ccallparanode.create(caddrnode.create_internal
  1158. (crttinode.create(tstoreddef(resultdef),initrtti)),
  1159. ccallparanode.create(
  1160. ctypeconvnode.create_internal(
  1161. ctemprefnode.create(temp),voidpointertype),
  1162. nil))))
  1163. ));
  1164. addstatement(newstatement,ctempdeletenode.create(temp2));
  1165. { copy ... }
  1166. addstatement(newstatement,cassignmentnode.create(
  1167. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1168. left
  1169. ));
  1170. { left is reused }
  1171. left:=nil;
  1172. { the last statement should return the value as
  1173. location and type, this is done be referencing the
  1174. temp and converting it first from a persistent temp to
  1175. normal temp }
  1176. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1177. addstatement(newstatement,ctemprefnode.create(temp));
  1178. end;
  1179. procedure copyparasym(p:TObject;arg:pointer);
  1180. var
  1181. newparast : TSymtable absolute arg;
  1182. vs : tparavarsym;
  1183. begin
  1184. if tsym(p).typ<>paravarsym then
  1185. exit;
  1186. with tparavarsym(p) do
  1187. begin
  1188. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1189. vs.defaultconstsym:=defaultconstsym;
  1190. newparast.insert(vs);
  1191. end;
  1192. end;
  1193. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1194. var
  1195. pd : tabstractprocdef;
  1196. begin
  1197. result:=nil;
  1198. pd:=tabstractprocdef(left.resultdef);
  1199. { create procvardef }
  1200. resultdef:=tprocvardef.create(pd.parast.symtablelevel);
  1201. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1202. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1203. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1204. tprocvardef(resultdef).returndef:=pd.returndef;
  1205. { method ? then set the methodpointer flag }
  1206. if (pd.owner.symtabletype=ObjectSymtable) then
  1207. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1208. { was it a local procedure? }
  1209. if (pd.owner.symtabletype=localsymtable) then
  1210. include(tprocvardef(resultdef).procoptions,po_local);
  1211. { only need the address of the method? this is needed
  1212. for @tobject.create. In this case there will be a loadn without
  1213. a methodpointer. }
  1214. if (left.nodetype=loadn) and
  1215. not assigned(tloadnode(left).left) then
  1216. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1217. { Add parameters use only references, we don't need to keep the
  1218. parast. We use the parast from the original function to calculate
  1219. our parameter data and reset it afterwards }
  1220. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1221. tprocvardef(resultdef).calcparas;
  1222. end;
  1223. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1224. const
  1225. resultdefconvert : array[tconverttype] of pointer = (
  1226. {none} nil,
  1227. {equal} nil,
  1228. {not_possible} nil,
  1229. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1230. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1231. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1232. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1233. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1234. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1235. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1236. { ansistring_2_pchar } nil,
  1237. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1238. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1239. { array_2_pointer } nil,
  1240. { pointer_2_array } nil,
  1241. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1242. { int_2_bool } nil,
  1243. { bool_2_bool } nil,
  1244. { bool_2_int } nil,
  1245. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1246. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1247. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1248. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1249. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1250. { load_smallset } nil,
  1251. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1252. { intf_2_string } nil,
  1253. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1254. { class_2_intf } nil,
  1255. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1256. { normal_2_smallset} nil,
  1257. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1258. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1259. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1260. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1261. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1262. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1263. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1264. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1265. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1266. );
  1267. type
  1268. tprocedureofobject = function : tnode of object;
  1269. var
  1270. r : packed record
  1271. proc : pointer;
  1272. obj : pointer;
  1273. end;
  1274. begin
  1275. result:=nil;
  1276. { this is a little bit dirty but it works }
  1277. { and should be quite portable too }
  1278. r.proc:=resultdefconvert[c];
  1279. r.obj:=self;
  1280. if assigned(r.proc) then
  1281. result:=tprocedureofobject(r)();
  1282. end;
  1283. function ttypeconvnode.pass_typecheck:tnode;
  1284. var
  1285. hdef : tdef;
  1286. hp : tnode;
  1287. currprocdef : tabstractprocdef;
  1288. aprocdef : tprocdef;
  1289. eq : tequaltype;
  1290. cdoptions : tcompare_defs_options;
  1291. newblock: tblocknode;
  1292. newstatement: tstatementnode;
  1293. tempnode: ttempcreatenode;
  1294. begin
  1295. result:=nil;
  1296. resultdef:=totypedef;
  1297. typecheckpass(left);
  1298. if codegenerror then
  1299. exit;
  1300. { When absolute force tc_equal }
  1301. if (nf_absolute in flags) then
  1302. begin
  1303. convtype:=tc_equal;
  1304. if not(tstoreddef(resultdef).is_intregable) and
  1305. not(tstoreddef(resultdef).is_fpuregable) then
  1306. make_not_regable(left,vr_addr);
  1307. exit;
  1308. end;
  1309. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1310. convert on the procvar value. This is used to access the
  1311. fields of a methodpointer }
  1312. if not(nf_load_procvar in flags) and
  1313. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1314. maybe_call_procvar(left,true);
  1315. { convert array constructors to sets, because there is no conversion
  1316. possible for array constructors }
  1317. if (resultdef.typ<>arraydef) and
  1318. is_array_constructor(left.resultdef) then
  1319. begin
  1320. arrayconstructor_to_set(left);
  1321. typecheckpass(left);
  1322. end;
  1323. if convtype=tc_none then
  1324. begin
  1325. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1326. if nf_explicit in flags then
  1327. include(cdoptions,cdo_explicit);
  1328. if nf_internal in flags then
  1329. include(cdoptions,cdo_internal);
  1330. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1331. case eq of
  1332. te_exact,
  1333. te_equal :
  1334. begin
  1335. result := simplify;
  1336. if assigned(result) then
  1337. exit;
  1338. { because is_equal only checks the basetype for sets we need to
  1339. check here if we are loading a smallset into a normalset }
  1340. if (resultdef.typ=setdef) and
  1341. (left.resultdef.typ=setdef) and
  1342. ((tsetdef(resultdef).settype = smallset) xor
  1343. (tsetdef(left.resultdef).settype = smallset)) then
  1344. begin
  1345. { constant sets can be converted by changing the type only }
  1346. if (left.nodetype=setconstn) then
  1347. begin
  1348. left.resultdef:=resultdef;
  1349. result:=left;
  1350. left:=nil;
  1351. exit;
  1352. end;
  1353. if (tsetdef(resultdef).settype <> smallset) then
  1354. convtype:=tc_load_smallset
  1355. else
  1356. convtype := tc_normal_2_smallset;
  1357. exit;
  1358. end
  1359. else
  1360. begin
  1361. { Only leave when there is no conversion to do.
  1362. We can still need to call a conversion routine,
  1363. like the routine to convert a stringconstnode }
  1364. if convtype in [tc_equal,tc_not_possible] then
  1365. begin
  1366. left.resultdef:=resultdef;
  1367. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1368. include(left.flags, nf_typedaddr);
  1369. result:=left;
  1370. left:=nil;
  1371. exit;
  1372. end;
  1373. end;
  1374. end;
  1375. te_convert_l1,
  1376. te_convert_l2,
  1377. te_convert_l3 :
  1378. begin
  1379. result := simplify;
  1380. if assigned(result) then
  1381. exit;
  1382. { nothing to do }
  1383. end;
  1384. te_convert_operator :
  1385. begin
  1386. include(current_procinfo.flags,pi_do_call);
  1387. aprocdef.procsym.IncRefCountBy(1);
  1388. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1389. { tell explicitly which def we must use !! (PM) }
  1390. tcallnode(hp).procdefinition:=aprocdef;
  1391. left:=nil;
  1392. result:=hp;
  1393. exit;
  1394. end;
  1395. te_incompatible :
  1396. begin
  1397. { Procedures have a resultdef of voiddef and functions of their
  1398. own resultdef. They will therefore always be incompatible with
  1399. a procvar. Because isconvertable cannot check for procedures we
  1400. use an extra check for them.}
  1401. if (left.nodetype=calln) and
  1402. (tcallnode(left).para_count=0) and
  1403. (resultdef.typ=procvardef) and
  1404. (
  1405. (m_tp_procvar in current_settings.modeswitches) or
  1406. (m_mac_procvar in current_settings.modeswitches)
  1407. ) then
  1408. begin
  1409. if assigned(tcallnode(left).right) then
  1410. begin
  1411. { this is already a procvar, if it is really equal
  1412. is checked below }
  1413. convtype:=tc_equal;
  1414. hp:=tcallnode(left).right.getcopy;
  1415. currprocdef:=tabstractprocdef(hp.resultdef);
  1416. end
  1417. else
  1418. begin
  1419. convtype:=tc_proc_2_procvar;
  1420. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1421. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1422. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1423. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1424. begin
  1425. if assigned(tcallnode(left).methodpointer) then
  1426. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1427. else
  1428. tloadnode(hp).set_mp(load_self_node);
  1429. end;
  1430. typecheckpass(hp);
  1431. end;
  1432. left.free;
  1433. left:=hp;
  1434. { Now check if the procedure we are going to assign to
  1435. the procvar, is compatible with the procvar's type }
  1436. if not(nf_explicit in flags) and
  1437. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef))=te_incompatible) then
  1438. IncompatibleTypes(left.resultdef,resultdef);
  1439. exit;
  1440. end;
  1441. { Handle explicit type conversions }
  1442. if nf_explicit in flags then
  1443. begin
  1444. { do common tc_equal cast }
  1445. convtype:=tc_equal;
  1446. { ordinal constants can be resized to 1,2,4,8 bytes }
  1447. if (left.nodetype=ordconstn) then
  1448. begin
  1449. { Insert typeconv for ordinal to the correct size first on left, after
  1450. that the other conversion can be done }
  1451. hdef:=nil;
  1452. case longint(resultdef.size) of
  1453. 1 :
  1454. hdef:=s8inttype;
  1455. 2 :
  1456. hdef:=s16inttype;
  1457. 4 :
  1458. hdef:=s32inttype;
  1459. 8 :
  1460. hdef:=s64inttype;
  1461. end;
  1462. { we need explicit, because it can also be an enum }
  1463. if assigned(hdef) then
  1464. inserttypeconv_internal(left,hdef)
  1465. else
  1466. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1467. end;
  1468. { check if the result could be in a register }
  1469. if (not(tstoreddef(resultdef).is_intregable) and
  1470. not(tstoreddef(resultdef).is_fpuregable)) or
  1471. ((left.resultdef.typ = floatdef) and
  1472. (resultdef.typ <> floatdef)) then
  1473. make_not_regable(left,vr_addr);
  1474. { class/interface to class/interface, with checkobject support }
  1475. if is_class_or_interface(resultdef) and
  1476. is_class_or_interface(left.resultdef) then
  1477. begin
  1478. { check if the types are related }
  1479. if not(nf_internal in flags) and
  1480. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1481. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1482. begin
  1483. { Give an error when typecasting class to interface, this is compatible
  1484. with delphi }
  1485. if is_interface(resultdef) and
  1486. not is_interface(left.resultdef) then
  1487. CGMessage2(type_e_classes_not_related,
  1488. FullTypeName(left.resultdef,resultdef),
  1489. FullTypeName(resultdef,left.resultdef))
  1490. else
  1491. CGMessage2(type_w_classes_not_related,
  1492. FullTypeName(left.resultdef,resultdef),
  1493. FullTypeName(resultdef,left.resultdef))
  1494. end;
  1495. { Add runtime check? }
  1496. if (cs_check_object in current_settings.localswitches) and
  1497. not(nf_internal in flags) then
  1498. begin
  1499. { we can translate the typeconvnode to 'as' when
  1500. typecasting to a class or interface }
  1501. { we need to make sure the result can still be
  1502. passed as a var parameter }
  1503. newblock:=internalstatements(newstatement);
  1504. if (valid_for_var(left,false)) then
  1505. begin
  1506. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1507. addstatement(newstatement,tempnode);
  1508. addstatement(newstatement,cassignmentnode.create(
  1509. ctemprefnode.create(tempnode),
  1510. caddrnode.create_internal(left)));
  1511. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1512. end
  1513. else
  1514. begin
  1515. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1516. addstatement(newstatement,tempnode);
  1517. addstatement(newstatement,cassignmentnode.create(
  1518. ctemprefnode.create(tempnode),
  1519. left));
  1520. left:=ctemprefnode.create(tempnode);
  1521. end;
  1522. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1523. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1524. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1525. left:=nil;
  1526. result:=newblock;
  1527. exit;
  1528. end;
  1529. end
  1530. else
  1531. begin
  1532. { only if the same size or formal def }
  1533. if not(
  1534. (left.resultdef.typ=formaldef) or
  1535. (
  1536. not(is_open_array(left.resultdef)) and
  1537. not(is_array_constructor(left.resultdef)) and
  1538. (left.resultdef.size=resultdef.size)
  1539. ) or
  1540. (
  1541. is_void(left.resultdef) and
  1542. (left.nodetype=derefn)
  1543. )
  1544. ) then
  1545. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1546. end;
  1547. end
  1548. else
  1549. IncompatibleTypes(left.resultdef,resultdef);
  1550. end;
  1551. else
  1552. internalerror(200211231);
  1553. end;
  1554. end;
  1555. { Give hint or warning for unportable code, exceptions are
  1556. - typecasts from constants
  1557. - void }
  1558. if not(nf_internal in flags) and
  1559. (left.nodetype<>ordconstn) and
  1560. not(is_void(left.resultdef)) and
  1561. (((left.resultdef.typ=orddef) and
  1562. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1563. ((resultdef.typ=orddef) and
  1564. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1565. begin
  1566. { Give a warning when sizes don't match, because then info will be lost }
  1567. if left.resultdef.size=resultdef.size then
  1568. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1569. else
  1570. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1571. end;
  1572. result := simplify;
  1573. if assigned(result) then
  1574. exit;
  1575. { now call the resultdef helper to do constant folding }
  1576. result:=typecheck_call_helper(convtype);
  1577. end;
  1578. function ttypeconvnode.simplify: tnode;
  1579. var
  1580. hp: tnode;
  1581. begin
  1582. result := nil;
  1583. { Constant folding and other node transitions to
  1584. remove the typeconv node }
  1585. case left.nodetype of
  1586. realconstn :
  1587. begin
  1588. if (convtype = tc_real_2_currency) then
  1589. result := typecheck_real_to_currency
  1590. else if (convtype = tc_real_2_real) then
  1591. result := typecheck_real_to_real
  1592. else
  1593. exit;
  1594. if not(assigned(result)) then
  1595. begin
  1596. result := left;
  1597. left := nil;
  1598. end;
  1599. if (result.nodetype = realconstn) then
  1600. begin
  1601. hp:=result;
  1602. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  1603. if ([nf_explicit,nf_internal] * flags <> []) then
  1604. include(result.flags, nf_explicit);
  1605. hp.free;
  1606. end;
  1607. end;
  1608. niln :
  1609. begin
  1610. { nil to ordinal node }
  1611. if (resultdef.typ=orddef) then
  1612. begin
  1613. hp:=cordconstnode.create(0,resultdef,true);
  1614. if ([nf_explicit,nf_internal] * flags <> []) then
  1615. include(hp.flags, nf_explicit);
  1616. result:=hp;
  1617. exit;
  1618. end
  1619. else
  1620. { fold nil to any pointer type }
  1621. if (resultdef.typ=pointerdef) then
  1622. begin
  1623. hp:=cnilnode.create;
  1624. hp.resultdef:=resultdef;
  1625. if ([nf_explicit,nf_internal] * flags <> []) then
  1626. include(hp.flags, nf_explicit);
  1627. result:=hp;
  1628. exit;
  1629. end
  1630. else
  1631. { remove typeconv after niln, but not when the result is a
  1632. methodpointer. The typeconv of the methodpointer will then
  1633. take care of updateing size of niln to OS_64 }
  1634. if not((resultdef.typ=procvardef) and
  1635. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1636. begin
  1637. left.resultdef:=resultdef;
  1638. if ([nf_explicit,nf_internal] * flags <> []) then
  1639. include(left.flags, nf_explicit);
  1640. result:=left;
  1641. left:=nil;
  1642. exit;
  1643. end;
  1644. end;
  1645. ordconstn :
  1646. begin
  1647. { ordinal contants can be directly converted }
  1648. { but not char to char because it is a widechar to char or via versa }
  1649. { which needs extra code to do the code page transistion }
  1650. { constant ordinal to pointer }
  1651. if (resultdef.typ=pointerdef) and
  1652. (convtype<>tc_cchar_2_pchar) then
  1653. begin
  1654. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resultdef);
  1655. if ([nf_explicit,nf_internal] * flags <> []) then
  1656. include(hp.flags, nf_explicit);
  1657. result:=hp;
  1658. exit;
  1659. end
  1660. else if is_ordinal(resultdef) and
  1661. not(convtype=tc_char_2_char) then
  1662. begin
  1663. { replace the resultdef and recheck the range }
  1664. left.resultdef:=resultdef;
  1665. if ([nf_explicit,nf_internal] * flags <> []) then
  1666. include(left.flags, nf_explicit);
  1667. testrange(left.resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1668. result:=left;
  1669. left:=nil;
  1670. exit;
  1671. end;
  1672. end;
  1673. pointerconstn :
  1674. begin
  1675. { pointerconstn to any pointer is folded too }
  1676. if (resultdef.typ=pointerdef) then
  1677. begin
  1678. left.resultdef:=resultdef;
  1679. if ([nf_explicit,nf_internal] * flags <> []) then
  1680. include(left.flags, nf_explicit);
  1681. result:=left;
  1682. left:=nil;
  1683. exit;
  1684. end
  1685. { constant pointer to ordinal }
  1686. else if is_ordinal(resultdef) then
  1687. begin
  1688. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1689. resultdef,not(nf_explicit in flags));
  1690. if ([nf_explicit,nf_internal] * flags <> []) then
  1691. include(hp.flags, nf_explicit);
  1692. result:=hp;
  1693. exit;
  1694. end;
  1695. end;
  1696. end;
  1697. end;
  1698. procedure Ttypeconvnode.mark_write;
  1699. begin
  1700. left.mark_write;
  1701. end;
  1702. function ttypeconvnode.first_cord_to_pointer : tnode;
  1703. begin
  1704. result:=nil;
  1705. internalerror(200104043);
  1706. end;
  1707. function ttypeconvnode.first_int_to_int : tnode;
  1708. begin
  1709. first_int_to_int:=nil;
  1710. expectloc:=left.expectloc;
  1711. if not is_void(left.resultdef) then
  1712. begin
  1713. if (left.expectloc<>LOC_REGISTER) and
  1714. (resultdef.size>left.resultdef.size) then
  1715. expectloc:=LOC_REGISTER
  1716. else
  1717. if (left.expectloc=LOC_CREGISTER) and
  1718. (resultdef.size<left.resultdef.size) then
  1719. expectloc:=LOC_REGISTER;
  1720. end;
  1721. {$ifndef cpu64bit}
  1722. if is_64bit(resultdef) then
  1723. registersint:=max(registersint,2)
  1724. else
  1725. {$endif cpu64bit}
  1726. registersint:=max(registersint,1);
  1727. end;
  1728. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1729. begin
  1730. result:=nil;
  1731. registersint:=1;
  1732. expectloc:=LOC_REGISTER;
  1733. end;
  1734. function ttypeconvnode.first_cstring_to_int : tnode;
  1735. begin
  1736. result:=nil;
  1737. internalerror(200510014);
  1738. end;
  1739. function ttypeconvnode.first_string_to_chararray : tnode;
  1740. begin
  1741. first_string_to_chararray:=nil;
  1742. expectloc:=left.expectloc;
  1743. end;
  1744. function ttypeconvnode.first_char_to_string : tnode;
  1745. begin
  1746. first_char_to_string:=nil;
  1747. expectloc:=LOC_REFERENCE;
  1748. end;
  1749. function ttypeconvnode.first_nothing : tnode;
  1750. begin
  1751. first_nothing:=nil;
  1752. end;
  1753. function ttypeconvnode.first_array_to_pointer : tnode;
  1754. begin
  1755. first_array_to_pointer:=nil;
  1756. if registersint<1 then
  1757. registersint:=1;
  1758. expectloc:=LOC_REGISTER;
  1759. end;
  1760. function ttypeconvnode.first_int_to_real: tnode;
  1761. var
  1762. fname: string[32];
  1763. begin
  1764. if target_info.system in system_wince then
  1765. begin
  1766. { converting a 64bit integer to a float requires a helper }
  1767. if is_64bitint(left.resultdef) or
  1768. is_currency(left.resultdef) then
  1769. begin
  1770. { hack to avoid double division by 10000, as it's
  1771. already done by typecheckpass.resultdef_int_to_real }
  1772. if is_currency(left.resultdef) then
  1773. left.resultdef := s64inttype;
  1774. if is_signed(left.resultdef) then
  1775. fname:='I64TOD'
  1776. else
  1777. fname:='UI64TOD';
  1778. end
  1779. else
  1780. { other integers are supposed to be 32 bit }
  1781. begin
  1782. if is_signed(left.resultdef) then
  1783. fname:='ITOD'
  1784. else
  1785. fname:='UTOD';
  1786. firstpass(left);
  1787. end;
  1788. result:=ccallnode.createintern(fname,ccallparanode.create(
  1789. left,nil));
  1790. left:=nil;
  1791. firstpass(result);
  1792. exit;
  1793. end
  1794. else
  1795. begin
  1796. { converting a 64bit integer to a float requires a helper }
  1797. if is_64bitint(left.resultdef) or
  1798. is_currency(left.resultdef) then
  1799. begin
  1800. { hack to avoid double division by 10000, as it's
  1801. already done by typecheckpass.resultdef_int_to_real }
  1802. if is_currency(left.resultdef) then
  1803. left.resultdef := s64inttype;
  1804. if is_signed(left.resultdef) then
  1805. fname:='int64_to_'
  1806. else
  1807. { we can't do better currently }
  1808. fname:='int64_to_';
  1809. end
  1810. else
  1811. { other integers are supposed to be 32 bit }
  1812. begin
  1813. if is_signed(left.resultdef) then
  1814. fname:='int32_to_'
  1815. else
  1816. { we can't do better currently }
  1817. fname:='int32_to_';
  1818. firstpass(left);
  1819. end;
  1820. if tfloatdef(resultdef).floattype=s64real then
  1821. fname:=fname+'float64'
  1822. else
  1823. fname:=fname+'float32';
  1824. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  1825. left,nil)),resultdef);
  1826. left:=nil;
  1827. firstpass(result);
  1828. exit;
  1829. end;
  1830. end;
  1831. function ttypeconvnode.first_real_to_real : tnode;
  1832. begin
  1833. {$ifdef cpufpemu}
  1834. if cs_fp_emulation in current_settings.moduleswitches then
  1835. begin
  1836. if target_info.system in system_wince then
  1837. begin
  1838. case tfloatdef(left.resultdef).floattype of
  1839. s32real:
  1840. case tfloatdef(resultdef).floattype of
  1841. s64real:
  1842. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1843. s32real:
  1844. begin
  1845. result:=left;
  1846. left:=nil;
  1847. end;
  1848. else
  1849. internalerror(2005082704);
  1850. end;
  1851. s64real:
  1852. case tfloatdef(resultdef).floattype of
  1853. s32real:
  1854. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1855. s64real:
  1856. begin
  1857. result:=left;
  1858. left:=nil;
  1859. end;
  1860. else
  1861. internalerror(2005082703);
  1862. end;
  1863. else
  1864. internalerror(2005082702);
  1865. end;
  1866. left:=nil;
  1867. firstpass(result);
  1868. exit;
  1869. end
  1870. else
  1871. begin
  1872. case tfloatdef(left.resultdef).floattype of
  1873. s32real:
  1874. case tfloatdef(resultdef).floattype of
  1875. s64real:
  1876. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  1877. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  1878. s32real:
  1879. begin
  1880. result:=left;
  1881. left:=nil;
  1882. end;
  1883. else
  1884. internalerror(200610151);
  1885. end;
  1886. s64real:
  1887. case tfloatdef(resultdef).floattype of
  1888. s32real:
  1889. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  1890. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  1891. s64real:
  1892. begin
  1893. result:=left;
  1894. left:=nil;
  1895. end;
  1896. else
  1897. internalerror(200610152);
  1898. end;
  1899. else
  1900. internalerror(200610153);
  1901. end;
  1902. left:=nil;
  1903. firstpass(result);
  1904. exit;
  1905. end;
  1906. end
  1907. else
  1908. {$endif cpufpemu}
  1909. begin
  1910. first_real_to_real:=nil;
  1911. if registersfpu<1 then
  1912. registersfpu:=1;
  1913. expectloc:=LOC_FPUREGISTER;
  1914. end;
  1915. end;
  1916. function ttypeconvnode.first_pointer_to_array : tnode;
  1917. begin
  1918. first_pointer_to_array:=nil;
  1919. if registersint<1 then
  1920. registersint:=1;
  1921. expectloc:=LOC_REFERENCE;
  1922. end;
  1923. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1924. begin
  1925. first_cchar_to_pchar:=nil;
  1926. internalerror(200104021);
  1927. end;
  1928. function ttypeconvnode.first_bool_to_int : tnode;
  1929. begin
  1930. first_bool_to_int:=nil;
  1931. { byte(boolean) or word(wordbool) or longint(longbool) must
  1932. be accepted for var parameters }
  1933. if (nf_explicit in flags) and
  1934. (left.resultdef.size=resultdef.size) and
  1935. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1936. exit;
  1937. { when converting to 64bit, first convert to a 32bit int and then }
  1938. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1939. if resultdef.size > sizeof(aint) then
  1940. begin
  1941. result := ctypeconvnode.create_internal(left,u32inttype);
  1942. result := ctypeconvnode.create(result,resultdef);
  1943. left := nil;
  1944. firstpass(result);
  1945. exit;
  1946. end;
  1947. expectloc:=LOC_REGISTER;
  1948. if registersint<1 then
  1949. registersint:=1;
  1950. end;
  1951. function ttypeconvnode.first_int_to_bool : tnode;
  1952. begin
  1953. first_int_to_bool:=nil;
  1954. { byte(boolean) or word(wordbool) or longint(longbool) must
  1955. be accepted for var parameters }
  1956. if (nf_explicit in flags) and
  1957. (left.resultdef.size=resultdef.size) and
  1958. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1959. exit;
  1960. expectloc:=LOC_REGISTER;
  1961. { need if bool to bool !!
  1962. not very nice !!
  1963. insertypeconv(left,s32inttype);
  1964. left.explizit:=true;
  1965. firstpass(left); }
  1966. if registersint<1 then
  1967. registersint:=1;
  1968. end;
  1969. function ttypeconvnode.first_bool_to_bool : tnode;
  1970. begin
  1971. first_bool_to_bool:=nil;
  1972. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  1973. expectloc := left.expectloc
  1974. else
  1975. begin
  1976. expectloc:=LOC_REGISTER;
  1977. if registersint<1 then
  1978. registersint:=1;
  1979. end;
  1980. end;
  1981. function ttypeconvnode.first_char_to_char : tnode;
  1982. begin
  1983. first_char_to_char:=first_int_to_int;
  1984. end;
  1985. function ttypeconvnode.first_proc_to_procvar : tnode;
  1986. begin
  1987. first_proc_to_procvar:=nil;
  1988. { if we take the address of a nested function, it'll }
  1989. { probably be used in a foreach() construct and then }
  1990. { the parent needs a stackframe }
  1991. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  1992. include(current_procinfo.flags,pi_needs_stackframe);
  1993. if tabstractprocdef(resultdef).is_addressonly then
  1994. begin
  1995. registersint:=left.registersint;
  1996. if registersint<1 then
  1997. registersint:=1;
  1998. expectloc:=LOC_REGISTER;
  1999. end
  2000. else
  2001. begin
  2002. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2003. CGMessage(parser_e_illegal_expression);
  2004. registersint:=left.registersint;
  2005. expectloc:=left.expectloc
  2006. end
  2007. end;
  2008. function ttypeconvnode.first_load_smallset : tnode;
  2009. var
  2010. srsym: ttypesym;
  2011. p: tcallparanode;
  2012. begin
  2013. srsym:=search_system_type('FPC_SMALL_SET');
  2014. p := ccallparanode.create(left,nil);
  2015. { reused }
  2016. left := nil;
  2017. { convert parameter explicitely to fpc_small_set }
  2018. p.left := ctypeconvnode.create_internal(p.left,srsym.typedef);
  2019. { create call, adjust resultdef }
  2020. result :=
  2021. ccallnode.createinternres('fpc_set_load_small',p,resultdef);
  2022. firstpass(result);
  2023. end;
  2024. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2025. begin
  2026. first_ansistring_to_pchar:=nil;
  2027. expectloc:=LOC_REGISTER;
  2028. if registersint<1 then
  2029. registersint:=1;
  2030. end;
  2031. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2032. begin
  2033. first_arrayconstructor_to_set:=nil;
  2034. internalerror(200104022);
  2035. end;
  2036. function ttypeconvnode.first_class_to_intf : tnode;
  2037. begin
  2038. first_class_to_intf:=nil;
  2039. expectloc:=LOC_REGISTER;
  2040. if registersint<1 then
  2041. registersint:=1;
  2042. end;
  2043. function ttypeconvnode._first_int_to_int : tnode;
  2044. begin
  2045. result:=first_int_to_int;
  2046. end;
  2047. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2048. begin
  2049. result:=first_cstring_to_pchar;
  2050. end;
  2051. function ttypeconvnode._first_cstring_to_int : tnode;
  2052. begin
  2053. result:=first_cstring_to_int;
  2054. end;
  2055. function ttypeconvnode._first_string_to_chararray : tnode;
  2056. begin
  2057. result:=first_string_to_chararray;
  2058. end;
  2059. function ttypeconvnode._first_char_to_string : tnode;
  2060. begin
  2061. result:=first_char_to_string;
  2062. end;
  2063. function ttypeconvnode._first_nothing : tnode;
  2064. begin
  2065. result:=first_nothing;
  2066. end;
  2067. function ttypeconvnode._first_array_to_pointer : tnode;
  2068. begin
  2069. result:=first_array_to_pointer;
  2070. end;
  2071. function ttypeconvnode._first_int_to_real : tnode;
  2072. begin
  2073. result:=first_int_to_real;
  2074. end;
  2075. function ttypeconvnode._first_real_to_real : tnode;
  2076. begin
  2077. result:=first_real_to_real;
  2078. end;
  2079. function ttypeconvnode._first_pointer_to_array : tnode;
  2080. begin
  2081. result:=first_pointer_to_array;
  2082. end;
  2083. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2084. begin
  2085. result:=first_cchar_to_pchar;
  2086. end;
  2087. function ttypeconvnode._first_bool_to_int : tnode;
  2088. begin
  2089. result:=first_bool_to_int;
  2090. end;
  2091. function ttypeconvnode._first_int_to_bool : tnode;
  2092. begin
  2093. result:=first_int_to_bool;
  2094. end;
  2095. function ttypeconvnode._first_bool_to_bool : tnode;
  2096. begin
  2097. result:=first_bool_to_bool;
  2098. end;
  2099. function ttypeconvnode._first_proc_to_procvar : tnode;
  2100. begin
  2101. result:=first_proc_to_procvar;
  2102. end;
  2103. function ttypeconvnode._first_load_smallset : tnode;
  2104. begin
  2105. result:=first_load_smallset;
  2106. end;
  2107. function ttypeconvnode._first_cord_to_pointer : tnode;
  2108. begin
  2109. result:=first_cord_to_pointer;
  2110. end;
  2111. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2112. begin
  2113. result:=first_ansistring_to_pchar;
  2114. end;
  2115. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2116. begin
  2117. result:=first_arrayconstructor_to_set;
  2118. end;
  2119. function ttypeconvnode._first_class_to_intf : tnode;
  2120. begin
  2121. result:=first_class_to_intf;
  2122. end;
  2123. function ttypeconvnode._first_char_to_char : tnode;
  2124. begin
  2125. result:=first_char_to_char;
  2126. end;
  2127. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2128. const
  2129. firstconvert : array[tconverttype] of pointer = (
  2130. nil, { none }
  2131. @ttypeconvnode._first_nothing, {equal}
  2132. @ttypeconvnode._first_nothing, {not_possible}
  2133. nil, { removed in typecheck_string_to_string }
  2134. @ttypeconvnode._first_char_to_string,
  2135. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2136. nil, { removed in typecheck_chararray_to_string }
  2137. @ttypeconvnode._first_cchar_to_pchar,
  2138. @ttypeconvnode._first_cstring_to_pchar,
  2139. @ttypeconvnode._first_cstring_to_int,
  2140. @ttypeconvnode._first_ansistring_to_pchar,
  2141. @ttypeconvnode._first_string_to_chararray,
  2142. nil, { removed in typecheck_chararray_to_string }
  2143. @ttypeconvnode._first_array_to_pointer,
  2144. @ttypeconvnode._first_pointer_to_array,
  2145. @ttypeconvnode._first_int_to_int,
  2146. @ttypeconvnode._first_int_to_bool,
  2147. @ttypeconvnode._first_bool_to_bool,
  2148. @ttypeconvnode._first_bool_to_int,
  2149. @ttypeconvnode._first_real_to_real,
  2150. @ttypeconvnode._first_int_to_real,
  2151. nil, { removed in typecheck_real_to_currency }
  2152. @ttypeconvnode._first_proc_to_procvar,
  2153. @ttypeconvnode._first_arrayconstructor_to_set,
  2154. @ttypeconvnode._first_load_smallset,
  2155. @ttypeconvnode._first_cord_to_pointer,
  2156. @ttypeconvnode._first_nothing,
  2157. @ttypeconvnode._first_nothing,
  2158. @ttypeconvnode._first_class_to_intf,
  2159. @ttypeconvnode._first_char_to_char,
  2160. @ttypeconvnode._first_nothing,
  2161. @ttypeconvnode._first_nothing,
  2162. nil,
  2163. nil,
  2164. nil,
  2165. nil,
  2166. nil,
  2167. nil,
  2168. nil,
  2169. nil
  2170. );
  2171. type
  2172. tprocedureofobject = function : tnode of object;
  2173. var
  2174. r : packed record
  2175. proc : pointer;
  2176. obj : pointer;
  2177. end;
  2178. begin
  2179. { this is a little bit dirty but it works }
  2180. { and should be quite portable too }
  2181. r.proc:=firstconvert[c];
  2182. r.obj:=self;
  2183. if not assigned(r.proc) then
  2184. internalerror(200312081);
  2185. first_call_helper:=tprocedureofobject(r)()
  2186. end;
  2187. function ttypeconvnode.pass_1 : tnode;
  2188. begin
  2189. result:=nil;
  2190. firstpass(left);
  2191. if codegenerror then
  2192. exit;
  2193. { load the value_str from the left part }
  2194. registersint:=left.registersint;
  2195. registersfpu:=left.registersfpu;
  2196. {$ifdef SUPPORT_MMX}
  2197. registersmmx:=left.registersmmx;
  2198. {$endif}
  2199. expectloc:=left.expectloc;
  2200. result:=first_call_helper(convtype);
  2201. end;
  2202. function ttypeconvnode.assign_allowed:boolean;
  2203. begin
  2204. result:=(convtype=tc_equal) or
  2205. { typecasting from void is always allowed }
  2206. is_void(left.resultdef) or
  2207. (left.resultdef.typ=formaldef) or
  2208. { int 2 int with same size reuses same location, or for
  2209. tp7 mode also allow size < orignal size }
  2210. (
  2211. (convtype=tc_int_2_int) and
  2212. (
  2213. (resultdef.size=left.resultdef.size) or
  2214. ((m_tp7 in current_settings.modeswitches) and
  2215. (resultdef.size<left.resultdef.size))
  2216. )
  2217. ) or
  2218. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2219. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  2220. (nf_explicit in flags) and
  2221. (resultdef.size=left.resultdef.size));
  2222. { When using only a part of the value it can't be in a register since
  2223. that will load the value in a new register first }
  2224. if (resultdef.size<left.resultdef.size) then
  2225. make_not_regable(left,vr_addr);
  2226. end;
  2227. function ttypeconvnode.docompare(p: tnode) : boolean;
  2228. begin
  2229. docompare :=
  2230. inherited docompare(p) and
  2231. (convtype = ttypeconvnode(p).convtype);
  2232. end;
  2233. procedure ttypeconvnode._second_int_to_int;
  2234. begin
  2235. second_int_to_int;
  2236. end;
  2237. procedure ttypeconvnode._second_string_to_string;
  2238. begin
  2239. second_string_to_string;
  2240. end;
  2241. procedure ttypeconvnode._second_cstring_to_pchar;
  2242. begin
  2243. second_cstring_to_pchar;
  2244. end;
  2245. procedure ttypeconvnode._second_cstring_to_int;
  2246. begin
  2247. second_cstring_to_int;
  2248. end;
  2249. procedure ttypeconvnode._second_string_to_chararray;
  2250. begin
  2251. second_string_to_chararray;
  2252. end;
  2253. procedure ttypeconvnode._second_array_to_pointer;
  2254. begin
  2255. second_array_to_pointer;
  2256. end;
  2257. procedure ttypeconvnode._second_pointer_to_array;
  2258. begin
  2259. second_pointer_to_array;
  2260. end;
  2261. procedure ttypeconvnode._second_chararray_to_string;
  2262. begin
  2263. second_chararray_to_string;
  2264. end;
  2265. procedure ttypeconvnode._second_char_to_string;
  2266. begin
  2267. second_char_to_string;
  2268. end;
  2269. procedure ttypeconvnode._second_int_to_real;
  2270. begin
  2271. second_int_to_real;
  2272. end;
  2273. procedure ttypeconvnode._second_real_to_real;
  2274. begin
  2275. second_real_to_real;
  2276. end;
  2277. procedure ttypeconvnode._second_cord_to_pointer;
  2278. begin
  2279. second_cord_to_pointer;
  2280. end;
  2281. procedure ttypeconvnode._second_proc_to_procvar;
  2282. begin
  2283. second_proc_to_procvar;
  2284. end;
  2285. procedure ttypeconvnode._second_bool_to_int;
  2286. begin
  2287. second_bool_to_int;
  2288. end;
  2289. procedure ttypeconvnode._second_int_to_bool;
  2290. begin
  2291. second_int_to_bool;
  2292. end;
  2293. procedure ttypeconvnode._second_bool_to_bool;
  2294. begin
  2295. second_bool_to_bool;
  2296. end;
  2297. procedure ttypeconvnode._second_load_smallset;
  2298. begin
  2299. second_load_smallset;
  2300. end;
  2301. procedure ttypeconvnode._second_ansistring_to_pchar;
  2302. begin
  2303. second_ansistring_to_pchar;
  2304. end;
  2305. procedure ttypeconvnode._second_class_to_intf;
  2306. begin
  2307. second_class_to_intf;
  2308. end;
  2309. procedure ttypeconvnode._second_char_to_char;
  2310. begin
  2311. second_char_to_char;
  2312. end;
  2313. procedure ttypeconvnode._second_nothing;
  2314. begin
  2315. second_nothing;
  2316. end;
  2317. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2318. const
  2319. secondconvert : array[tconverttype] of pointer = (
  2320. @ttypeconvnode._second_nothing, {none}
  2321. @ttypeconvnode._second_nothing, {equal}
  2322. @ttypeconvnode._second_nothing, {not_possible}
  2323. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2324. @ttypeconvnode._second_char_to_string,
  2325. @ttypeconvnode._second_nothing, {char_to_charray}
  2326. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2327. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2328. @ttypeconvnode._second_cstring_to_pchar,
  2329. @ttypeconvnode._second_cstring_to_int,
  2330. @ttypeconvnode._second_ansistring_to_pchar,
  2331. @ttypeconvnode._second_string_to_chararray,
  2332. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2333. @ttypeconvnode._second_array_to_pointer,
  2334. @ttypeconvnode._second_pointer_to_array,
  2335. @ttypeconvnode._second_int_to_int,
  2336. @ttypeconvnode._second_int_to_bool,
  2337. @ttypeconvnode._second_bool_to_bool,
  2338. @ttypeconvnode._second_bool_to_int,
  2339. @ttypeconvnode._second_real_to_real,
  2340. @ttypeconvnode._second_int_to_real,
  2341. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2342. @ttypeconvnode._second_proc_to_procvar,
  2343. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2344. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2345. @ttypeconvnode._second_cord_to_pointer,
  2346. @ttypeconvnode._second_nothing, { interface 2 string }
  2347. @ttypeconvnode._second_nothing, { interface 2 guid }
  2348. @ttypeconvnode._second_class_to_intf,
  2349. @ttypeconvnode._second_char_to_char,
  2350. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2351. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2352. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2353. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2354. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2355. @ttypeconvnode._second_nothing, { variant_2_enum }
  2356. @ttypeconvnode._second_nothing, { enum_2_variant }
  2357. @ttypeconvnode._second_nothing, { variant_2_interface }
  2358. @ttypeconvnode._second_nothing, { interface_2_variant }
  2359. @ttypeconvnode._second_nothing { array_2_dynarray }
  2360. );
  2361. type
  2362. tprocedureofobject = procedure of object;
  2363. var
  2364. r : packed record
  2365. proc : pointer;
  2366. obj : pointer;
  2367. end;
  2368. begin
  2369. { this is a little bit dirty but it works }
  2370. { and should be quite portable too }
  2371. r.proc:=secondconvert[c];
  2372. r.obj:=self;
  2373. tprocedureofobject(r)();
  2374. end;
  2375. {*****************************************************************************
  2376. TISNODE
  2377. *****************************************************************************}
  2378. constructor tisnode.create(l,r : tnode);
  2379. begin
  2380. inherited create(isn,l,r);
  2381. end;
  2382. function tisnode.pass_typecheck:tnode;
  2383. var
  2384. paras: tcallparanode;
  2385. begin
  2386. result:=nil;
  2387. typecheckpass(left);
  2388. typecheckpass(right);
  2389. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2390. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2391. if codegenerror then
  2392. exit;
  2393. if (right.resultdef.typ=classrefdef) then
  2394. begin
  2395. { left must be a class }
  2396. if is_class(left.resultdef) then
  2397. begin
  2398. { the operands must be related }
  2399. if (not(tobjectdef(left.resultdef).is_related(
  2400. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2401. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2402. tobjectdef(left.resultdef)))) then
  2403. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2404. tclassrefdef(right.resultdef).pointeddef.typename);
  2405. end
  2406. else
  2407. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2408. { call fpc_do_is helper }
  2409. paras := ccallparanode.create(
  2410. left,
  2411. ccallparanode.create(
  2412. right,nil));
  2413. result := ccallnode.createintern('fpc_do_is',paras);
  2414. left := nil;
  2415. right := nil;
  2416. end
  2417. else if is_interface(right.resultdef) then
  2418. begin
  2419. { left is a class }
  2420. if is_class(left.resultdef) then
  2421. begin
  2422. { the class must implement the interface }
  2423. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2424. CGMessage2(type_e_classes_not_related,
  2425. FullTypeName(left.resultdef,right.resultdef),
  2426. FullTypeName(right.resultdef,left.resultdef))
  2427. end
  2428. { left is an interface }
  2429. else if is_interface(left.resultdef) then
  2430. begin
  2431. { the operands must be related }
  2432. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2433. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2434. CGMessage2(type_e_classes_not_related,
  2435. FullTypeName(left.resultdef,right.resultdef),
  2436. FullTypeName(right.resultdef,left.resultdef));
  2437. end
  2438. else
  2439. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2440. { call fpc_do_is helper }
  2441. paras := ccallparanode.create(
  2442. left,
  2443. ccallparanode.create(
  2444. right,nil));
  2445. result := ccallnode.createintern('fpc_do_is',paras);
  2446. left := nil;
  2447. right := nil;
  2448. end
  2449. else
  2450. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2451. resultdef:=booltype;
  2452. end;
  2453. function tisnode.pass_1 : tnode;
  2454. begin
  2455. internalerror(200204254);
  2456. result:=nil;
  2457. end;
  2458. { dummy pass_2, it will never be called, but we need one since }
  2459. { you can't instantiate an abstract class }
  2460. procedure tisnode.pass_generate_code;
  2461. begin
  2462. end;
  2463. {*****************************************************************************
  2464. TASNODE
  2465. *****************************************************************************}
  2466. constructor tasnode.create(l,r : tnode);
  2467. begin
  2468. inherited create(asn,l,r);
  2469. call := nil;
  2470. end;
  2471. destructor tasnode.destroy;
  2472. begin
  2473. call.free;
  2474. inherited destroy;
  2475. end;
  2476. function tasnode.pass_typecheck:tnode;
  2477. var
  2478. hp : tnode;
  2479. begin
  2480. result:=nil;
  2481. typecheckpass(right);
  2482. typecheckpass(left);
  2483. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2484. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2485. if codegenerror then
  2486. exit;
  2487. if (right.resultdef.typ=classrefdef) then
  2488. begin
  2489. { left must be a class }
  2490. if is_class(left.resultdef) then
  2491. begin
  2492. { the operands must be related }
  2493. if (not(tobjectdef(left.resultdef).is_related(
  2494. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2495. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2496. tobjectdef(left.resultdef)))) then
  2497. CGMessage2(type_e_classes_not_related,
  2498. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2499. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2500. end
  2501. else
  2502. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2503. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2504. end
  2505. else if is_interface(right.resultdef) then
  2506. begin
  2507. { left is a class }
  2508. if not(is_class(left.resultdef) or
  2509. is_interfacecom(left.resultdef)) then
  2510. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2511. resultdef:=right.resultdef;
  2512. { load the GUID of the interface }
  2513. if (right.nodetype=typen) then
  2514. begin
  2515. if assigned(tobjectdef(right.resultdef).iidguid) then
  2516. begin
  2517. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  2518. right.free;
  2519. right:=hp;
  2520. end
  2521. else
  2522. internalerror(200206282);
  2523. typecheckpass(right);
  2524. end;
  2525. end
  2526. else
  2527. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2528. end;
  2529. function tasnode.dogetcopy: tnode;
  2530. begin
  2531. result := inherited dogetcopy;
  2532. if assigned(call) then
  2533. tasnode(result).call := call.getcopy
  2534. else
  2535. tasnode(result).call := nil;
  2536. end;
  2537. function tasnode.pass_1 : tnode;
  2538. var
  2539. procname: string;
  2540. begin
  2541. result:=nil;
  2542. if not assigned(call) then
  2543. begin
  2544. if is_class(left.resultdef) and
  2545. (right.resultdef.typ=classrefdef) then
  2546. call := ccallnode.createinternres('fpc_do_as',
  2547. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2548. resultdef)
  2549. else
  2550. begin
  2551. if is_class(left.resultdef) then
  2552. procname := 'fpc_class_as_intf'
  2553. else
  2554. procname := 'fpc_intf_as';
  2555. call := ccallnode.createintern(procname,
  2556. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2557. call := ctypeconvnode.create_internal(call,resultdef);
  2558. end;
  2559. left := nil;
  2560. right := nil;
  2561. firstpass(call);
  2562. if codegenerror then
  2563. exit;
  2564. expectloc:=call.expectloc;
  2565. registersint:=call.registersint;
  2566. registersfpu:=call.registersfpu;
  2567. {$ifdef SUPPORT_MMX}
  2568. registersmmx:=call.registersmmx;
  2569. {$endif SUPPORT_MMX}
  2570. end;
  2571. end;
  2572. begin
  2573. ctypeconvnode:=ttypeconvnode;
  2574. casnode:=tasnode;
  2575. cisnode:=tisnode;
  2576. end.