ncnv.pas 106 KB

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