ncnv.pas 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,
  24. defutil,defcmp,
  25. nld
  26. ;
  27. type
  28. ttypeconvnode = class(tunarynode)
  29. totype : ttype;
  30. convtype : tconverttype;
  31. constructor create(node : tnode;const t : ttype);virtual;
  32. constructor create_explicit(node : tnode;const t : ttype);
  33. constructor create_internal(node : tnode;const t : ttype);
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure buildderefimpl;override;
  37. procedure derefimpl;override;
  38. function getcopy : tnode;override;
  39. function pass_1 : tnode;override;
  40. function det_resulttype:tnode;override;
  41. procedure mark_write;override;
  42. function docompare(p: tnode) : boolean; override;
  43. function assign_allowed:boolean;
  44. procedure second_call_helper(c : tconverttype);
  45. private
  46. function resulttype_int_to_int : tnode;
  47. function resulttype_cord_to_pointer : tnode;
  48. function resulttype_chararray_to_string : tnode;
  49. function resulttype_string_to_chararray : tnode;
  50. function resulttype_string_to_string : tnode;
  51. function resulttype_char_to_string : tnode;
  52. function resulttype_char_to_chararray : tnode;
  53. function resulttype_int_to_real : tnode;
  54. function resulttype_real_to_real : tnode;
  55. function resulttype_real_to_currency : tnode;
  56. function resulttype_cchar_to_pchar : tnode;
  57. function resulttype_cstring_to_pchar : tnode;
  58. function resulttype_char_to_char : tnode;
  59. function resulttype_arrayconstructor_to_set : tnode;
  60. function resulttype_pchar_to_string : tnode;
  61. function resulttype_interface_to_guid : tnode;
  62. function resulttype_dynarray_to_openarray : tnode;
  63. function resulttype_pwchar_to_string : tnode;
  64. function resulttype_variant_to_dynarray : tnode;
  65. function resulttype_dynarray_to_variant : tnode;
  66. function resulttype_call_helper(c : tconverttype) : tnode;
  67. function resulttype_variant_to_enum : tnode;
  68. function resulttype_enum_to_variant : tnode;
  69. protected
  70. function first_int_to_int : tnode;virtual;
  71. function first_cstring_to_pchar : tnode;virtual;
  72. function first_string_to_chararray : tnode;virtual;
  73. function first_char_to_string : tnode;virtual;
  74. function first_nothing : tnode;virtual;
  75. function first_array_to_pointer : tnode;virtual;
  76. function first_int_to_real : tnode;virtual;
  77. function first_real_to_real : tnode;virtual;
  78. function first_pointer_to_array : tnode;virtual;
  79. function first_cchar_to_pchar : tnode;virtual;
  80. function first_bool_to_int : tnode;virtual;
  81. function first_int_to_bool : tnode;virtual;
  82. function first_bool_to_bool : tnode;virtual;
  83. function first_proc_to_procvar : tnode;virtual;
  84. function first_load_smallset : tnode;virtual;
  85. function first_cord_to_pointer : tnode;virtual;
  86. function first_ansistring_to_pchar : tnode;virtual;
  87. function first_arrayconstructor_to_set : tnode;virtual;
  88. function first_class_to_intf : tnode;virtual;
  89. function first_char_to_char : tnode;virtual;
  90. function first_call_helper(c : tconverttype) : tnode;
  91. { these wrapper are necessary, because the first_* stuff is called }
  92. { through a table. Without the wrappers override wouldn't have }
  93. { any effect }
  94. function _first_int_to_int : tnode;
  95. function _first_cstring_to_pchar : tnode;
  96. function _first_string_to_chararray : tnode;
  97. function _first_char_to_string : tnode;
  98. function _first_nothing : tnode;
  99. function _first_array_to_pointer : tnode;
  100. function _first_int_to_real : tnode;
  101. function _first_real_to_real: tnode;
  102. function _first_pointer_to_array : tnode;
  103. function _first_cchar_to_pchar : tnode;
  104. function _first_bool_to_int : tnode;
  105. function _first_int_to_bool : tnode;
  106. function _first_bool_to_bool : tnode;
  107. function _first_proc_to_procvar : tnode;
  108. function _first_load_smallset : tnode;
  109. function _first_cord_to_pointer : tnode;
  110. function _first_ansistring_to_pchar : tnode;
  111. function _first_arrayconstructor_to_set : tnode;
  112. function _first_class_to_intf : tnode;
  113. function _first_char_to_char : tnode;
  114. procedure _second_int_to_int;virtual;
  115. procedure _second_string_to_string;virtual;
  116. procedure _second_cstring_to_pchar;virtual;
  117. procedure _second_string_to_chararray;virtual;
  118. procedure _second_array_to_pointer;virtual;
  119. procedure _second_pointer_to_array;virtual;
  120. procedure _second_chararray_to_string;virtual;
  121. procedure _second_char_to_string;virtual;
  122. procedure _second_int_to_real;virtual;
  123. procedure _second_real_to_real;virtual;
  124. procedure _second_cord_to_pointer;virtual;
  125. procedure _second_proc_to_procvar;virtual;
  126. procedure _second_bool_to_int;virtual;
  127. procedure _second_int_to_bool;virtual;
  128. procedure _second_bool_to_bool;virtual;
  129. procedure _second_load_smallset;virtual;
  130. procedure _second_ansistring_to_pchar;virtual;
  131. procedure _second_class_to_intf;virtual;
  132. procedure _second_char_to_char;virtual;
  133. procedure _second_nothing; virtual;
  134. procedure second_int_to_int;virtual;abstract;
  135. procedure second_string_to_string;virtual;abstract;
  136. procedure second_cstring_to_pchar;virtual;abstract;
  137. procedure second_string_to_chararray;virtual;abstract;
  138. procedure second_array_to_pointer;virtual;abstract;
  139. procedure second_pointer_to_array;virtual;abstract;
  140. procedure second_chararray_to_string;virtual;abstract;
  141. procedure second_char_to_string;virtual;abstract;
  142. procedure second_int_to_real;virtual;abstract;
  143. procedure second_real_to_real;virtual;abstract;
  144. procedure second_cord_to_pointer;virtual;abstract;
  145. procedure second_proc_to_procvar;virtual;abstract;
  146. procedure second_bool_to_int;virtual;abstract;
  147. procedure second_int_to_bool;virtual;abstract;
  148. procedure second_bool_to_bool;virtual;abstract;
  149. procedure second_load_smallset;virtual;abstract;
  150. procedure second_ansistring_to_pchar;virtual;abstract;
  151. procedure second_class_to_intf;virtual;abstract;
  152. procedure second_char_to_char;virtual;abstract;
  153. procedure second_nothing; virtual;abstract;
  154. end;
  155. ttypeconvnodeclass = class of ttypeconvnode;
  156. tasnode = class(tbinarynode)
  157. constructor create(l,r : tnode);virtual;
  158. function pass_1 : tnode;override;
  159. function det_resulttype:tnode;override;
  160. function getcopy: tnode;override;
  161. destructor destroy; override;
  162. protected
  163. call: tnode;
  164. end;
  165. tasnodeclass = class of tasnode;
  166. tisnode = class(tbinarynode)
  167. constructor create(l,r : tnode);virtual;
  168. function pass_1 : tnode;override;
  169. function det_resulttype:tnode;override;
  170. procedure pass_2;override;
  171. end;
  172. tisnodeclass = class of tisnode;
  173. var
  174. ctypeconvnode : ttypeconvnodeclass;
  175. casnode : tasnodeclass;
  176. cisnode : tisnodeclass;
  177. procedure inserttypeconv(var p:tnode;const t:ttype);
  178. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  179. procedure arrayconstructor_to_set(var p : tnode);
  180. implementation
  181. uses
  182. globtype,systems,
  183. cutils,verbose,globals,widestr,
  184. symconst,symdef,symsym,symtable,
  185. ncon,ncal,nset,nadd,ninl,nmem,nmat,nutils,
  186. cgbase,procinfo,
  187. htypechk,pass_1,cpuinfo;
  188. {*****************************************************************************
  189. Helpers
  190. *****************************************************************************}
  191. procedure inserttypeconv(var p:tnode;const t:ttype);
  192. begin
  193. if not assigned(p.resulttype.def) then
  194. begin
  195. resulttypepass(p);
  196. if codegenerror then
  197. exit;
  198. end;
  199. { don't insert obsolete type conversions }
  200. if equal_defs(p.resulttype.def,t.def) and
  201. not ((p.resulttype.def.deftype=setdef) and
  202. (tsetdef(p.resulttype.def).settype <>
  203. tsetdef(t.def).settype)) then
  204. begin
  205. p.resulttype:=t;
  206. end
  207. else
  208. begin
  209. p:=ctypeconvnode.create(p,t);
  210. resulttypepass(p);
  211. end;
  212. end;
  213. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  214. begin
  215. if not assigned(p.resulttype.def) then
  216. begin
  217. resulttypepass(p);
  218. if codegenerror then
  219. exit;
  220. end;
  221. { don't insert obsolete type conversions }
  222. if equal_defs(p.resulttype.def,t.def) and
  223. not ((p.resulttype.def.deftype=setdef) and
  224. (tsetdef(p.resulttype.def).settype <>
  225. tsetdef(t.def).settype)) then
  226. begin
  227. p.resulttype:=t;
  228. end
  229. else
  230. begin
  231. p:=ctypeconvnode.create_internal(p,t);
  232. resulttypepass(p);
  233. end;
  234. end;
  235. {*****************************************************************************
  236. Array constructor to Set Conversion
  237. *****************************************************************************}
  238. procedure arrayconstructor_to_set(var p : tnode);
  239. var
  240. constp : tsetconstnode;
  241. buildp,
  242. p2,p3,p4 : tnode;
  243. htype : ttype;
  244. constset : Pconstset;
  245. constsetlo,
  246. constsethi : TConstExprInt;
  247. procedure update_constsethi(t:ttype);
  248. begin
  249. if ((t.def.deftype=orddef) and
  250. (torddef(t.def).high>=constsethi)) then
  251. begin
  252. if torddef(t.def).typ=uwidechar then
  253. begin
  254. constsethi:=255;
  255. if htype.def=nil then
  256. htype:=t;
  257. end
  258. else
  259. begin
  260. constsethi:=torddef(t.def).high;
  261. if htype.def=nil then
  262. begin
  263. if (constsethi>255) or
  264. (torddef(t.def).low<0) then
  265. htype:=u8inttype
  266. else
  267. htype:=t;
  268. end;
  269. if constsethi>255 then
  270. constsethi:=255;
  271. end;
  272. end
  273. else if ((t.def.deftype=enumdef) and
  274. (tenumdef(t.def).max>=constsethi)) then
  275. begin
  276. if htype.def=nil then
  277. htype:=t;
  278. constsethi:=tenumdef(t.def).max;
  279. end;
  280. end;
  281. procedure do_set(pos : longint);
  282. begin
  283. if (pos and not $ff)<>0 then
  284. Message(parser_e_illegal_set_expr);
  285. if pos>constsethi then
  286. constsethi:=pos;
  287. if pos<constsetlo then
  288. constsetlo:=pos;
  289. if pos in constset^ then
  290. Message(parser_e_illegal_set_expr);
  291. include(constset^,pos);
  292. end;
  293. var
  294. l : Longint;
  295. lr,hr : TConstExprInt;
  296. hp : tarrayconstructornode;
  297. begin
  298. if p.nodetype<>arrayconstructorn then
  299. internalerror(200205105);
  300. new(constset);
  301. constset^:=[];
  302. htype.reset;
  303. constsetlo:=0;
  304. constsethi:=0;
  305. constp:=csetconstnode.create(nil,htype);
  306. constp.value_set:=constset;
  307. buildp:=constp;
  308. hp:=tarrayconstructornode(p);
  309. if assigned(hp.left) then
  310. begin
  311. while assigned(hp) do
  312. begin
  313. p4:=nil; { will contain the tree to create the set }
  314. {split a range into p2 and p3 }
  315. if hp.left.nodetype=arrayconstructorrangen then
  316. begin
  317. p2:=tarrayconstructorrangenode(hp.left).left;
  318. p3:=tarrayconstructorrangenode(hp.left).right;
  319. tarrayconstructorrangenode(hp.left).left:=nil;
  320. tarrayconstructorrangenode(hp.left).right:=nil;
  321. end
  322. else
  323. begin
  324. p2:=hp.left;
  325. hp.left:=nil;
  326. p3:=nil;
  327. end;
  328. resulttypepass(p2);
  329. if assigned(p3) then
  330. resulttypepass(p3);
  331. if codegenerror then
  332. break;
  333. case p2.resulttype.def.deftype of
  334. enumdef,
  335. orddef:
  336. begin
  337. getrange(p2.resulttype.def,lr,hr);
  338. if assigned(p3) then
  339. begin
  340. { this isn't good, you'll get problems with
  341. type t010 = 0..10;
  342. ts = set of t010;
  343. var s : ts;b : t010
  344. begin s:=[1,2,b]; end.
  345. if is_integer(p3^.resulttype.def) then
  346. begin
  347. inserttypeconv(p3,u8bitdef);
  348. end;
  349. }
  350. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  351. begin
  352. aktfilepos:=p3.fileinfo;
  353. CGMessage(type_e_typeconflict_in_set);
  354. end
  355. else
  356. begin
  357. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  358. begin
  359. if not(is_integer(p3.resulttype.def)) then
  360. htype:=p3.resulttype
  361. else
  362. begin
  363. inserttypeconv(p3,u8inttype);
  364. inserttypeconv(p2,u8inttype);
  365. end;
  366. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  367. do_set(l);
  368. p2.free;
  369. p3.free;
  370. end
  371. else
  372. begin
  373. update_constsethi(p2.resulttype);
  374. inserttypeconv(p2,htype);
  375. update_constsethi(p3.resulttype);
  376. inserttypeconv(p3,htype);
  377. if assigned(htype.def) then
  378. inserttypeconv(p3,htype)
  379. else
  380. inserttypeconv(p3,u8inttype);
  381. p4:=csetelementnode.create(p2,p3);
  382. end;
  383. end;
  384. end
  385. else
  386. begin
  387. { Single value }
  388. if p2.nodetype=ordconstn then
  389. begin
  390. if not(is_integer(p2.resulttype.def)) then
  391. update_constsethi(p2.resulttype)
  392. else
  393. inserttypeconv(p2,u8inttype);
  394. do_set(tordconstnode(p2).value);
  395. p2.free;
  396. end
  397. else
  398. begin
  399. update_constsethi(p2.resulttype);
  400. if assigned(htype.def) then
  401. inserttypeconv(p2,htype)
  402. else
  403. inserttypeconv(p2,u8inttype);
  404. p4:=csetelementnode.create(p2,nil);
  405. end;
  406. end;
  407. end;
  408. stringdef :
  409. begin
  410. { if we've already set elements which are constants }
  411. { throw an error }
  412. if ((htype.def=nil) and assigned(buildp)) or
  413. not(is_char(htype.def)) then
  414. CGMessage(type_e_typeconflict_in_set)
  415. else
  416. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  417. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  418. if htype.def=nil then
  419. htype:=cchartype;
  420. p2.free;
  421. end;
  422. else
  423. CGMessage(type_e_ordinal_expr_expected);
  424. end;
  425. { insert the set creation tree }
  426. if assigned(p4) then
  427. buildp:=caddnode.create(addn,buildp,p4);
  428. { load next and dispose current node }
  429. p2:=hp;
  430. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  431. tarrayconstructornode(p2).right:=nil;
  432. p2.free;
  433. end;
  434. if (htype.def=nil) then
  435. htype:=u8inttype;
  436. end
  437. else
  438. begin
  439. { empty set [], only remove node }
  440. p.free;
  441. end;
  442. { set the initial set type }
  443. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  444. { determine the resulttype for the tree }
  445. resulttypepass(buildp);
  446. { set the new tree }
  447. p:=buildp;
  448. end;
  449. {*****************************************************************************
  450. TTYPECONVNODE
  451. *****************************************************************************}
  452. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  453. begin
  454. inherited create(typeconvn,node);
  455. convtype:=tc_not_possible;
  456. totype:=t;
  457. if t.def=nil then
  458. internalerror(200103281);
  459. set_file_line(node);
  460. end;
  461. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  462. begin
  463. self.create(node,t);
  464. include(flags,nf_explicit);
  465. end;
  466. constructor ttypeconvnode.create_internal(node : tnode;const t:ttype);
  467. begin
  468. self.create(node,t);
  469. { handle like explicit conversions }
  470. include(flags,nf_explicit);
  471. include(flags,nf_internal);
  472. end;
  473. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  474. begin
  475. inherited ppuload(t,ppufile);
  476. ppufile.gettype(totype);
  477. convtype:=tconverttype(ppufile.getbyte);
  478. end;
  479. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  480. begin
  481. inherited ppuwrite(ppufile);
  482. ppufile.puttype(totype);
  483. ppufile.putbyte(byte(convtype));
  484. end;
  485. procedure ttypeconvnode.buildderefimpl;
  486. begin
  487. inherited buildderefimpl;
  488. totype.buildderef;
  489. end;
  490. procedure ttypeconvnode.derefimpl;
  491. begin
  492. inherited derefimpl;
  493. totype.resolve;
  494. end;
  495. function ttypeconvnode.getcopy : tnode;
  496. var
  497. n : ttypeconvnode;
  498. begin
  499. n:=ttypeconvnode(inherited getcopy);
  500. n.convtype:=convtype;
  501. n.totype:=totype;
  502. getcopy:=n;
  503. end;
  504. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  505. var
  506. t : tnode;
  507. begin
  508. result:=nil;
  509. if left.nodetype=ordconstn then
  510. begin
  511. { check if we have a valid pointer constant (JM) }
  512. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  513. if (sizeof(TConstPtrUInt) = 4) then
  514. begin
  515. if (tordconstnode(left).value < low(longint)) or
  516. (tordconstnode(left).value > high(cardinal)) then
  517. CGMessage(parser_e_range_check_error);
  518. end
  519. else if (sizeof(TConstPtrUInt) = 8) then
  520. begin
  521. if (tordconstnode(left).value < low(int64)) or
  522. (tordconstnode(left).value > high(qword)) then
  523. CGMessage(parser_e_range_check_error);
  524. end
  525. else
  526. internalerror(2001020801);
  527. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  528. result:=t;
  529. end
  530. else
  531. internalerror(200104023);
  532. end;
  533. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  534. begin
  535. result := ccallnode.createinternres(
  536. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  537. ccallparanode.create(left,nil),resulttype);
  538. left := nil;
  539. end;
  540. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  541. var
  542. arrsize : aint;
  543. begin
  544. with tarraydef(resulttype.def) do
  545. begin
  546. if highrange<lowrange then
  547. internalerror(75432653);
  548. arrsize := highrange-lowrange+1;
  549. end;
  550. if (left.nodetype = stringconstn) and
  551. { left.length+1 since there's always a terminating #0 character (JM) }
  552. (tstringconstnode(left).len+1 >= arrsize) and
  553. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  554. begin
  555. { handled separately }
  556. result := nil;
  557. exit;
  558. end;
  559. result := ccallnode.createinternres(
  560. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  561. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  562. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  563. left := nil;
  564. end;
  565. function ttypeconvnode.resulttype_string_to_string : tnode;
  566. var
  567. procname: string[31];
  568. stringpara : tcallparanode;
  569. pw : pcompilerwidestring;
  570. pc : pchar;
  571. begin
  572. result:=nil;
  573. if left.nodetype=stringconstn then
  574. begin
  575. { convert ascii 2 unicode }
  576. {$ifdef ansistring_bits}
  577. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  578. (tstringconstnode(left).st_type in [st_ansistring16,st_ansistring32,
  579. st_ansistring64,st_shortstring,st_longstring]) then
  580. {$else}
  581. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  582. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  583. {$endif}
  584. begin
  585. initwidestring(pw);
  586. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  587. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  588. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  589. end
  590. else
  591. { convert unicode 2 ascii }
  592. {$ifdef ansistring_bits}
  593. if (tstringconstnode(left).st_type=st_widestring) and
  594. (tstringdef(resulttype.def).string_typ in [st_ansistring16,st_ansistring32,
  595. st_ansistring64,st_shortstring,st_longstring]) then
  596. {$else}
  597. if (tstringconstnode(left).st_type=st_widestring) and
  598. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  599. {$endif}
  600. begin
  601. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  602. getmem(pc,getlengthwidestring(pw)+1);
  603. unicode2ascii(pw,pc);
  604. donewidestring(pw);
  605. tstringconstnode(left).value_str:=pc;
  606. end;
  607. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  608. tstringconstnode(left).resulttype:=resulttype;
  609. result:=left;
  610. left:=nil;
  611. end
  612. else
  613. begin
  614. { get the correct procedure name }
  615. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  616. '_to_'+tstringdef(resulttype.def).stringtypname;
  617. { create parameter (and remove left node from typeconvnode }
  618. { since it's reused as parameter) }
  619. stringpara := ccallparanode.create(left,nil);
  620. left := nil;
  621. { when converting to shortstrings, we have to pass high(destination) too }
  622. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  623. stringpara.right := ccallparanode.create(cinlinenode.create(
  624. in_high_x,false,self.getcopy),nil);
  625. { and create the callnode }
  626. result := ccallnode.createinternres(procname,stringpara,resulttype);
  627. end;
  628. end;
  629. function ttypeconvnode.resulttype_char_to_string : tnode;
  630. var
  631. procname: string[31];
  632. para : tcallparanode;
  633. hp : tstringconstnode;
  634. ws : pcompilerwidestring;
  635. begin
  636. result:=nil;
  637. if left.nodetype=ordconstn then
  638. begin
  639. if tstringdef(resulttype.def).string_typ=st_widestring then
  640. begin
  641. initwidestring(ws);
  642. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  643. hp:=cstringconstnode.createwstr(ws);
  644. donewidestring(ws);
  645. end
  646. else
  647. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  648. result:=hp;
  649. end
  650. else
  651. { shortstrings are handled 'inline' }
  652. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  653. begin
  654. { create the parameter }
  655. para := ccallparanode.create(left,nil);
  656. left := nil;
  657. { and the procname }
  658. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  659. { and finally the call }
  660. result := ccallnode.createinternres(procname,para,resulttype);
  661. end
  662. else
  663. begin
  664. { create word(byte(char) shl 8 or 1) for litte endian machines }
  665. { and word(byte(char) or 256) for big endian machines }
  666. left := ctypeconvnode.create_internal(left,u8inttype);
  667. if (target_info.endian = endian_little) then
  668. left := caddnode.create(orn,
  669. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  670. cordconstnode.create(1,s32inttype,false))
  671. else
  672. left := caddnode.create(orn,left,
  673. cordconstnode.create(1 shl 8,s32inttype,false));
  674. left := ctypeconvnode.create_internal(left,u16inttype);
  675. resulttypepass(left);
  676. end;
  677. end;
  678. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  679. begin
  680. if resulttype.def.size <> 1 then
  681. begin
  682. { convert first to string, then to chararray }
  683. inserttypeconv(left,cshortstringtype);
  684. inserttypeconv(left,resulttype);
  685. result:=left;
  686. left := nil;
  687. exit;
  688. end;
  689. result := nil;
  690. end;
  691. function ttypeconvnode.resulttype_char_to_char : tnode;
  692. var
  693. hp : tordconstnode;
  694. begin
  695. result:=nil;
  696. if left.nodetype=ordconstn then
  697. begin
  698. if (torddef(resulttype.def).typ=uchar) and
  699. (torddef(left.resulttype.def).typ=uwidechar) then
  700. begin
  701. hp:=cordconstnode.create(
  702. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  703. cchartype,true);
  704. result:=hp;
  705. end
  706. else if (torddef(resulttype.def).typ=uwidechar) and
  707. (torddef(left.resulttype.def).typ=uchar) then
  708. begin
  709. hp:=cordconstnode.create(
  710. asciichar2unicode(chr(tordconstnode(left).value)),
  711. cwidechartype,true);
  712. result:=hp;
  713. end
  714. else
  715. internalerror(200105131);
  716. exit;
  717. end;
  718. end;
  719. function ttypeconvnode.resulttype_int_to_int : tnode;
  720. var
  721. v : TConstExprInt;
  722. begin
  723. result:=nil;
  724. if left.nodetype=ordconstn then
  725. begin
  726. v:=tordconstnode(left).value;
  727. if is_currency(resulttype.def) then
  728. v:=v*10000;
  729. if (resulttype.def.deftype=pointerdef) then
  730. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  731. else
  732. begin
  733. if is_currency(left.resulttype.def) then
  734. v:=v div 10000;
  735. result:=cordconstnode.create(v,resulttype,false);
  736. end;
  737. end
  738. else if left.nodetype=pointerconstn then
  739. begin
  740. v:=tpointerconstnode(left).value;
  741. if (resulttype.def.deftype=pointerdef) then
  742. result:=cpointerconstnode.create(v,resulttype)
  743. else
  744. begin
  745. if is_currency(resulttype.def) then
  746. v:=v*10000;
  747. result:=cordconstnode.create(v,resulttype,false);
  748. end;
  749. end
  750. else
  751. begin
  752. { multiply by 10000 for currency. We need to use getcopy to pass
  753. the argument because the current node is always disposed. Only
  754. inserting the multiply in the left node is not possible because
  755. it'll get in an infinite loop to convert int->currency }
  756. if is_currency(resulttype.def) then
  757. begin
  758. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  759. include(result.flags,nf_is_currency);
  760. end
  761. else if is_currency(left.resulttype.def) then
  762. begin
  763. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  764. include(result.flags,nf_is_currency);
  765. end;
  766. end;
  767. end;
  768. function ttypeconvnode.resulttype_int_to_real : tnode;
  769. var
  770. rv : bestreal;
  771. begin
  772. result:=nil;
  773. if left.nodetype=ordconstn then
  774. begin
  775. rv:=tordconstnode(left).value;
  776. if is_currency(resulttype.def) then
  777. rv:=rv*10000.0
  778. else if is_currency(left.resulttype.def) then
  779. rv:=rv/10000.0;
  780. result:=crealconstnode.create(rv,resulttype);
  781. end
  782. else
  783. begin
  784. { multiply by 10000 for currency. We need to use getcopy to pass
  785. the argument because the current node is always disposed. Only
  786. inserting the multiply in the left node is not possible because
  787. it'll get in an infinite loop to convert int->currency }
  788. if is_currency(resulttype.def) then
  789. begin
  790. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  791. include(result.flags,nf_is_currency);
  792. end
  793. else if is_currency(left.resulttype.def) then
  794. begin
  795. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  796. include(result.flags,nf_is_currency);
  797. end;
  798. end;
  799. end;
  800. function ttypeconvnode.resulttype_real_to_currency : tnode;
  801. begin
  802. if not is_currency(resulttype.def) then
  803. internalerror(200304221);
  804. result:=nil;
  805. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  806. include(left.flags,nf_is_currency);
  807. resulttypepass(left);
  808. { Convert constants directly, else call Round() }
  809. if left.nodetype=realconstn then
  810. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  811. else
  812. result:=ccallnode.createinternres('fpc_round_real',
  813. ccallparanode.create(left,nil),resulttype);
  814. left:=nil;
  815. end;
  816. function ttypeconvnode.resulttype_real_to_real : tnode;
  817. begin
  818. result:=nil;
  819. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  820. begin
  821. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  822. include(left.flags,nf_is_currency);
  823. resulttypepass(left);
  824. end
  825. else
  826. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  827. begin
  828. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  829. include(left.flags,nf_is_currency);
  830. resulttypepass(left);
  831. end;
  832. if left.nodetype=realconstn then
  833. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  834. end;
  835. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  836. begin
  837. result:=nil;
  838. if is_pwidechar(resulttype.def) then
  839. inserttypeconv(left,cwidestringtype)
  840. else
  841. inserttypeconv(left,cshortstringtype);
  842. { evaluate again, reset resulttype so the convert_typ
  843. will be calculated again and cstring_to_pchar will
  844. be used for futher conversion }
  845. result:=det_resulttype;
  846. end;
  847. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  848. begin
  849. result:=nil;
  850. if is_pwidechar(resulttype.def) then
  851. inserttypeconv(left,cwidestringtype);
  852. end;
  853. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  854. var
  855. hp : tnode;
  856. begin
  857. result:=nil;
  858. if left.nodetype<>arrayconstructorn then
  859. internalerror(5546);
  860. { remove typeconv node }
  861. hp:=left;
  862. left:=nil;
  863. { create a set constructor tree }
  864. arrayconstructor_to_set(hp);
  865. result:=hp;
  866. end;
  867. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  868. begin
  869. result := ccallnode.createinternres(
  870. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  871. ccallparanode.create(left,nil),resulttype);
  872. left := nil;
  873. end;
  874. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  875. begin
  876. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  877. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  878. end;
  879. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  880. begin
  881. { a dynamic array is a pointer to an array, so to convert it to }
  882. { an open array, we have to dereference it (JM) }
  883. result := ctypeconvnode.create_internal(left,voidpointertype);
  884. resulttypepass(result);
  885. { left is reused }
  886. left := nil;
  887. result := cderefnode.create(result);
  888. include(result.flags,nf_no_checkpointer);
  889. result.resulttype := resulttype;
  890. end;
  891. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  892. begin
  893. result := ccallnode.createinternres(
  894. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  895. ccallparanode.create(left,nil),resulttype);
  896. left := nil;
  897. end;
  898. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  899. begin
  900. result := ccallnode.createinternres(
  901. 'fpc_variant_to_dynarray',
  902. ccallparanode.create(caddrnode.create(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  903. ccallparanode.create(left,nil)
  904. ),resulttype);
  905. left := nil;
  906. end;
  907. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  908. begin
  909. result := ccallnode.createinternres(
  910. 'fpc_dynarray_to_variant',
  911. ccallparanode.create(caddrnode.create(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  912. ccallparanode.create(left,nil)
  913. ),resulttype);
  914. result:=nil;
  915. end;
  916. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  917. begin
  918. result := ctypeconvnode.create_internal(left,sinttype);
  919. result := ctypeconvnode.create_internal(result,resulttype);
  920. resulttypepass(result);
  921. { left is reused }
  922. left := nil;
  923. end;
  924. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  925. begin
  926. result := ctypeconvnode.create_internal(left,sinttype);
  927. result := ctypeconvnode.create_internal(result,cvarianttype);
  928. resulttypepass(result);
  929. { left is reused }
  930. left := nil;
  931. end;
  932. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  933. {$ifdef fpc}
  934. const
  935. resulttypeconvert : array[tconverttype] of pointer = (
  936. {equal} nil,
  937. {not_possible} nil,
  938. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  939. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  940. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  941. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  942. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  943. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  944. { ansistring_2_pchar } nil,
  945. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  946. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  947. { array_2_pointer } nil,
  948. { pointer_2_array } nil,
  949. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  950. { int_2_bool } nil,
  951. { bool_2_bool } nil,
  952. { bool_2_int } nil,
  953. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  954. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  955. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  956. { proc_2_procvar } nil,
  957. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  958. { load_smallset } nil,
  959. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  960. { intf_2_string } nil,
  961. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  962. { class_2_intf } nil,
  963. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  964. { normal_2_smallset} nil,
  965. { dynarray_2_openarray} @resulttype_dynarray_to_openarray,
  966. { pwchar_2_string} @resulttype_pwchar_to_string,
  967. { variant_2_dynarray} @resulttype_variant_to_dynarray,
  968. { dynarray_2_variant} @resulttype_dynarray_to_variant,
  969. { variant_2_enum} @resulttype_variant_to_enum,
  970. { enum_2_variant} @resulttype_enum_to_variant
  971. );
  972. type
  973. tprocedureofobject = function : tnode of object;
  974. var
  975. r : packed record
  976. proc : pointer;
  977. obj : pointer;
  978. end;
  979. begin
  980. result:=nil;
  981. { this is a little bit dirty but it works }
  982. { and should be quite portable too }
  983. r.proc:=resulttypeconvert[c];
  984. r.obj:=self;
  985. if assigned(r.proc) then
  986. result:=tprocedureofobject(r)();
  987. end;
  988. {$else}
  989. begin
  990. case c of
  991. tc_string_2_string: resulttype_string_to_string;
  992. tc_char_2_string : resulttype_char_to_string;
  993. tc_char_2_chararray: resulttype_char_to_chararray;
  994. tc_pchar_2_string : resulttype_pchar_to_string;
  995. tc_cchar_2_pchar : resulttype_cchar_to_pchar;
  996. tc_cstring_2_pchar : resulttype_cstring_to_pchar;
  997. tc_string_2_chararray : resulttype_string_to_chararray;
  998. tc_chararray_2_string : resulttype_chararray_to_string;
  999. tc_real_2_real : resulttype_real_to_real;
  1000. tc_int_2_real : resulttype_int_to_real;
  1001. tc_real_2_currency : resulttype_real_to_currency;
  1002. tc_arrayconstructor_2_set : resulttype_arrayconstructor_to_set;
  1003. tc_cord_2_pointer : resulttype_cord_to_pointer;
  1004. tc_intf_2_guid : resulttype_interface_to_guid;
  1005. tc_char_2_char : resulttype_char_to_char;
  1006. tc_dynarray_2_openarray : resulttype_dynarray_to_openarray;
  1007. tc_pwchar_2_string : resulttype_pwchar_to_string;
  1008. tc_variant_2_dynarray : resulttype_variant_to_dynarray;
  1009. tc_dynarray_2_variant : resulttype_dynarray_to_variant;
  1010. end;
  1011. end;
  1012. {$Endif fpc}
  1013. function ttypeconvnode.det_resulttype:tnode;
  1014. var
  1015. htype : ttype;
  1016. hp : tnode;
  1017. currprocdef,
  1018. aprocdef : tprocdef;
  1019. eq : tequaltype;
  1020. cdoptions : tcompare_defs_options;
  1021. begin
  1022. result:=nil;
  1023. resulttype:=totype;
  1024. resulttypepass(left);
  1025. if codegenerror then
  1026. exit;
  1027. { When absolute force tc_equal }
  1028. if (nf_absolute in flags) then
  1029. begin
  1030. convtype:=tc_equal;
  1031. if not(tstoreddef(resulttype.def).is_intregable) and
  1032. not(tstoreddef(resulttype.def).is_fpuregable) then
  1033. make_not_regable(left);
  1034. exit;
  1035. end;
  1036. { tp procvar support. Skip typecasts to record or set. Those
  1037. convert on the procvar value. This is used to access the
  1038. fields of a methodpointer }
  1039. if not(resulttype.def.deftype in [recorddef,setdef]) then
  1040. maybe_call_procvar(left,true);
  1041. { convert array constructors to sets, because there is no conversion
  1042. possible for array constructors }
  1043. if (resulttype.def.deftype<>arraydef) and
  1044. is_array_constructor(left.resulttype.def) then
  1045. begin
  1046. arrayconstructor_to_set(left);
  1047. resulttypepass(left);
  1048. end;
  1049. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1050. if nf_explicit in flags then
  1051. include(cdoptions,cdo_explicit);
  1052. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1053. case eq of
  1054. te_exact,
  1055. te_equal :
  1056. begin
  1057. { because is_equal only checks the basetype for sets we need to
  1058. check here if we are loading a smallset into a normalset }
  1059. if (resulttype.def.deftype=setdef) and
  1060. (left.resulttype.def.deftype=setdef) and
  1061. ((tsetdef(resulttype.def).settype = smallset) xor
  1062. (tsetdef(left.resulttype.def).settype = smallset)) then
  1063. begin
  1064. { constant sets can be converted by changing the type only }
  1065. if (left.nodetype=setconstn) then
  1066. begin
  1067. left.resulttype:=resulttype;
  1068. result:=left;
  1069. left:=nil;
  1070. exit;
  1071. end;
  1072. if (tsetdef(resulttype.def).settype <> smallset) then
  1073. convtype:=tc_load_smallset
  1074. else
  1075. convtype := tc_normal_2_smallset;
  1076. exit;
  1077. end
  1078. else
  1079. begin
  1080. { Only leave when there is no conversion to do.
  1081. We can still need to call a conversion routine,
  1082. like the routine to convert a stringconstnode }
  1083. if convtype in [tc_equal,tc_not_possible] then
  1084. begin
  1085. left.resulttype:=resulttype;
  1086. result:=left;
  1087. left:=nil;
  1088. exit;
  1089. end;
  1090. end;
  1091. end;
  1092. te_convert_l1,
  1093. te_convert_l2,
  1094. te_convert_l3 :
  1095. begin
  1096. { nothing to do }
  1097. end;
  1098. te_convert_operator :
  1099. begin
  1100. include(current_procinfo.flags,pi_do_call);
  1101. inc(aprocdef.procsym.refs);
  1102. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1103. { tell explicitly which def we must use !! (PM) }
  1104. tcallnode(hp).procdefinition:=aprocdef;
  1105. left:=nil;
  1106. result:=hp;
  1107. exit;
  1108. end;
  1109. te_incompatible :
  1110. begin
  1111. { Procedures have a resulttype.def of voiddef and functions of their
  1112. own resulttype.def. They will therefore always be incompatible with
  1113. a procvar. Because isconvertable cannot check for procedures we
  1114. use an extra check for them.}
  1115. if (m_tp_procvar in aktmodeswitches) and
  1116. (resulttype.def.deftype=procvardef) then
  1117. begin
  1118. if is_procsym_load(left) then
  1119. begin
  1120. if (left.nodetype<>addrn) then
  1121. begin
  1122. convtype:=tc_proc_2_procvar;
  1123. { Now check if the procedure we are going to assign to
  1124. the procvar, is compatible with the procvar's type }
  1125. if not(nf_explicit in flags) and
  1126. (proc_to_procvar_equal(tprocsym(tloadnode(left).symtableentry).first_procdef,
  1127. tprocvardef(resulttype.def),true)=te_incompatible) then
  1128. IncompatibleTypes(tprocsym(tloadnode(left).symtableentry).first_procdef,resulttype.def);
  1129. exit;
  1130. end;
  1131. end
  1132. else
  1133. if (left.nodetype=calln) and
  1134. (tcallnode(left).para_count=0) then
  1135. begin
  1136. if assigned(tcallnode(left).right) then
  1137. begin
  1138. { this is already a procvar, if it is really equal
  1139. is checked below }
  1140. convtype:=tc_equal;
  1141. hp:=tcallnode(left).right.getcopy;
  1142. currprocdef:=tprocdef(hp.resulttype.def);
  1143. end
  1144. else
  1145. begin
  1146. convtype:=tc_proc_2_procvar;
  1147. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1148. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1149. currprocdef,tcallnode(left).symtableproc);
  1150. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1151. begin
  1152. if assigned(tcallnode(left).methodpointer) then
  1153. begin
  1154. { Under certain circumstances the methodpointer is a loadvmtaddrn
  1155. which isn't possible if it is used as a method pointer, so
  1156. fix this.
  1157. If you change this, ensure that tests/tbs/tw2669.pp still works }
  1158. if tcallnode(left).methodpointer.nodetype=loadvmtaddrn then
  1159. tloadnode(hp).set_mp(tloadvmtaddrnode(tcallnode(left).methodpointer).left.getcopy)
  1160. else
  1161. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  1162. end
  1163. else
  1164. tloadnode(hp).set_mp(load_self_node);
  1165. end;
  1166. resulttypepass(hp);
  1167. end;
  1168. left.free;
  1169. left:=hp;
  1170. { Now check if the procedure we are going to assign to
  1171. the procvar, is compatible with the procvar's type }
  1172. if not(nf_explicit in flags) and
  1173. (proc_to_procvar_equal(currprocdef,
  1174. tprocvardef(resulttype.def),true)=te_incompatible) then
  1175. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1176. exit;
  1177. end;
  1178. end;
  1179. { Handle explicit type conversions }
  1180. if nf_explicit in flags then
  1181. begin
  1182. { do common tc_equal cast }
  1183. convtype:=tc_equal;
  1184. { ordinal constants can be resized to 1,2,4,8 bytes }
  1185. if (left.nodetype=ordconstn) then
  1186. begin
  1187. { Insert typeconv for ordinal to the correct size first on left, after
  1188. that the other conversion can be done }
  1189. htype.reset;
  1190. case longint(resulttype.def.size) of
  1191. 1 :
  1192. htype:=s8inttype;
  1193. 2 :
  1194. htype:=s16inttype;
  1195. 4 :
  1196. htype:=s32inttype;
  1197. 8 :
  1198. htype:=s64inttype;
  1199. end;
  1200. { we need explicit, because it can also be an enum }
  1201. if assigned(htype.def) then
  1202. inserttypeconv_internal(left,htype)
  1203. else
  1204. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1205. end;
  1206. { check if the result could be in a register }
  1207. if (not(tstoreddef(resulttype.def).is_intregable) and
  1208. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1209. ((left.resulttype.def.deftype = floatdef) and
  1210. (resulttype.def.deftype <> floatdef)) then
  1211. make_not_regable(left);
  1212. { class to class or object to object, with checkobject support }
  1213. if (resulttype.def.deftype=objectdef) and
  1214. (left.resulttype.def.deftype=objectdef) then
  1215. begin
  1216. if (cs_check_object in aktlocalswitches) then
  1217. begin
  1218. if is_class_or_interface(resulttype.def) then
  1219. begin
  1220. { we can translate the typeconvnode to 'as' when
  1221. typecasting to a class or interface }
  1222. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1223. left:=nil;
  1224. result:=hp;
  1225. exit;
  1226. end;
  1227. end
  1228. else
  1229. begin
  1230. { check if the types are related }
  1231. if not(nf_internal in flags) and
  1232. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1233. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1234. begin
  1235. { Give an error when typecasting class to interface, this is compatible
  1236. with delphi }
  1237. if is_interface(resulttype.def) and
  1238. not is_interface(left.resulttype.def) then
  1239. CGMessage2(type_e_classes_not_related,
  1240. FullTypeName(left.resulttype.def,resulttype.def),
  1241. FullTypeName(resulttype.def,left.resulttype.def))
  1242. else
  1243. CGMessage2(type_w_classes_not_related,
  1244. FullTypeName(left.resulttype.def,resulttype.def),
  1245. FullTypeName(resulttype.def,left.resulttype.def))
  1246. end;
  1247. end;
  1248. end
  1249. else
  1250. begin
  1251. { only if the same size or formal def }
  1252. if not(
  1253. (left.resulttype.def.deftype=formaldef) or
  1254. (
  1255. not(is_open_array(left.resulttype.def)) and
  1256. (left.resulttype.def.size=resulttype.def.size)
  1257. ) or
  1258. (
  1259. is_void(left.resulttype.def) and
  1260. (left.nodetype=derefn)
  1261. )
  1262. ) then
  1263. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1264. end;
  1265. end
  1266. else
  1267. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1268. end;
  1269. else
  1270. internalerror(200211231);
  1271. end;
  1272. { Give hint or warning for unportable code, exceptions are
  1273. - typecasts from constants
  1274. - void }
  1275. if not(nf_internal in flags) and
  1276. (left.nodetype<>ordconstn) and
  1277. not(is_void(left.resulttype.def)) and
  1278. (((left.resulttype.def.deftype=orddef) and
  1279. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1280. ((resulttype.def.deftype=orddef) and
  1281. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1282. begin
  1283. { Give a warning when sizes don't match, because then info will be lost }
  1284. if left.resulttype.def.size=resulttype.def.size then
  1285. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1286. else
  1287. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1288. end;
  1289. { Constant folding and other node transitions to
  1290. remove the typeconv node }
  1291. case left.nodetype of
  1292. niln :
  1293. begin
  1294. { nil to ordinal node }
  1295. if (resulttype.def.deftype=orddef) then
  1296. begin
  1297. hp:=cordconstnode.create(0,resulttype,true);
  1298. result:=hp;
  1299. exit;
  1300. end
  1301. else
  1302. { fold nil to any pointer type }
  1303. if (resulttype.def.deftype=pointerdef) then
  1304. begin
  1305. hp:=cnilnode.create;
  1306. hp.resulttype:=resulttype;
  1307. result:=hp;
  1308. exit;
  1309. end
  1310. else
  1311. { remove typeconv after niln, but not when the result is a
  1312. methodpointer. The typeconv of the methodpointer will then
  1313. take care of updateing size of niln to OS_64 }
  1314. if not((resulttype.def.deftype=procvardef) and
  1315. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1316. begin
  1317. left.resulttype:=resulttype;
  1318. result:=left;
  1319. left:=nil;
  1320. exit;
  1321. end;
  1322. end;
  1323. ordconstn :
  1324. begin
  1325. { ordinal contants can be directly converted }
  1326. { but not char to char because it is a widechar to char or via versa }
  1327. { which needs extra code to do the code page transistion }
  1328. { constant ordinal to pointer }
  1329. if (resulttype.def.deftype=pointerdef) and
  1330. (convtype<>tc_cchar_2_pchar) then
  1331. begin
  1332. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1333. result:=hp;
  1334. exit;
  1335. end
  1336. else if is_ordinal(resulttype.def) and
  1337. not(convtype=tc_char_2_char) then
  1338. begin
  1339. { replace the resulttype and recheck the range }
  1340. left.resulttype:=resulttype;
  1341. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1342. result:=left;
  1343. left:=nil;
  1344. exit;
  1345. end;
  1346. end;
  1347. pointerconstn :
  1348. begin
  1349. { pointerconstn to any pointer is folded too }
  1350. if (resulttype.def.deftype=pointerdef) then
  1351. begin
  1352. left.resulttype:=resulttype;
  1353. result:=left;
  1354. left:=nil;
  1355. exit;
  1356. end
  1357. { constant pointer to ordinal }
  1358. else if is_ordinal(resulttype.def) then
  1359. begin
  1360. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1361. resulttype,true);
  1362. result:=hp;
  1363. exit;
  1364. end;
  1365. end;
  1366. end;
  1367. { check if the result could be in a register }
  1368. if not(tstoreddef(resulttype.def).is_intregable) and
  1369. not(tstoreddef(resulttype.def).is_fpuregable) then
  1370. make_not_regable(left);
  1371. { now call the resulttype helper to do constant folding }
  1372. result:=resulttype_call_helper(convtype);
  1373. end;
  1374. procedure Ttypeconvnode.mark_write;
  1375. begin
  1376. left.mark_write;
  1377. end;
  1378. function ttypeconvnode.first_cord_to_pointer : tnode;
  1379. begin
  1380. result:=nil;
  1381. internalerror(200104043);
  1382. end;
  1383. function ttypeconvnode.first_int_to_int : tnode;
  1384. begin
  1385. first_int_to_int:=nil;
  1386. expectloc:=left.expectloc;
  1387. if not is_void(left.resulttype.def) then
  1388. begin
  1389. if (left.expectloc<>LOC_REGISTER) and
  1390. (resulttype.def.size>left.resulttype.def.size) then
  1391. expectloc:=LOC_REGISTER
  1392. else
  1393. if (left.expectloc=LOC_CREGISTER) and
  1394. (resulttype.def.size<left.resulttype.def.size) then
  1395. expectloc:=LOC_REGISTER;
  1396. end;
  1397. {$ifndef cpu64bit}
  1398. if is_64bit(resulttype.def) then
  1399. registersint:=max(registersint,2)
  1400. else
  1401. {$endif cpu64bit}
  1402. registersint:=max(registersint,1);
  1403. end;
  1404. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1405. begin
  1406. first_cstring_to_pchar:=nil;
  1407. registersint:=1;
  1408. expectloc:=LOC_REGISTER;
  1409. end;
  1410. function ttypeconvnode.first_string_to_chararray : tnode;
  1411. begin
  1412. first_string_to_chararray:=nil;
  1413. expectloc:=left.expectloc;
  1414. end;
  1415. function ttypeconvnode.first_char_to_string : tnode;
  1416. begin
  1417. first_char_to_string:=nil;
  1418. expectloc:=LOC_REFERENCE;
  1419. end;
  1420. function ttypeconvnode.first_nothing : tnode;
  1421. begin
  1422. first_nothing:=nil;
  1423. end;
  1424. function ttypeconvnode.first_array_to_pointer : tnode;
  1425. begin
  1426. first_array_to_pointer:=nil;
  1427. if registersint<1 then
  1428. registersint:=1;
  1429. expectloc:=LOC_REGISTER;
  1430. end;
  1431. function ttypeconvnode.first_int_to_real: tnode;
  1432. var
  1433. fname: string[32];
  1434. typname : string[12];
  1435. begin
  1436. { Get the type name }
  1437. { Normally the typename should be one of the following:
  1438. single, double - carl
  1439. }
  1440. typname := lower(pbestrealtype^.def.gettypename);
  1441. { converting a 64bit integer to a float requires a helper }
  1442. if is_64bit(left.resulttype.def) then
  1443. begin
  1444. if is_signed(left.resulttype.def) then
  1445. fname := 'fpc_int64_to_'+typname
  1446. else
  1447. {$warning generic conversion from int to float does not support unsigned integers}
  1448. fname := 'fpc_int64_to_'+typname;
  1449. result := ccallnode.createintern(fname,ccallparanode.create(
  1450. left,nil));
  1451. left:=nil;
  1452. firstpass(result);
  1453. exit;
  1454. end
  1455. else
  1456. { other integers are supposed to be 32 bit }
  1457. begin
  1458. {$warning generic conversion from int to float does not support unsigned integers}
  1459. if is_signed(left.resulttype.def) then
  1460. fname := 'fpc_longint_to_'+typname
  1461. else
  1462. fname := 'fpc_longint_to_'+typname;
  1463. result := ccallnode.createintern(fname,ccallparanode.create(
  1464. left,nil));
  1465. left:=nil;
  1466. firstpass(result);
  1467. exit;
  1468. end;
  1469. end;
  1470. function ttypeconvnode.first_real_to_real : tnode;
  1471. begin
  1472. first_real_to_real:=nil;
  1473. { comp isn't a floating type }
  1474. if registersfpu<1 then
  1475. registersfpu:=1;
  1476. expectloc:=LOC_FPUREGISTER;
  1477. end;
  1478. function ttypeconvnode.first_pointer_to_array : tnode;
  1479. begin
  1480. first_pointer_to_array:=nil;
  1481. if registersint<1 then
  1482. registersint:=1;
  1483. expectloc:=LOC_REFERENCE;
  1484. end;
  1485. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1486. begin
  1487. first_cchar_to_pchar:=nil;
  1488. internalerror(200104021);
  1489. end;
  1490. function ttypeconvnode.first_bool_to_int : tnode;
  1491. begin
  1492. first_bool_to_int:=nil;
  1493. { byte(boolean) or word(wordbool) or longint(longbool) must
  1494. be accepted for var parameters }
  1495. if (nf_explicit in flags) and
  1496. (left.resulttype.def.size=resulttype.def.size) and
  1497. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1498. exit;
  1499. { when converting to 64bit, first convert to a 32bit int and then }
  1500. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1501. if resulttype.def.size > sizeof(aint) then
  1502. begin
  1503. result := ctypeconvnode.create_internal(left,u32inttype);
  1504. result := ctypeconvnode.create(result,resulttype);
  1505. left := nil;
  1506. firstpass(result);
  1507. exit;
  1508. end;
  1509. expectloc:=LOC_REGISTER;
  1510. if registersint<1 then
  1511. registersint:=1;
  1512. end;
  1513. function ttypeconvnode.first_int_to_bool : tnode;
  1514. begin
  1515. first_int_to_bool:=nil;
  1516. { byte(boolean) or word(wordbool) or longint(longbool) must
  1517. be accepted for var parameters }
  1518. if (nf_explicit in flags) and
  1519. (left.resulttype.def.size=resulttype.def.size) and
  1520. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1521. exit;
  1522. expectloc:=LOC_REGISTER;
  1523. { need if bool to bool !!
  1524. not very nice !!
  1525. insertypeconv(left,s32inttype);
  1526. left.explizit:=true;
  1527. firstpass(left); }
  1528. if registersint<1 then
  1529. registersint:=1;
  1530. end;
  1531. function ttypeconvnode.first_bool_to_bool : tnode;
  1532. begin
  1533. first_bool_to_bool:=nil;
  1534. expectloc:=LOC_REGISTER;
  1535. if registersint<1 then
  1536. registersint:=1;
  1537. end;
  1538. function ttypeconvnode.first_char_to_char : tnode;
  1539. begin
  1540. first_char_to_char:=first_int_to_int;
  1541. end;
  1542. function ttypeconvnode.first_proc_to_procvar : tnode;
  1543. begin
  1544. first_proc_to_procvar:=nil;
  1545. if assigned(tunarynode(left).left) then
  1546. begin
  1547. if (left.expectloc<>LOC_CREFERENCE) then
  1548. CGMessage(parser_e_illegal_expression);
  1549. registersint:=left.registersint;
  1550. expectloc:=left.expectloc
  1551. end
  1552. else
  1553. begin
  1554. registersint:=left.registersint;
  1555. if registersint<1 then
  1556. registersint:=1;
  1557. expectloc:=LOC_REGISTER;
  1558. end
  1559. end;
  1560. function ttypeconvnode.first_load_smallset : tnode;
  1561. var
  1562. srsym: ttypesym;
  1563. p: tcallparanode;
  1564. begin
  1565. if not searchsystype('FPC_SMALL_SET',srsym) then
  1566. internalerror(200108313);
  1567. p := ccallparanode.create(left,nil);
  1568. { reused }
  1569. left := nil;
  1570. { convert parameter explicitely to fpc_small_set }
  1571. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1572. { create call, adjust resulttype }
  1573. result :=
  1574. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1575. firstpass(result);
  1576. end;
  1577. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1578. begin
  1579. first_ansistring_to_pchar:=nil;
  1580. expectloc:=LOC_REGISTER;
  1581. if registersint<1 then
  1582. registersint:=1;
  1583. end;
  1584. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1585. begin
  1586. first_arrayconstructor_to_set:=nil;
  1587. internalerror(200104022);
  1588. end;
  1589. function ttypeconvnode.first_class_to_intf : tnode;
  1590. begin
  1591. first_class_to_intf:=nil;
  1592. expectloc:=LOC_REGISTER;
  1593. if registersint<1 then
  1594. registersint:=1;
  1595. end;
  1596. function ttypeconvnode._first_int_to_int : tnode;
  1597. begin
  1598. result:=first_int_to_int;
  1599. end;
  1600. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1601. begin
  1602. result:=first_cstring_to_pchar;
  1603. end;
  1604. function ttypeconvnode._first_string_to_chararray : tnode;
  1605. begin
  1606. result:=first_string_to_chararray;
  1607. end;
  1608. function ttypeconvnode._first_char_to_string : tnode;
  1609. begin
  1610. result:=first_char_to_string;
  1611. end;
  1612. function ttypeconvnode._first_nothing : tnode;
  1613. begin
  1614. result:=first_nothing;
  1615. end;
  1616. function ttypeconvnode._first_array_to_pointer : tnode;
  1617. begin
  1618. result:=first_array_to_pointer;
  1619. end;
  1620. function ttypeconvnode._first_int_to_real : tnode;
  1621. begin
  1622. result:=first_int_to_real;
  1623. end;
  1624. function ttypeconvnode._first_real_to_real : tnode;
  1625. begin
  1626. result:=first_real_to_real;
  1627. end;
  1628. function ttypeconvnode._first_pointer_to_array : tnode;
  1629. begin
  1630. result:=first_pointer_to_array;
  1631. end;
  1632. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1633. begin
  1634. result:=first_cchar_to_pchar;
  1635. end;
  1636. function ttypeconvnode._first_bool_to_int : tnode;
  1637. begin
  1638. result:=first_bool_to_int;
  1639. end;
  1640. function ttypeconvnode._first_int_to_bool : tnode;
  1641. begin
  1642. result:=first_int_to_bool;
  1643. end;
  1644. function ttypeconvnode._first_bool_to_bool : tnode;
  1645. begin
  1646. result:=first_bool_to_bool;
  1647. end;
  1648. function ttypeconvnode._first_proc_to_procvar : tnode;
  1649. begin
  1650. result:=first_proc_to_procvar;
  1651. end;
  1652. function ttypeconvnode._first_load_smallset : tnode;
  1653. begin
  1654. result:=first_load_smallset;
  1655. end;
  1656. function ttypeconvnode._first_cord_to_pointer : tnode;
  1657. begin
  1658. result:=first_cord_to_pointer;
  1659. end;
  1660. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1661. begin
  1662. result:=first_ansistring_to_pchar;
  1663. end;
  1664. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1665. begin
  1666. result:=first_arrayconstructor_to_set;
  1667. end;
  1668. function ttypeconvnode._first_class_to_intf : tnode;
  1669. begin
  1670. result:=first_class_to_intf;
  1671. end;
  1672. function ttypeconvnode._first_char_to_char : tnode;
  1673. begin
  1674. result:=first_char_to_char;
  1675. end;
  1676. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1677. const
  1678. firstconvert : array[tconverttype] of pointer = (
  1679. @ttypeconvnode._first_nothing, {equal}
  1680. @ttypeconvnode._first_nothing, {not_possible}
  1681. nil, { removed in resulttype_string_to_string }
  1682. @ttypeconvnode._first_char_to_string,
  1683. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1684. nil, { removed in resulttype_chararray_to_string }
  1685. @ttypeconvnode._first_cchar_to_pchar,
  1686. @ttypeconvnode._first_cstring_to_pchar,
  1687. @ttypeconvnode._first_ansistring_to_pchar,
  1688. @ttypeconvnode._first_string_to_chararray,
  1689. nil, { removed in resulttype_chararray_to_string }
  1690. @ttypeconvnode._first_array_to_pointer,
  1691. @ttypeconvnode._first_pointer_to_array,
  1692. @ttypeconvnode._first_int_to_int,
  1693. @ttypeconvnode._first_int_to_bool,
  1694. @ttypeconvnode._first_bool_to_bool,
  1695. @ttypeconvnode._first_bool_to_int,
  1696. @ttypeconvnode._first_real_to_real,
  1697. @ttypeconvnode._first_int_to_real,
  1698. nil, { removed in resulttype_real_to_currency }
  1699. @ttypeconvnode._first_proc_to_procvar,
  1700. @ttypeconvnode._first_arrayconstructor_to_set,
  1701. @ttypeconvnode._first_load_smallset,
  1702. @ttypeconvnode._first_cord_to_pointer,
  1703. @ttypeconvnode._first_nothing,
  1704. @ttypeconvnode._first_nothing,
  1705. @ttypeconvnode._first_class_to_intf,
  1706. @ttypeconvnode._first_char_to_char,
  1707. @ttypeconvnode._first_nothing,
  1708. @ttypeconvnode._first_nothing,
  1709. nil,
  1710. nil,
  1711. nil,
  1712. nil,
  1713. nil
  1714. );
  1715. type
  1716. tprocedureofobject = function : tnode of object;
  1717. var
  1718. r : packed record
  1719. proc : pointer;
  1720. obj : pointer;
  1721. end;
  1722. begin
  1723. { this is a little bit dirty but it works }
  1724. { and should be quite portable too }
  1725. r.proc:=firstconvert[c];
  1726. r.obj:=self;
  1727. if not assigned(r.proc) then
  1728. internalerror(200312081);
  1729. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1730. end;
  1731. function ttypeconvnode.pass_1 : tnode;
  1732. begin
  1733. result:=nil;
  1734. firstpass(left);
  1735. if codegenerror then
  1736. exit;
  1737. { load the value_str from the left part }
  1738. registersint:=left.registersint;
  1739. registersfpu:=left.registersfpu;
  1740. {$ifdef SUPPORT_MMX}
  1741. registersmmx:=left.registersmmx;
  1742. {$endif}
  1743. expectloc:=left.expectloc;
  1744. result:=first_call_helper(convtype);
  1745. end;
  1746. function ttypeconvnode.assign_allowed:boolean;
  1747. begin
  1748. result:=(convtype=tc_equal) or
  1749. { typecasting from void is always allowed }
  1750. is_void(left.resulttype.def) or
  1751. (left.resulttype.def.deftype=formaldef) or
  1752. { int 2 int with same size reuses same location, or for
  1753. tp7 mode also allow size < orignal size }
  1754. (
  1755. (convtype=tc_int_2_int) and
  1756. (
  1757. (resulttype.def.size=left.resulttype.def.size) or
  1758. ((m_tp7 in aktmodeswitches) and
  1759. (resulttype.def.size<left.resulttype.def.size))
  1760. )
  1761. ) or
  1762. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1763. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1764. (nf_explicit in flags) and
  1765. (resulttype.def.size=left.resulttype.def.size));
  1766. { When using only a part of the value it can't be in a register since
  1767. that will load the value in a new register first }
  1768. if (resulttype.def.size<left.resulttype.def.size) then
  1769. make_not_regable(left);
  1770. end;
  1771. function ttypeconvnode.docompare(p: tnode) : boolean;
  1772. begin
  1773. docompare :=
  1774. inherited docompare(p) and
  1775. (convtype = ttypeconvnode(p).convtype);
  1776. end;
  1777. procedure ttypeconvnode._second_int_to_int;
  1778. begin
  1779. second_int_to_int;
  1780. end;
  1781. procedure ttypeconvnode._second_string_to_string;
  1782. begin
  1783. second_string_to_string;
  1784. end;
  1785. procedure ttypeconvnode._second_cstring_to_pchar;
  1786. begin
  1787. second_cstring_to_pchar;
  1788. end;
  1789. procedure ttypeconvnode._second_string_to_chararray;
  1790. begin
  1791. second_string_to_chararray;
  1792. end;
  1793. procedure ttypeconvnode._second_array_to_pointer;
  1794. begin
  1795. second_array_to_pointer;
  1796. end;
  1797. procedure ttypeconvnode._second_pointer_to_array;
  1798. begin
  1799. second_pointer_to_array;
  1800. end;
  1801. procedure ttypeconvnode._second_chararray_to_string;
  1802. begin
  1803. second_chararray_to_string;
  1804. end;
  1805. procedure ttypeconvnode._second_char_to_string;
  1806. begin
  1807. second_char_to_string;
  1808. end;
  1809. procedure ttypeconvnode._second_int_to_real;
  1810. begin
  1811. second_int_to_real;
  1812. end;
  1813. procedure ttypeconvnode._second_real_to_real;
  1814. begin
  1815. second_real_to_real;
  1816. end;
  1817. procedure ttypeconvnode._second_cord_to_pointer;
  1818. begin
  1819. second_cord_to_pointer;
  1820. end;
  1821. procedure ttypeconvnode._second_proc_to_procvar;
  1822. begin
  1823. second_proc_to_procvar;
  1824. end;
  1825. procedure ttypeconvnode._second_bool_to_int;
  1826. begin
  1827. second_bool_to_int;
  1828. end;
  1829. procedure ttypeconvnode._second_int_to_bool;
  1830. begin
  1831. second_int_to_bool;
  1832. end;
  1833. procedure ttypeconvnode._second_bool_to_bool;
  1834. begin
  1835. second_bool_to_bool;
  1836. end;
  1837. procedure ttypeconvnode._second_load_smallset;
  1838. begin
  1839. second_load_smallset;
  1840. end;
  1841. procedure ttypeconvnode._second_ansistring_to_pchar;
  1842. begin
  1843. second_ansistring_to_pchar;
  1844. end;
  1845. procedure ttypeconvnode._second_class_to_intf;
  1846. begin
  1847. second_class_to_intf;
  1848. end;
  1849. procedure ttypeconvnode._second_char_to_char;
  1850. begin
  1851. second_char_to_char;
  1852. end;
  1853. procedure ttypeconvnode._second_nothing;
  1854. begin
  1855. second_nothing;
  1856. end;
  1857. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  1858. {$ifdef fpc}
  1859. const
  1860. secondconvert : array[tconverttype] of pointer = (
  1861. @_second_nothing, {equal}
  1862. @_second_nothing, {not_possible}
  1863. @_second_nothing, {second_string_to_string, handled in resulttype pass }
  1864. @_second_char_to_string,
  1865. @_second_nothing, {char_to_charray}
  1866. @_second_nothing, { pchar_to_string, handled in resulttype pass }
  1867. @_second_nothing, {cchar_to_pchar}
  1868. @_second_cstring_to_pchar,
  1869. @_second_ansistring_to_pchar,
  1870. @_second_string_to_chararray,
  1871. @_second_nothing, { chararray_to_string, handled in resulttype pass }
  1872. @_second_array_to_pointer,
  1873. @_second_pointer_to_array,
  1874. @_second_int_to_int,
  1875. @_second_int_to_bool,
  1876. @_second_bool_to_bool,
  1877. @_second_bool_to_int,
  1878. @_second_real_to_real,
  1879. @_second_int_to_real,
  1880. @_second_nothing, { real_to_currency, handled in resulttype pass }
  1881. @_second_proc_to_procvar,
  1882. @_second_nothing, { arrayconstructor_to_set }
  1883. @_second_nothing, { second_load_smallset, handled in first pass }
  1884. @_second_cord_to_pointer,
  1885. @_second_nothing, { interface 2 string }
  1886. @_second_nothing, { interface 2 guid }
  1887. @_second_class_to_intf,
  1888. @_second_char_to_char,
  1889. @_second_nothing, { normal_2_smallset }
  1890. @_second_nothing, { dynarray_2_openarray }
  1891. @_second_nothing, { pwchar_2_string }
  1892. @_second_nothing, { variant_2_dynarray }
  1893. @_second_nothing, { dynarray_2_variant}
  1894. @_second_nothing, { variant_2_enum }
  1895. @_second_nothing { enum_2_variant }
  1896. );
  1897. type
  1898. tprocedureofobject = procedure of object;
  1899. var
  1900. r : packed record
  1901. proc : pointer;
  1902. obj : pointer;
  1903. end;
  1904. begin
  1905. { this is a little bit dirty but it works }
  1906. { and should be quite portable too }
  1907. r.proc:=secondconvert[c];
  1908. r.obj:=self;
  1909. tprocedureofobject(r)();
  1910. end;
  1911. {$else fpc}
  1912. begin
  1913. case c of
  1914. tc_equal,
  1915. tc_not_possible,
  1916. tc_string_2_string : second_nothing;
  1917. tc_char_2_string : second_char_to_string;
  1918. tc_char_2_chararray : second_nothing;
  1919. tc_pchar_2_string : second_nothing;
  1920. tc_cchar_2_pchar : second_nothing;
  1921. tc_cstring_2_pchar : second_cstring_to_pchar;
  1922. tc_ansistring_2_pchar : second_ansistring_to_pchar;
  1923. tc_string_2_chararray : second_string_to_chararray;
  1924. tc_chararray_2_string : second_nothing;
  1925. tc_array_2_pointer : second_array_to_pointer;
  1926. tc_pointer_2_array : second_pointer_to_array;
  1927. tc_int_2_int : second_int_to_int;
  1928. tc_int_2_bool : second_int_to_bool;
  1929. tc_bool_2_bool : second_bool_to_bool;
  1930. tc_bool_2_int : second_bool_to_int;
  1931. tc_real_2_real : second_real_to_real;
  1932. tc_int_2_real : second_int_to_real;
  1933. tc_real_2_currency : second_nothing;
  1934. tc_proc_2_procvar : second_proc_to_procvar;
  1935. tc_arrayconstructor_2_set : second_nothing;
  1936. tc_load_smallset : second_nothing;
  1937. tc_cord_2_pointer : second_cord_to_pointer;
  1938. tc_intf_2_string : second_nothing;
  1939. tc_intf_2_guid : second_nothing;
  1940. tc_class_2_intf : second_class_to_intf;
  1941. tc_char_2_char : second_char_to_char;
  1942. tc_normal_2_smallset : second_nothing;
  1943. tc_dynarray_2_openarray : second_nothing;
  1944. tc_pwchar_2_string : second_nothing;
  1945. tc_variant_2_dynarray : second_nothing;
  1946. tc_dynarray_2_variant : second_nothing;
  1947. else internalerror(2002101101);
  1948. end;
  1949. end;
  1950. {$endif fpc}
  1951. {*****************************************************************************
  1952. TISNODE
  1953. *****************************************************************************}
  1954. constructor tisnode.create(l,r : tnode);
  1955. begin
  1956. inherited create(isn,l,r);
  1957. end;
  1958. function tisnode.det_resulttype:tnode;
  1959. var
  1960. paras: tcallparanode;
  1961. begin
  1962. result:=nil;
  1963. resulttypepass(left);
  1964. resulttypepass(right);
  1965. set_varstate(left,vs_used,true);
  1966. set_varstate(right,vs_used,true);
  1967. if codegenerror then
  1968. exit;
  1969. if (right.resulttype.def.deftype=classrefdef) then
  1970. begin
  1971. { left must be a class }
  1972. if is_class(left.resulttype.def) then
  1973. begin
  1974. { the operands must be related }
  1975. if (not(tobjectdef(left.resulttype.def).is_related(
  1976. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1977. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1978. tobjectdef(left.resulttype.def)))) then
  1979. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1980. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1981. end
  1982. else
  1983. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1984. { call fpc_do_is helper }
  1985. paras := ccallparanode.create(
  1986. left,
  1987. ccallparanode.create(
  1988. right,nil));
  1989. result := ccallnode.createintern('fpc_do_is',paras);
  1990. left := nil;
  1991. right := nil;
  1992. end
  1993. else if is_interface(right.resulttype.def) then
  1994. begin
  1995. { left is a class }
  1996. if is_class(left.resulttype.def) then
  1997. begin
  1998. { the operands must be related }
  1999. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2000. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2001. CGMessage2(type_e_classes_not_related,
  2002. FullTypeName(left.resulttype.def,right.resulttype.def),
  2003. FullTypeName(right.resulttype.def,left.resulttype.def))
  2004. end
  2005. { left is an interface }
  2006. else if is_interface(left.resulttype.def) then
  2007. begin
  2008. { the operands must be related }
  2009. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2010. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2011. CGMessage2(type_e_classes_not_related,
  2012. FullTypeName(left.resulttype.def,right.resulttype.def),
  2013. FullTypeName(right.resulttype.def,left.resulttype.def));
  2014. end
  2015. else
  2016. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2017. { call fpc_do_is helper }
  2018. paras := ccallparanode.create(
  2019. left,
  2020. ccallparanode.create(
  2021. right,nil));
  2022. result := ccallnode.createintern('fpc_do_is',paras);
  2023. left := nil;
  2024. right := nil;
  2025. end
  2026. else
  2027. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2028. resulttype:=booltype;
  2029. end;
  2030. function tisnode.pass_1 : tnode;
  2031. begin
  2032. internalerror(200204254);
  2033. result:=nil;
  2034. end;
  2035. { dummy pass_2, it will never be called, but we need one since }
  2036. { you can't instantiate an abstract class }
  2037. procedure tisnode.pass_2;
  2038. begin
  2039. end;
  2040. {*****************************************************************************
  2041. TASNODE
  2042. *****************************************************************************}
  2043. constructor tasnode.create(l,r : tnode);
  2044. begin
  2045. inherited create(asn,l,r);
  2046. call := nil;
  2047. end;
  2048. destructor tasnode.destroy;
  2049. begin
  2050. call.free;
  2051. inherited destroy;
  2052. end;
  2053. function tasnode.det_resulttype:tnode;
  2054. var
  2055. hp : tnode;
  2056. begin
  2057. result:=nil;
  2058. resulttypepass(right);
  2059. resulttypepass(left);
  2060. set_varstate(right,vs_used,true);
  2061. set_varstate(left,vs_used,true);
  2062. if codegenerror then
  2063. exit;
  2064. if (right.resulttype.def.deftype=classrefdef) then
  2065. begin
  2066. { left must be a class }
  2067. if is_class(left.resulttype.def) then
  2068. begin
  2069. { the operands must be related }
  2070. if (not(tobjectdef(left.resulttype.def).is_related(
  2071. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2072. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2073. tobjectdef(left.resulttype.def)))) then
  2074. CGMessage2(type_e_classes_not_related,
  2075. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2076. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2077. end
  2078. else
  2079. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2080. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2081. end
  2082. else if is_interface(right.resulttype.def) then
  2083. begin
  2084. { left is a class }
  2085. if not(is_class(left.resulttype.def) or
  2086. is_interface(left.resulttype.def)) then
  2087. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2088. resulttype:=right.resulttype;
  2089. { load the GUID of the interface }
  2090. if (right.nodetype=typen) then
  2091. begin
  2092. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2093. begin
  2094. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2095. right.free;
  2096. right:=hp;
  2097. end
  2098. else
  2099. internalerror(200206282);
  2100. resulttypepass(right);
  2101. end;
  2102. end
  2103. else
  2104. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2105. end;
  2106. function tasnode.getcopy: tnode;
  2107. begin
  2108. result := inherited getcopy;
  2109. if assigned(call) then
  2110. tasnode(result).call := call.getcopy
  2111. else
  2112. tasnode(result).call := nil;
  2113. end;
  2114. function tasnode.pass_1 : tnode;
  2115. var
  2116. procname: string;
  2117. begin
  2118. result:=nil;
  2119. if not assigned(call) then
  2120. begin
  2121. if is_class(left.resulttype.def) and
  2122. (right.resulttype.def.deftype=classrefdef) then
  2123. call := ccallnode.createinternres('fpc_do_as',
  2124. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2125. resulttype)
  2126. else
  2127. begin
  2128. if is_class(left.resulttype.def) then
  2129. procname := 'fpc_class_as_intf'
  2130. else
  2131. procname := 'fpc_intf_as';
  2132. call := ccallnode.createinternres(procname,
  2133. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2134. resulttype);
  2135. end;
  2136. left := nil;
  2137. right := nil;
  2138. firstpass(call);
  2139. if codegenerror then
  2140. exit;
  2141. expectloc:=call.expectloc;
  2142. registersint:=call.registersint;
  2143. registersfpu:=call.registersfpu;
  2144. {$ifdef SUPPORT_MMX}
  2145. registersmmx:=call.registersmmx;
  2146. {$endif SUPPORT_MMX}
  2147. end;
  2148. end;
  2149. begin
  2150. ctypeconvnode:=ttypeconvnode;
  2151. casnode:=tasnode;
  2152. cisnode:=tisnode;
  2153. end.
  2154. {
  2155. $Log$
  2156. Revision 1.163 2004-11-21 15:35:23 peter
  2157. * float routines all use internproc and compilerproc helpers
  2158. Revision 1.162 2004/11/02 20:15:53 jonas
  2159. * copy totype field in ttypeconvnode.getcopy()
  2160. Revision 1.161 2004/11/02 12:55:16 peter
  2161. * nf_internal flag for internal inserted typeconvs. This will
  2162. supress the generation of warning/hints
  2163. Revision 1.160 2004/11/01 23:30:11 peter
  2164. * support > 32bit accesses for x86_64
  2165. * rewrote array size checking to support 64bit
  2166. Revision 1.159 2004/11/01 17:15:47 peter
  2167. * no checkpointer code for dynarr to openarr
  2168. Revision 1.158 2004/11/01 15:31:58 peter
  2169. * -Or fix for absolute
  2170. Revision 1.157 2004/10/24 11:44:28 peter
  2171. * small regvar fixes
  2172. * loadref parameter removed from concatcopy,incrrefcount,etc
  2173. Revision 1.156 2004/10/15 09:14:17 mazen
  2174. - remove $IFDEF DELPHI and related code
  2175. - remove $IFDEF FPCPROCVAR and related code
  2176. Revision 1.155 2004/10/12 14:33:41 peter
  2177. * give error when converting class to interface are not related
  2178. Revision 1.154 2004/10/11 15:48:15 peter
  2179. * small regvar for para fixes
  2180. * function tvarsym.is_regvar added
  2181. * tvarsym.getvaluesize removed, use getsize instead
  2182. Revision 1.153 2004/09/26 17:45:30 peter
  2183. * simple regvar support, not yet finished
  2184. Revision 1.152 2004/08/08 16:00:56 florian
  2185. * constant floating point assignments etc. are now overflow checked
  2186. if Q+ or R+ is turned on
  2187. Revision 1.151 2004/06/29 20:57:50 peter
  2188. * fix pchar:=char
  2189. * fix longint(smallset)
  2190. Revision 1.150 2004/06/23 16:22:45 peter
  2191. * include unit name in error messages when types are the same
  2192. Revision 1.149 2004/06/20 08:55:29 florian
  2193. * logs truncated
  2194. Revision 1.148 2004/06/16 20:07:08 florian
  2195. * dwarf branch merged
  2196. Revision 1.147 2004/05/23 18:28:41 peter
  2197. * methodpointer is loaded into a temp when it was a calln
  2198. Revision 1.146 2004/05/23 15:03:40 peter
  2199. * some typeconvs don't allow assignment or passing to var para
  2200. Revision 1.145 2004/05/23 14:14:18 florian
  2201. + added set of widechar support (limited to 256 chars, is delphi compatible)
  2202. Revision 1.144 2004/04/29 19:56:37 daniel
  2203. * Prepare compiler infrastructure for multiple ansistring types
  2204. }