ncnv.pas 105 KB

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