ncnv.pas 111 KB

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