ncnv.pas 108 KB

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