ncnv.pas 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  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,symppu,
  24. defutil,defcmp,
  25. nld
  26. {$ifdef Delphi}
  27. ,dmisc
  28. {$endif}
  29. ;
  30. type
  31. ttypeconvnode = class(tunarynode)
  32. totype : ttype;
  33. convtype : tconverttype;
  34. constructor create(node : tnode;const t : ttype);virtual;
  35. constructor create_explicit(node : tnode;const t : ttype);
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. function getcopy : tnode;override;
  41. function pass_1 : tnode;override;
  42. function det_resulttype:tnode;override;
  43. procedure mark_write;override;
  44. function docompare(p: tnode) : boolean; override;
  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. protected
  68. function first_int_to_int : tnode;virtual;
  69. function first_cstring_to_pchar : tnode;virtual;
  70. function first_string_to_chararray : tnode;virtual;
  71. function first_char_to_string : tnode;virtual;
  72. function first_nothing : tnode;virtual;
  73. function first_array_to_pointer : tnode;virtual;
  74. function first_int_to_real : tnode;virtual;
  75. function first_real_to_real : tnode;virtual;
  76. function first_pointer_to_array : tnode;virtual;
  77. function first_cchar_to_pchar : tnode;virtual;
  78. function first_bool_to_int : tnode;virtual;
  79. function first_int_to_bool : tnode;virtual;
  80. function first_bool_to_bool : tnode;virtual;
  81. function first_proc_to_procvar : tnode;virtual;
  82. function first_load_smallset : tnode;virtual;
  83. function first_cord_to_pointer : tnode;virtual;
  84. function first_ansistring_to_pchar : tnode;virtual;
  85. function first_arrayconstructor_to_set : tnode;virtual;
  86. function first_class_to_intf : tnode;virtual;
  87. function first_char_to_char : tnode;virtual;
  88. function first_call_helper(c : tconverttype) : tnode;
  89. { these wrapper are necessary, because the first_* stuff is called }
  90. { through a table. Without the wrappers override wouldn't have }
  91. { any effect }
  92. function _first_int_to_int : tnode;
  93. function _first_cstring_to_pchar : tnode;
  94. function _first_string_to_chararray : tnode;
  95. function _first_char_to_string : tnode;
  96. function _first_nothing : tnode;
  97. function _first_array_to_pointer : tnode;
  98. function _first_int_to_real : tnode;
  99. function _first_real_to_real: tnode;
  100. function _first_pointer_to_array : tnode;
  101. function _first_cchar_to_pchar : tnode;
  102. function _first_bool_to_int : tnode;
  103. function _first_int_to_bool : tnode;
  104. function _first_bool_to_bool : tnode;
  105. function _first_proc_to_procvar : tnode;
  106. function _first_load_smallset : tnode;
  107. function _first_cord_to_pointer : tnode;
  108. function _first_ansistring_to_pchar : tnode;
  109. function _first_arrayconstructor_to_set : tnode;
  110. function _first_class_to_intf : tnode;
  111. function _first_char_to_char : tnode;
  112. procedure second_int_to_int;virtual;abstract;
  113. procedure second_string_to_string;virtual;abstract;
  114. procedure second_cstring_to_pchar;virtual;abstract;
  115. procedure second_string_to_chararray;virtual;abstract;
  116. procedure second_array_to_pointer;virtual;abstract;
  117. procedure second_pointer_to_array;virtual;abstract;
  118. procedure second_chararray_to_string;virtual;abstract;
  119. procedure second_char_to_string;virtual;abstract;
  120. procedure second_int_to_real;virtual;abstract;
  121. procedure second_real_to_real;virtual;abstract;
  122. procedure second_cord_to_pointer;virtual;abstract;
  123. procedure second_proc_to_procvar;virtual;abstract;
  124. procedure second_bool_to_int;virtual;abstract;
  125. procedure second_int_to_bool;virtual;abstract;
  126. procedure second_bool_to_bool;virtual;abstract;
  127. procedure second_load_smallset;virtual;abstract;
  128. procedure second_ansistring_to_pchar;virtual;abstract;
  129. procedure second_class_to_intf;virtual;abstract;
  130. procedure second_char_to_char;virtual;abstract;
  131. procedure second_nothing; virtual;abstract;
  132. end;
  133. ttypeconvnodeclass = class of ttypeconvnode;
  134. tasnode = class(tbinarynode)
  135. constructor create(l,r : tnode);virtual;
  136. function pass_1 : tnode;override;
  137. function det_resulttype:tnode;override;
  138. function getcopy: tnode;override;
  139. destructor destroy; override;
  140. protected
  141. call: tnode;
  142. end;
  143. tasnodeclass = class of tasnode;
  144. tisnode = class(tbinarynode)
  145. constructor create(l,r : tnode);virtual;
  146. function pass_1 : tnode;override;
  147. function det_resulttype:tnode;override;
  148. procedure pass_2;override;
  149. end;
  150. tisnodeclass = class of tisnode;
  151. var
  152. ctypeconvnode : ttypeconvnodeclass;
  153. casnode : tasnodeclass;
  154. cisnode : tisnodeclass;
  155. procedure inserttypeconv(var p:tnode;const t:ttype);
  156. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  157. procedure arrayconstructor_to_set(var p : tnode);
  158. implementation
  159. uses
  160. globtype,systems,tokens,
  161. cutils,verbose,globals,widestr,
  162. symconst,symdef,symsym,symtable,
  163. ncon,ncal,nset,nadd,ninl,nmem,nmat,
  164. cgbase,procinfo,
  165. htypechk,pass_1,cpuinfo;
  166. {*****************************************************************************
  167. Helpers
  168. *****************************************************************************}
  169. procedure inserttypeconv(var p:tnode;const t:ttype);
  170. begin
  171. if not assigned(p.resulttype.def) then
  172. begin
  173. resulttypepass(p);
  174. if codegenerror then
  175. exit;
  176. end;
  177. { don't insert obsolete type conversions }
  178. if equal_defs(p.resulttype.def,t.def) and
  179. not ((p.resulttype.def.deftype=setdef) and
  180. (tsetdef(p.resulttype.def).settype <>
  181. tsetdef(t.def).settype)) then
  182. begin
  183. p.resulttype:=t;
  184. end
  185. else
  186. begin
  187. p:=ctypeconvnode.create(p,t);
  188. resulttypepass(p);
  189. end;
  190. end;
  191. procedure inserttypeconv_explicit(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_explicit(p,t);
  210. resulttypepass(p);
  211. end;
  212. end;
  213. {*****************************************************************************
  214. Array constructor to Set Conversion
  215. *****************************************************************************}
  216. procedure arrayconstructor_to_set(var p : tnode);
  217. var
  218. constp : tsetconstnode;
  219. buildp,
  220. p2,p3,p4 : tnode;
  221. htype : ttype;
  222. constset : Pconstset;
  223. constsetlo,
  224. constsethi : longint;
  225. procedure update_constsethi(t:ttype);
  226. begin
  227. if ((t.def.deftype=orddef) and
  228. (torddef(t.def).high>=constsethi)) then
  229. begin
  230. constsethi:=torddef(t.def).high;
  231. if htype.def=nil then
  232. begin
  233. if (constsethi>255) or
  234. (torddef(t.def).low<0) then
  235. htype:=u8bittype
  236. else
  237. htype:=t;
  238. end;
  239. if constsethi>255 then
  240. constsethi:=255;
  241. end
  242. else if ((t.def.deftype=enumdef) and
  243. (tenumdef(t.def).max>=constsethi)) then
  244. begin
  245. if htype.def=nil then
  246. htype:=t;
  247. constsethi:=tenumdef(t.def).max;
  248. end;
  249. end;
  250. procedure do_set(pos : longint);
  251. begin
  252. if (pos and not $ff)<>0 then
  253. Message(parser_e_illegal_set_expr);
  254. if pos>constsethi then
  255. constsethi:=pos;
  256. if pos<constsetlo then
  257. constsetlo:=pos;
  258. if pos in constset^ then
  259. Message(parser_e_illegal_set_expr);
  260. include(constset^,pos);
  261. end;
  262. var
  263. l : Longint;
  264. lr,hr : TConstExprInt;
  265. hp : tarrayconstructornode;
  266. begin
  267. if p.nodetype<>arrayconstructorn then
  268. internalerror(200205105);
  269. new(constset);
  270. constset^:=[];
  271. htype.reset;
  272. constsetlo:=0;
  273. constsethi:=0;
  274. constp:=csetconstnode.create(nil,htype);
  275. constp.value_set:=constset;
  276. buildp:=constp;
  277. hp:=tarrayconstructornode(p);
  278. if assigned(hp.left) then
  279. begin
  280. while assigned(hp) do
  281. begin
  282. p4:=nil; { will contain the tree to create the set }
  283. {split a range into p2 and p3 }
  284. if hp.left.nodetype=arrayconstructorrangen then
  285. begin
  286. p2:=tarrayconstructorrangenode(hp.left).left;
  287. p3:=tarrayconstructorrangenode(hp.left).right;
  288. tarrayconstructorrangenode(hp.left).left:=nil;
  289. tarrayconstructorrangenode(hp.left).right:=nil;
  290. end
  291. else
  292. begin
  293. p2:=hp.left;
  294. hp.left:=nil;
  295. p3:=nil;
  296. end;
  297. resulttypepass(p2);
  298. if assigned(p3) then
  299. resulttypepass(p3);
  300. if codegenerror then
  301. break;
  302. case p2.resulttype.def.deftype of
  303. enumdef,
  304. orddef:
  305. begin
  306. getrange(p2.resulttype.def,lr,hr);
  307. if assigned(p3) then
  308. begin
  309. { this isn't good, you'll get problems with
  310. type t010 = 0..10;
  311. ts = set of t010;
  312. var s : ts;b : t010
  313. begin s:=[1,2,b]; end.
  314. if is_integer(p3^.resulttype.def) then
  315. begin
  316. inserttypeconv(p3,u8bitdef);
  317. end;
  318. }
  319. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  320. begin
  321. aktfilepos:=p3.fileinfo;
  322. CGMessage(type_e_typeconflict_in_set);
  323. end
  324. else
  325. begin
  326. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  327. begin
  328. if not(is_integer(p3.resulttype.def)) then
  329. htype:=p3.resulttype
  330. else
  331. begin
  332. inserttypeconv(p3,u8bittype);
  333. inserttypeconv(p2,u8bittype);
  334. end;
  335. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  336. do_set(l);
  337. p2.free;
  338. p3.free;
  339. end
  340. else
  341. begin
  342. update_constsethi(p2.resulttype);
  343. inserttypeconv(p2,htype);
  344. update_constsethi(p3.resulttype);
  345. inserttypeconv(p3,htype);
  346. if assigned(htype.def) then
  347. inserttypeconv(p3,htype)
  348. else
  349. inserttypeconv(p3,u8bittype);
  350. p4:=csetelementnode.create(p2,p3);
  351. end;
  352. end;
  353. end
  354. else
  355. begin
  356. { Single value }
  357. if p2.nodetype=ordconstn then
  358. begin
  359. if not(is_integer(p2.resulttype.def)) then
  360. update_constsethi(p2.resulttype)
  361. else
  362. inserttypeconv(p2,u8bittype);
  363. do_set(tordconstnode(p2).value);
  364. p2.free;
  365. end
  366. else
  367. begin
  368. update_constsethi(p2.resulttype);
  369. if assigned(htype.def) then
  370. inserttypeconv(p2,htype)
  371. else
  372. inserttypeconv(p2,u8bittype);
  373. p4:=csetelementnode.create(p2,nil);
  374. end;
  375. end;
  376. end;
  377. stringdef :
  378. begin
  379. { if we've already set elements which are constants }
  380. { throw an error }
  381. if ((htype.def=nil) and assigned(buildp)) or
  382. not(is_char(htype.def)) then
  383. CGMessage(type_e_typeconflict_in_set)
  384. else
  385. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  386. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  387. if htype.def=nil then
  388. htype:=cchartype;
  389. p2.free;
  390. end;
  391. else
  392. CGMessage(type_e_ordinal_expr_expected);
  393. end;
  394. { insert the set creation tree }
  395. if assigned(p4) then
  396. buildp:=caddnode.create(addn,buildp,p4);
  397. { load next and dispose current node }
  398. p2:=hp;
  399. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  400. tarrayconstructornode(p2).right:=nil;
  401. p2.free;
  402. end;
  403. if (htype.def=nil) then
  404. htype:=u8bittype;
  405. end
  406. else
  407. begin
  408. { empty set [], only remove node }
  409. p.free;
  410. end;
  411. { set the initial set type }
  412. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  413. { determine the resulttype for the tree }
  414. resulttypepass(buildp);
  415. { set the new tree }
  416. p:=buildp;
  417. end;
  418. {*****************************************************************************
  419. TTYPECONVNODE
  420. *****************************************************************************}
  421. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  422. begin
  423. inherited create(typeconvn,node);
  424. convtype:=tc_not_possible;
  425. totype:=t;
  426. if t.def=nil then
  427. internalerror(200103281);
  428. set_file_line(node);
  429. end;
  430. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  431. begin
  432. self.create(node,t);
  433. include(flags,nf_explicit);
  434. end;
  435. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  436. begin
  437. inherited ppuload(t,ppufile);
  438. ppufile.gettype(totype);
  439. convtype:=tconverttype(ppufile.getbyte);
  440. end;
  441. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  442. begin
  443. inherited ppuwrite(ppufile);
  444. ppufile.puttype(totype);
  445. ppufile.putbyte(byte(convtype));
  446. end;
  447. procedure ttypeconvnode.buildderefimpl;
  448. begin
  449. inherited buildderefimpl;
  450. totype.buildderef;
  451. end;
  452. procedure ttypeconvnode.derefimpl;
  453. begin
  454. inherited derefimpl;
  455. totype.resolve;
  456. end;
  457. function ttypeconvnode.getcopy : tnode;
  458. var
  459. n : ttypeconvnode;
  460. begin
  461. n:=ttypeconvnode(inherited getcopy);
  462. n.convtype:=convtype;
  463. getcopy:=n;
  464. end;
  465. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  466. var
  467. t : tnode;
  468. begin
  469. result:=nil;
  470. if left.nodetype=ordconstn then
  471. begin
  472. { check if we have a valid pointer constant (JM) }
  473. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  474. if (sizeof(TConstPtrUInt) = 4) then
  475. begin
  476. if (tordconstnode(left).value < low(longint)) or
  477. (tordconstnode(left).value > high(cardinal)) then
  478. CGMessage(parser_e_range_check_error);
  479. end
  480. else if (sizeof(TConstPtrUInt) = 8) then
  481. begin
  482. if (tordconstnode(left).value < low(int64)) or
  483. (tordconstnode(left).value > high(qword)) then
  484. CGMessage(parser_e_range_check_error);
  485. end
  486. else
  487. internalerror(2001020801);
  488. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  489. result:=t;
  490. end
  491. else
  492. internalerror(200104023);
  493. end;
  494. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  495. begin
  496. result := ccallnode.createinternres(
  497. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  498. ccallparanode.create(left,nil),resulttype);
  499. left := nil;
  500. end;
  501. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  502. var
  503. arrsize: longint;
  504. begin
  505. with tarraydef(resulttype.def) do
  506. begin
  507. if highrange<lowrange then
  508. internalerror(75432653);
  509. arrsize := highrange-lowrange+1;
  510. end;
  511. if (left.nodetype = stringconstn) and
  512. { left.length+1 since there's always a terminating #0 character (JM) }
  513. (tstringconstnode(left).len+1 >= arrsize) and
  514. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  515. begin
  516. { handled separately }
  517. result := nil;
  518. exit;
  519. end;
  520. result := ccallnode.createinternres(
  521. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  522. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  523. cordconstnode.create(arrsize,s32bittype,true),nil)),resulttype);
  524. left := nil;
  525. end;
  526. function ttypeconvnode.resulttype_string_to_string : tnode;
  527. var
  528. procname: string[31];
  529. stringpara : tcallparanode;
  530. pw : pcompilerwidestring;
  531. pc : pchar;
  532. begin
  533. result:=nil;
  534. if left.nodetype=stringconstn then
  535. begin
  536. { convert ascii 2 unicode }
  537. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  538. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  539. begin
  540. initwidestring(pw);
  541. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  542. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  543. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  544. end
  545. else
  546. { convert unicode 2 ascii }
  547. if (tstringconstnode(left).st_type=st_widestring) and
  548. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  549. begin
  550. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  551. getmem(pc,getlengthwidestring(pw)+1);
  552. unicode2ascii(pw,pc);
  553. donewidestring(pw);
  554. tstringconstnode(left).value_str:=pc;
  555. end;
  556. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  557. tstringconstnode(left).resulttype:=resulttype;
  558. result:=left;
  559. left:=nil;
  560. end
  561. else
  562. begin
  563. { get the correct procedure name }
  564. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  565. '_to_'+tstringdef(resulttype.def).stringtypname;
  566. { create parameter (and remove left node from typeconvnode }
  567. { since it's reused as parameter) }
  568. stringpara := ccallparanode.create(left,nil);
  569. left := nil;
  570. { when converting to shortstrings, we have to pass high(destination) too }
  571. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  572. stringpara.right := ccallparanode.create(cinlinenode.create(
  573. in_high_x,false,self.getcopy),nil);
  574. { and create the callnode }
  575. result := ccallnode.createinternres(procname,stringpara,resulttype);
  576. end;
  577. end;
  578. function ttypeconvnode.resulttype_char_to_string : tnode;
  579. var
  580. procname: string[31];
  581. para : tcallparanode;
  582. hp : tstringconstnode;
  583. ws : pcompilerwidestring;
  584. begin
  585. result:=nil;
  586. if left.nodetype=ordconstn then
  587. begin
  588. if tstringdef(resulttype.def).string_typ=st_widestring then
  589. begin
  590. initwidestring(ws);
  591. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  592. hp:=cstringconstnode.createwstr(ws);
  593. donewidestring(ws);
  594. end
  595. else
  596. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  597. result:=hp;
  598. end
  599. else
  600. { shortstrings are handled 'inline' }
  601. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  602. begin
  603. { create the parameter }
  604. para := ccallparanode.create(left,nil);
  605. left := nil;
  606. { and the procname }
  607. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  608. { and finally the call }
  609. result := ccallnode.createinternres(procname,para,resulttype);
  610. end
  611. else
  612. begin
  613. { create word(byte(char) shl 8 or 1) for litte endian machines }
  614. { and word(byte(char) or 256) for big endian machines }
  615. left := ctypeconvnode.create_explicit(left,u8bittype);
  616. if (target_info.endian = endian_little) then
  617. left := caddnode.create(orn,
  618. cshlshrnode.create(shln,left,cordconstnode.create(8,s32bittype,false)),
  619. cordconstnode.create(1,s32bittype,false))
  620. else
  621. left := caddnode.create(orn,left,
  622. cordconstnode.create(1 shl 8,s32bittype,false));
  623. left := ctypeconvnode.create_explicit(left,u16bittype);
  624. resulttypepass(left);
  625. end;
  626. end;
  627. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  628. begin
  629. if resulttype.def.size <> 1 then
  630. begin
  631. { convert first to string, then to chararray }
  632. inserttypeconv(left,cshortstringtype);
  633. inserttypeconv(left,resulttype);
  634. result:=left;
  635. left := nil;
  636. exit;
  637. end;
  638. result := nil;
  639. end;
  640. function ttypeconvnode.resulttype_char_to_char : tnode;
  641. var
  642. hp : tordconstnode;
  643. begin
  644. result:=nil;
  645. if left.nodetype=ordconstn then
  646. begin
  647. if (torddef(resulttype.def).typ=uchar) and
  648. (torddef(left.resulttype.def).typ=uwidechar) then
  649. begin
  650. hp:=cordconstnode.create(
  651. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  652. cchartype,true);
  653. result:=hp;
  654. end
  655. else if (torddef(resulttype.def).typ=uwidechar) and
  656. (torddef(left.resulttype.def).typ=uchar) then
  657. begin
  658. hp:=cordconstnode.create(
  659. asciichar2unicode(chr(tordconstnode(left).value)),
  660. cwidechartype,true);
  661. result:=hp;
  662. end
  663. else
  664. internalerror(200105131);
  665. exit;
  666. end;
  667. end;
  668. function ttypeconvnode.resulttype_int_to_int : tnode;
  669. var
  670. v : TConstExprInt;
  671. begin
  672. result:=nil;
  673. if left.nodetype=ordconstn then
  674. begin
  675. v:=tordconstnode(left).value;
  676. if is_currency(resulttype.def) then
  677. v:=v*10000;
  678. if (resulttype.def.deftype=pointerdef) then
  679. result:=cpointerconstnode.create(v,resulttype)
  680. else
  681. begin
  682. if is_currency(left.resulttype.def) then
  683. v:=v div 10000;
  684. result:=cordconstnode.create(v,resulttype,false);
  685. end;
  686. end
  687. else if left.nodetype=pointerconstn then
  688. begin
  689. v:=tpointerconstnode(left).value;
  690. if (resulttype.def.deftype=pointerdef) then
  691. result:=cpointerconstnode.create(v,resulttype)
  692. else
  693. begin
  694. if is_currency(resulttype.def) then
  695. v:=v*10000;
  696. result:=cordconstnode.create(v,resulttype,false);
  697. end;
  698. end
  699. else
  700. begin
  701. { multiply by 10000 for currency. We need to use getcopy to pass
  702. the argument because the current node is always disposed. Only
  703. inserting the multiply in the left node is not possible because
  704. it'll get in an infinite loop to convert int->currency }
  705. if is_currency(resulttype.def) then
  706. begin
  707. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  708. include(result.flags,nf_is_currency);
  709. end
  710. else if is_currency(left.resulttype.def) then
  711. begin
  712. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  713. include(result.flags,nf_is_currency);
  714. end;
  715. end;
  716. end;
  717. function ttypeconvnode.resulttype_int_to_real : tnode;
  718. var
  719. rv : bestreal;
  720. begin
  721. result:=nil;
  722. if left.nodetype=ordconstn then
  723. begin
  724. rv:=tordconstnode(left).value;
  725. if is_currency(resulttype.def) then
  726. rv:=rv*10000.0
  727. else if is_currency(left.resulttype.def) then
  728. rv:=rv/10000.0;
  729. result:=crealconstnode.create(rv,resulttype);
  730. end
  731. else
  732. begin
  733. { multiply by 10000 for currency. We need to use getcopy to pass
  734. the argument because the current node is always disposed. Only
  735. inserting the multiply in the left node is not possible because
  736. it'll get in an infinite loop to convert int->currency }
  737. if is_currency(resulttype.def) then
  738. begin
  739. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  740. include(result.flags,nf_is_currency);
  741. end
  742. else if is_currency(left.resulttype.def) then
  743. begin
  744. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  745. include(result.flags,nf_is_currency);
  746. end;
  747. end;
  748. end;
  749. function ttypeconvnode.resulttype_real_to_currency : tnode;
  750. begin
  751. if not is_currency(resulttype.def) then
  752. internalerror(200304221);
  753. result:=nil;
  754. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  755. include(left.flags,nf_is_currency);
  756. resulttypepass(left);
  757. { Convert constants directly, else call Round() }
  758. if left.nodetype=realconstn then
  759. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  760. else
  761. result:=ccallnode.createinternres('fpc_round',
  762. ccallparanode.create(left,nil),resulttype);
  763. left:=nil;
  764. end;
  765. function ttypeconvnode.resulttype_real_to_real : tnode;
  766. begin
  767. result:=nil;
  768. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  769. begin
  770. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  771. include(left.flags,nf_is_currency);
  772. resulttypepass(left);
  773. end
  774. else
  775. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  776. begin
  777. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  778. include(left.flags,nf_is_currency);
  779. resulttypepass(left);
  780. end;
  781. if left.nodetype=realconstn then
  782. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  783. end;
  784. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  785. begin
  786. result:=nil;
  787. if is_pwidechar(resulttype.def) then
  788. inserttypeconv(left,cwidestringtype)
  789. else
  790. inserttypeconv(left,cshortstringtype);
  791. { evaluate again, reset resulttype so the convert_typ
  792. will be calculated again and cstring_to_pchar will
  793. be used for futher conversion }
  794. result:=det_resulttype;
  795. end;
  796. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  797. begin
  798. result:=nil;
  799. if is_pwidechar(resulttype.def) then
  800. inserttypeconv(left,cwidestringtype);
  801. end;
  802. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  803. var
  804. hp : tnode;
  805. begin
  806. result:=nil;
  807. if left.nodetype<>arrayconstructorn then
  808. internalerror(5546);
  809. { remove typeconv node }
  810. hp:=left;
  811. left:=nil;
  812. { create a set constructor tree }
  813. arrayconstructor_to_set(hp);
  814. result:=hp;
  815. end;
  816. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  817. begin
  818. result := ccallnode.createinternres(
  819. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  820. ccallparanode.create(left,nil),resulttype);
  821. left := nil;
  822. end;
  823. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  824. begin
  825. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  826. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  827. end;
  828. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  829. begin
  830. { a dynamic array is a pointer to an array, so to convert it to }
  831. { an open array, we have to dereference it (JM) }
  832. result := ctypeconvnode.create_explicit(left,voidpointertype);
  833. resulttypepass(result);
  834. { left is reused }
  835. left := nil;
  836. result := cderefnode.create(result);
  837. result.resulttype := resulttype;
  838. end;
  839. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  840. begin
  841. result := ccallnode.createinternres(
  842. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  843. ccallparanode.create(left,nil),resulttype);
  844. left := nil;
  845. end;
  846. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  847. begin
  848. result := ccallnode.createinternres(
  849. 'fpc_variant_to_dynarray',
  850. ccallparanode.create(caddrnode.create(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  851. ccallparanode.create(left,nil)
  852. ),resulttype);
  853. left := nil;
  854. end;
  855. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  856. begin
  857. result:=nil;
  858. end;
  859. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  860. {$ifdef fpc}
  861. const
  862. resulttypeconvert : array[tconverttype] of pointer = (
  863. {equal} nil,
  864. {not_possible} nil,
  865. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  866. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  867. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  868. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  869. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  870. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  871. { ansistring_2_pchar } nil,
  872. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  873. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  874. { array_2_pointer } nil,
  875. { pointer_2_array } nil,
  876. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  877. { int_2_bool } nil,
  878. { bool_2_bool } nil,
  879. { bool_2_int } nil,
  880. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  881. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  882. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  883. { proc_2_procvar } nil,
  884. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  885. { load_smallset } nil,
  886. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  887. { intf_2_string } nil,
  888. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  889. { class_2_intf } nil,
  890. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  891. { normal_2_smallset} nil,
  892. { dynarray_2_openarray} @resulttype_dynarray_to_openarray,
  893. { pwchar_2_string} @resulttype_pwchar_to_string,
  894. { variant_2_dynarray} @resulttype_variant_to_dynarray,
  895. { dynarray_2_variant} @resulttype_dynarray_to_variant
  896. );
  897. type
  898. tprocedureofobject = function : tnode of object;
  899. var
  900. r : packed record
  901. proc : pointer;
  902. obj : pointer;
  903. end;
  904. begin
  905. result:=nil;
  906. { this is a little bit dirty but it works }
  907. { and should be quite portable too }
  908. r.proc:=resulttypeconvert[c];
  909. r.obj:=self;
  910. if assigned(r.proc) then
  911. result:=tprocedureofobject(r)();
  912. end;
  913. {$else}
  914. begin
  915. case c of
  916. tc_string_2_string: resulttype_string_to_string;
  917. tc_char_2_string : resulttype_char_to_string;
  918. tc_char_2_chararray: resulttype_char_to_chararray;
  919. tc_pchar_2_string : resulttype_pchar_to_string;
  920. tc_cchar_2_pchar : resulttype_cchar_to_pchar;
  921. tc_cstring_2_pchar : resulttype_cstring_to_pchar;
  922. tc_string_2_chararray : resulttype_string_to_chararray;
  923. tc_chararray_2_string : resulttype_chararray_to_string;
  924. tc_real_2_real : resulttype_real_to_real;
  925. tc_int_2_real : resulttype_int_to_real;
  926. tc_real_2_currency : resulttype_real_to_currency;
  927. tc_arrayconstructor_2_set : resulttype_arrayconstructor_to_set;
  928. tc_cord_2_pointer : resulttype_cord_to_pointer;
  929. tc_intf_2_guid : resulttype_interface_to_guid;
  930. tc_char_2_char : resulttype_char_to_char;
  931. tc_dynarray_2_openarray : resulttype_dynarray_to_openarray;
  932. tc_pwchar_2_string : resulttype_pwchar_to_string;
  933. tc_variant_2_dynarray : resulttype_variant_to_dynarray;
  934. tc_dynarray_2_variant : resulttype_dynarray_to_variant;
  935. end;
  936. end;
  937. {$Endif fpc}
  938. function ttypeconvnode.det_resulttype:tnode;
  939. var
  940. hp : tnode;
  941. currprocdef,
  942. aprocdef : tprocdef;
  943. eq : tequaltype;
  944. begin
  945. result:=nil;
  946. resulttype:=totype;
  947. resulttypepass(left);
  948. if codegenerror then
  949. exit;
  950. { When absolute force tc_equal }
  951. if (nf_absolute in flags) then
  952. begin
  953. convtype:=tc_equal;
  954. exit;
  955. end;
  956. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,
  957. nf_explicit in flags,true,convtype,aprocdef);
  958. case eq of
  959. te_exact,
  960. te_equal :
  961. begin
  962. { because is_equal only checks the basetype for sets we need to
  963. check here if we are loading a smallset into a normalset }
  964. if (resulttype.def.deftype=setdef) and
  965. (left.resulttype.def.deftype=setdef) and
  966. ((tsetdef(resulttype.def).settype = smallset) xor
  967. (tsetdef(left.resulttype.def).settype = smallset)) then
  968. begin
  969. { constant sets can be converted by changing the type only }
  970. if (left.nodetype=setconstn) then
  971. begin
  972. left.resulttype:=resulttype;
  973. result:=left;
  974. left:=nil;
  975. exit;
  976. end;
  977. if (tsetdef(resulttype.def).settype <> smallset) then
  978. convtype:=tc_load_smallset
  979. else
  980. convtype := tc_normal_2_smallset;
  981. exit;
  982. end
  983. else
  984. begin
  985. { Only leave when there is no conversion to do.
  986. We can still need to call a conversion routine,
  987. like the routine to convert a stringconstnode }
  988. if convtype in [tc_equal,tc_not_possible] then
  989. begin
  990. left.resulttype:=resulttype;
  991. result:=left;
  992. left:=nil;
  993. exit;
  994. end;
  995. end;
  996. end;
  997. te_convert_l1,
  998. te_convert_l2,
  999. te_convert_l3 :
  1000. begin
  1001. { nothing to do }
  1002. end;
  1003. te_convert_operator :
  1004. begin
  1005. include(current_procinfo.flags,pi_do_call);
  1006. inc(overloaded_operators[_assignment].refs);
  1007. hp:=ccallnode.create(ccallparanode.create(left,nil),
  1008. overloaded_operators[_assignment],nil,nil);
  1009. { tell explicitly which def we must use !! (PM) }
  1010. tcallnode(hp).procdefinition:=aprocdef;
  1011. left:=nil;
  1012. result:=hp;
  1013. exit;
  1014. end;
  1015. te_incompatible :
  1016. begin
  1017. { Procedures have a resulttype.def of voiddef and functions of their
  1018. own resulttype.def. They will therefore always be incompatible with
  1019. a procvar. Because isconvertable cannot check for procedures we
  1020. use an extra check for them.}
  1021. if (m_tp_procvar in aktmodeswitches) and
  1022. (resulttype.def.deftype=procvardef) then
  1023. begin
  1024. if is_procsym_load(left) then
  1025. begin
  1026. if (left.nodetype<>addrn) then
  1027. begin
  1028. convtype:=tc_proc_2_procvar;
  1029. { Now check if the procedure we are going to assign to
  1030. the procvar, is compatible with the procvar's type }
  1031. if not(nf_explicit in flags) and
  1032. (proc_to_procvar_equal(tprocsym(tloadnode(left).symtableentry).first_procdef,
  1033. tprocvardef(resulttype.def),true)=te_incompatible) then
  1034. IncompatibleTypes(tprocsym(tloadnode(left).symtableentry).first_procdef,resulttype.def);
  1035. exit;
  1036. end;
  1037. end
  1038. else
  1039. if (left.nodetype=calln) and
  1040. (tcallnode(left).para_count=0) then
  1041. begin
  1042. if assigned(tcallnode(left).right) then
  1043. begin
  1044. hp:=tcallnode(left).right.getcopy;
  1045. currprocdef:=tprocdef(hp.resulttype.def);
  1046. end
  1047. else
  1048. begin
  1049. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1050. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1051. currprocdef,tcallnode(left).symtableproc);
  1052. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1053. begin
  1054. if assigned(tcallnode(left).methodpointer) then
  1055. begin
  1056. { Under certain circumstances the methodpointer is a loadvmtaddrn
  1057. which isn't possible if it is used as a method pointer, so
  1058. fix this.
  1059. If you change this, ensure that tests/tbs/tw2669.pp still works }
  1060. if tcallnode(left).methodpointer.nodetype=loadvmtaddrn then
  1061. tloadnode(hp).set_mp(tloadvmtaddrnode(tcallnode(left).methodpointer).left.getcopy)
  1062. else
  1063. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  1064. end
  1065. else
  1066. tloadnode(hp).set_mp(load_self_node);
  1067. end;
  1068. resulttypepass(hp);
  1069. end;
  1070. left.free;
  1071. left:=hp;
  1072. convtype:=tc_proc_2_procvar;
  1073. { Now check if the procedure we are going to assign to
  1074. the procvar, is compatible with the procvar's type }
  1075. if not(nf_explicit in flags) and
  1076. (proc_to_procvar_equal(currprocdef,
  1077. tprocvardef(resulttype.def),true)=te_incompatible) then
  1078. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1079. exit;
  1080. end;
  1081. end;
  1082. { Handle explicit type conversions }
  1083. if nf_explicit in flags then
  1084. begin
  1085. { do common tc_equal cast }
  1086. convtype:=tc_equal;
  1087. { check if the result could be in a register }
  1088. if (not(tstoreddef(resulttype.def).is_intregable) and
  1089. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1090. ((left.resulttype.def.deftype = floatdef) and
  1091. (resulttype.def.deftype <> floatdef)) then
  1092. make_not_regable(left);
  1093. { class to class or object to object, with checkobject support }
  1094. if (resulttype.def.deftype=objectdef) and
  1095. (left.resulttype.def.deftype=objectdef) then
  1096. begin
  1097. if (cs_check_object in aktlocalswitches) then
  1098. begin
  1099. if is_class_or_interface(resulttype.def) then
  1100. begin
  1101. { we can translate the typeconvnode to 'as' when
  1102. typecasting to a class or interface }
  1103. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1104. left:=nil;
  1105. result:=hp;
  1106. exit;
  1107. end;
  1108. end
  1109. else
  1110. begin
  1111. { check if the types are related }
  1112. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1113. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1114. CGMessage2(type_w_classes_not_related,left.resulttype.def.typename,resulttype.def.typename);
  1115. end;
  1116. end
  1117. else
  1118. begin
  1119. { only if the same size or formal def }
  1120. if not(
  1121. (left.resulttype.def.deftype=formaldef) or
  1122. (
  1123. not(is_open_array(left.resulttype.def)) and
  1124. (left.resulttype.def.size=resulttype.def.size)
  1125. ) or
  1126. (
  1127. is_void(left.resulttype.def) and
  1128. (left.nodetype=derefn)
  1129. )
  1130. ) then
  1131. CGMessage(cg_e_illegal_type_conversion);
  1132. end;
  1133. end
  1134. else
  1135. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1136. end;
  1137. else
  1138. internalerror(200211231);
  1139. end;
  1140. { Give hint for unportable code }
  1141. if ((left.resulttype.def.deftype=orddef) and
  1142. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1143. ((resulttype.def.deftype=orddef) and
  1144. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) then
  1145. CGMessage(cg_h_pointer_to_longint_conv_not_portable);
  1146. { Constant folding and other node transitions to
  1147. remove the typeconv node }
  1148. case left.nodetype of
  1149. loadn :
  1150. begin
  1151. { tp7 procvar support, when right is not a procvardef and we got a
  1152. loadn of a procvar (ignore procedures as void can not be converted)
  1153. then convert to a calln, the check for the result is already done
  1154. in is_convertible, also no conflict with @procvar is here because
  1155. that has an extra addrn.
  1156. The following deftypes always access the procvar: recorddef,setdef. This
  1157. has been tested with Kylix using trial and error }
  1158. if (m_tp_procvar in aktmodeswitches) and
  1159. (resulttype.def.deftype<>procvardef) and
  1160. { ignore internal typecasts to access methodpointer fields }
  1161. not(resulttype.def.deftype in [recorddef,setdef]) and
  1162. (left.resulttype.def.deftype=procvardef) and
  1163. (not is_void(tprocvardef(left.resulttype.def).rettype.def)) then
  1164. begin
  1165. hp:=ccallnode.create_procvar(nil,left);
  1166. resulttypepass(hp);
  1167. left:=hp;
  1168. end;
  1169. end;
  1170. niln :
  1171. begin
  1172. { nil to ordinal node }
  1173. if (resulttype.def.deftype=orddef) then
  1174. begin
  1175. hp:=cordconstnode.create(0,resulttype,true);
  1176. result:=hp;
  1177. exit;
  1178. end
  1179. else
  1180. { fold nil to any pointer type }
  1181. if (resulttype.def.deftype=pointerdef) then
  1182. begin
  1183. hp:=cnilnode.create;
  1184. hp.resulttype:=resulttype;
  1185. result:=hp;
  1186. exit;
  1187. end
  1188. else
  1189. { remove typeconv after niln, but not when the result is a
  1190. methodpointer. The typeconv of the methodpointer will then
  1191. take care of updateing size of niln to OS_64 }
  1192. if not((resulttype.def.deftype=procvardef) and
  1193. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1194. begin
  1195. left.resulttype:=resulttype;
  1196. result:=left;
  1197. left:=nil;
  1198. exit;
  1199. end;
  1200. end;
  1201. ordconstn :
  1202. begin
  1203. { ordinal contants can be directly converted }
  1204. { but not char to char because it is a widechar to char or via versa }
  1205. { which needs extra code to do the code page transistion }
  1206. if is_ordinal(resulttype.def) and
  1207. not(convtype=tc_char_2_char) then
  1208. begin
  1209. { replace the resulttype and recheck the range }
  1210. left.resulttype:=resulttype;
  1211. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1212. result:=left;
  1213. left:=nil;
  1214. exit;
  1215. end;
  1216. end;
  1217. pointerconstn :
  1218. begin
  1219. { pointerconstn to any pointer is folded too }
  1220. if (resulttype.def.deftype=pointerdef) then
  1221. begin
  1222. left.resulttype:=resulttype;
  1223. result:=left;
  1224. left:=nil;
  1225. exit;
  1226. end
  1227. { constant pointer to ordinal }
  1228. else if is_ordinal(resulttype.def) then
  1229. begin
  1230. hp:=cordconstnode.create(tpointerconstnode(left).value,
  1231. resulttype,true);
  1232. result:=hp;
  1233. exit;
  1234. end;
  1235. end;
  1236. end;
  1237. { now call the resulttype helper to do constant folding }
  1238. result:=resulttype_call_helper(convtype);
  1239. end;
  1240. procedure Ttypeconvnode.mark_write;
  1241. begin
  1242. left.mark_write;
  1243. end;
  1244. function ttypeconvnode.first_cord_to_pointer : tnode;
  1245. begin
  1246. result:=nil;
  1247. internalerror(200104043);
  1248. end;
  1249. function ttypeconvnode.first_int_to_int : tnode;
  1250. begin
  1251. first_int_to_int:=nil;
  1252. if (left.expectloc<>LOC_REGISTER) and
  1253. not is_void(left.resulttype.def) and
  1254. (resulttype.def.size>left.resulttype.def.size) then
  1255. expectloc:=LOC_REGISTER
  1256. else
  1257. expectloc:=left.expectloc;
  1258. if is_64bit(resulttype.def) then
  1259. registers32:=max(registers32,2)
  1260. else
  1261. registers32:=max(registers32,1);
  1262. end;
  1263. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1264. begin
  1265. first_cstring_to_pchar:=nil;
  1266. registers32:=1;
  1267. expectloc:=LOC_REGISTER;
  1268. end;
  1269. function ttypeconvnode.first_string_to_chararray : tnode;
  1270. begin
  1271. first_string_to_chararray:=nil;
  1272. expectloc:=left.expectloc;
  1273. end;
  1274. function ttypeconvnode.first_char_to_string : tnode;
  1275. begin
  1276. first_char_to_string:=nil;
  1277. expectloc:=LOC_REFERENCE;
  1278. end;
  1279. function ttypeconvnode.first_nothing : tnode;
  1280. begin
  1281. first_nothing:=nil;
  1282. end;
  1283. function ttypeconvnode.first_array_to_pointer : tnode;
  1284. begin
  1285. first_array_to_pointer:=nil;
  1286. if registers32<1 then
  1287. registers32:=1;
  1288. expectloc:=LOC_REGISTER;
  1289. end;
  1290. function ttypeconvnode.first_int_to_real: tnode;
  1291. var
  1292. fname: string[32];
  1293. typname : string[12];
  1294. begin
  1295. { Get the type name }
  1296. { Normally the typename should be one of the following:
  1297. single, double - carl
  1298. }
  1299. typname := lower(pbestrealtype^.def.gettypename);
  1300. { converting a 64bit integer to a float requires a helper }
  1301. if is_64bit(left.resulttype.def) then
  1302. begin
  1303. if is_signed(left.resulttype.def) then
  1304. fname := 'fpc_int64_to_'+typname
  1305. else
  1306. {$warning generic conversion from int to float does not support unsigned integers}
  1307. fname := 'fpc_int64_to_'+typname;
  1308. result := ccallnode.createintern(fname,ccallparanode.create(
  1309. left,nil));
  1310. left:=nil;
  1311. firstpass(result);
  1312. exit;
  1313. end
  1314. else
  1315. { other integers are supposed to be 32 bit }
  1316. begin
  1317. {$warning generic conversion from int to float does not support unsigned integers}
  1318. if is_signed(left.resulttype.def) then
  1319. fname := 'fpc_longint_to_'+typname
  1320. else
  1321. fname := 'fpc_longint_to_'+typname;
  1322. result := ccallnode.createintern(fname,ccallparanode.create(
  1323. left,nil));
  1324. left:=nil;
  1325. firstpass(result);
  1326. exit;
  1327. end;
  1328. end;
  1329. function ttypeconvnode.first_real_to_real : tnode;
  1330. begin
  1331. first_real_to_real:=nil;
  1332. { comp isn't a floating type }
  1333. {$ifdef i386}
  1334. if (tfloatdef(resulttype.def).typ=s64comp) and
  1335. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1336. not (nf_explicit in flags) then
  1337. CGMessage(type_w_convert_real_2_comp);
  1338. {$endif}
  1339. if registersfpu<1 then
  1340. registersfpu:=1;
  1341. expectloc:=LOC_FPUREGISTER;
  1342. end;
  1343. function ttypeconvnode.first_pointer_to_array : tnode;
  1344. begin
  1345. first_pointer_to_array:=nil;
  1346. if registers32<1 then
  1347. registers32:=1;
  1348. expectloc:=LOC_REFERENCE;
  1349. end;
  1350. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1351. begin
  1352. first_cchar_to_pchar:=nil;
  1353. internalerror(200104021);
  1354. end;
  1355. function ttypeconvnode.first_bool_to_int : tnode;
  1356. begin
  1357. first_bool_to_int:=nil;
  1358. { byte(boolean) or word(wordbool) or longint(longbool) must
  1359. be accepted for var parameters }
  1360. if (nf_explicit in flags) and
  1361. (left.resulttype.def.size=resulttype.def.size) and
  1362. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1363. exit;
  1364. { when converting to 64bit, first convert to a 32bit int and then }
  1365. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1366. if resulttype.def.size > sizeof(aword) then
  1367. begin
  1368. result := ctypeconvnode.create_explicit(left,u32bittype);
  1369. result := ctypeconvnode.create(result,resulttype);
  1370. left := nil;
  1371. firstpass(result);
  1372. exit;
  1373. end;
  1374. expectloc:=LOC_REGISTER;
  1375. if registers32<1 then
  1376. registers32:=1;
  1377. end;
  1378. function ttypeconvnode.first_int_to_bool : tnode;
  1379. begin
  1380. first_int_to_bool:=nil;
  1381. { byte(boolean) or word(wordbool) or longint(longbool) must
  1382. be accepted for var parameters }
  1383. if (nf_explicit in flags) and
  1384. (left.resulttype.def.size=resulttype.def.size) and
  1385. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1386. exit;
  1387. expectloc:=LOC_REGISTER;
  1388. { need if bool to bool !!
  1389. not very nice !!
  1390. insertypeconv(left,s32bittype);
  1391. left.explizit:=true;
  1392. firstpass(left); }
  1393. if registers32<1 then
  1394. registers32:=1;
  1395. end;
  1396. function ttypeconvnode.first_bool_to_bool : tnode;
  1397. begin
  1398. first_bool_to_bool:=nil;
  1399. expectloc:=LOC_REGISTER;
  1400. if registers32<1 then
  1401. registers32:=1;
  1402. end;
  1403. function ttypeconvnode.first_char_to_char : tnode;
  1404. begin
  1405. first_char_to_char:=first_int_to_int;
  1406. end;
  1407. function ttypeconvnode.first_proc_to_procvar : tnode;
  1408. begin
  1409. first_proc_to_procvar:=nil;
  1410. if assigned(tunarynode(left).left) then
  1411. begin
  1412. if (left.expectloc<>LOC_CREFERENCE) then
  1413. CGMessage(cg_e_illegal_expression);
  1414. registers32:=left.registers32;
  1415. expectloc:=left.expectloc
  1416. end
  1417. else
  1418. begin
  1419. registers32:=left.registers32;
  1420. if registers32<1 then
  1421. registers32:=1;
  1422. expectloc:=LOC_REGISTER;
  1423. end
  1424. end;
  1425. function ttypeconvnode.first_load_smallset : tnode;
  1426. var
  1427. srsym: ttypesym;
  1428. p: tcallparanode;
  1429. begin
  1430. if not searchsystype('FPC_SMALL_SET',srsym) then
  1431. internalerror(200108313);
  1432. p := ccallparanode.create(left,nil);
  1433. { reused }
  1434. left := nil;
  1435. { convert parameter explicitely to fpc_small_set }
  1436. p.left := ctypeconvnode.create_explicit(p.left,srsym.restype);
  1437. { create call, adjust resulttype }
  1438. result :=
  1439. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1440. firstpass(result);
  1441. end;
  1442. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1443. begin
  1444. first_ansistring_to_pchar:=nil;
  1445. expectloc:=LOC_REGISTER;
  1446. if registers32<1 then
  1447. registers32:=1;
  1448. end;
  1449. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1450. begin
  1451. first_arrayconstructor_to_set:=nil;
  1452. internalerror(200104022);
  1453. end;
  1454. function ttypeconvnode.first_class_to_intf : tnode;
  1455. begin
  1456. first_class_to_intf:=nil;
  1457. expectloc:=LOC_REGISTER;
  1458. if registers32<1 then
  1459. registers32:=1;
  1460. end;
  1461. function ttypeconvnode._first_int_to_int : tnode;
  1462. begin
  1463. result:=first_int_to_int;
  1464. end;
  1465. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1466. begin
  1467. result:=first_cstring_to_pchar;
  1468. end;
  1469. function ttypeconvnode._first_string_to_chararray : tnode;
  1470. begin
  1471. result:=first_string_to_chararray;
  1472. end;
  1473. function ttypeconvnode._first_char_to_string : tnode;
  1474. begin
  1475. result:=first_char_to_string;
  1476. end;
  1477. function ttypeconvnode._first_nothing : tnode;
  1478. begin
  1479. result:=first_nothing;
  1480. end;
  1481. function ttypeconvnode._first_array_to_pointer : tnode;
  1482. begin
  1483. result:=first_array_to_pointer;
  1484. end;
  1485. function ttypeconvnode._first_int_to_real : tnode;
  1486. begin
  1487. result:=first_int_to_real;
  1488. end;
  1489. function ttypeconvnode._first_real_to_real : tnode;
  1490. begin
  1491. result:=first_real_to_real;
  1492. end;
  1493. function ttypeconvnode._first_pointer_to_array : tnode;
  1494. begin
  1495. result:=first_pointer_to_array;
  1496. end;
  1497. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1498. begin
  1499. result:=first_cchar_to_pchar;
  1500. end;
  1501. function ttypeconvnode._first_bool_to_int : tnode;
  1502. begin
  1503. result:=first_bool_to_int;
  1504. end;
  1505. function ttypeconvnode._first_int_to_bool : tnode;
  1506. begin
  1507. result:=first_int_to_bool;
  1508. end;
  1509. function ttypeconvnode._first_bool_to_bool : tnode;
  1510. begin
  1511. result:=first_bool_to_bool;
  1512. end;
  1513. function ttypeconvnode._first_proc_to_procvar : tnode;
  1514. begin
  1515. result:=first_proc_to_procvar;
  1516. end;
  1517. function ttypeconvnode._first_load_smallset : tnode;
  1518. begin
  1519. result:=first_load_smallset;
  1520. end;
  1521. function ttypeconvnode._first_cord_to_pointer : tnode;
  1522. begin
  1523. result:=first_cord_to_pointer;
  1524. end;
  1525. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1526. begin
  1527. result:=first_ansistring_to_pchar;
  1528. end;
  1529. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1530. begin
  1531. result:=first_arrayconstructor_to_set;
  1532. end;
  1533. function ttypeconvnode._first_class_to_intf : tnode;
  1534. begin
  1535. result:=first_class_to_intf;
  1536. end;
  1537. function ttypeconvnode._first_char_to_char : tnode;
  1538. begin
  1539. result:=first_char_to_char;
  1540. end;
  1541. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1542. const
  1543. firstconvert : array[tconverttype] of pointer = (
  1544. @ttypeconvnode._first_nothing, {equal}
  1545. @ttypeconvnode._first_nothing, {not_possible}
  1546. nil, { removed in resulttype_string_to_string }
  1547. @ttypeconvnode._first_char_to_string,
  1548. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1549. nil, { removed in resulttype_chararray_to_string }
  1550. @ttypeconvnode._first_cchar_to_pchar,
  1551. @ttypeconvnode._first_cstring_to_pchar,
  1552. @ttypeconvnode._first_ansistring_to_pchar,
  1553. @ttypeconvnode._first_string_to_chararray,
  1554. nil, { removed in resulttype_chararray_to_string }
  1555. @ttypeconvnode._first_array_to_pointer,
  1556. @ttypeconvnode._first_pointer_to_array,
  1557. @ttypeconvnode._first_int_to_int,
  1558. @ttypeconvnode._first_int_to_bool,
  1559. @ttypeconvnode._first_bool_to_bool,
  1560. @ttypeconvnode._first_bool_to_int,
  1561. @ttypeconvnode._first_real_to_real,
  1562. @ttypeconvnode._first_int_to_real,
  1563. nil, { removed in resulttype_real_to_currency }
  1564. @ttypeconvnode._first_proc_to_procvar,
  1565. @ttypeconvnode._first_arrayconstructor_to_set,
  1566. @ttypeconvnode._first_load_smallset,
  1567. @ttypeconvnode._first_cord_to_pointer,
  1568. @ttypeconvnode._first_nothing,
  1569. @ttypeconvnode._first_nothing,
  1570. @ttypeconvnode._first_class_to_intf,
  1571. @ttypeconvnode._first_char_to_char,
  1572. @ttypeconvnode._first_nothing,
  1573. @ttypeconvnode._first_nothing,
  1574. nil,
  1575. nil,
  1576. nil
  1577. );
  1578. type
  1579. tprocedureofobject = function : tnode of object;
  1580. var
  1581. r : packed record
  1582. proc : pointer;
  1583. obj : pointer;
  1584. end;
  1585. begin
  1586. { this is a little bit dirty but it works }
  1587. { and should be quite portable too }
  1588. r.proc:=firstconvert[c];
  1589. r.obj:=self;
  1590. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1591. end;
  1592. function ttypeconvnode.pass_1 : tnode;
  1593. begin
  1594. result:=nil;
  1595. firstpass(left);
  1596. if codegenerror then
  1597. exit;
  1598. { load the value_str from the left part }
  1599. registers32:=left.registers32;
  1600. registersfpu:=left.registersfpu;
  1601. {$ifdef SUPPORT_MMX}
  1602. registersmmx:=left.registersmmx;
  1603. {$endif}
  1604. expectloc:=left.expectloc;
  1605. if (nf_explicit in flags) or
  1606. (nf_absolute in flags) then
  1607. begin
  1608. { check if the result could be in a register }
  1609. if not(tstoreddef(resulttype.def).is_intregable) and
  1610. not(tstoreddef(resulttype.def).is_fpuregable) then
  1611. make_not_regable(left);
  1612. end;
  1613. result:=first_call_helper(convtype);
  1614. end;
  1615. function ttypeconvnode.docompare(p: tnode) : boolean;
  1616. begin
  1617. docompare :=
  1618. inherited docompare(p) and
  1619. (convtype = ttypeconvnode(p).convtype);
  1620. end;
  1621. {*****************************************************************************
  1622. TISNODE
  1623. *****************************************************************************}
  1624. constructor tisnode.create(l,r : tnode);
  1625. begin
  1626. inherited create(isn,l,r);
  1627. end;
  1628. function tisnode.det_resulttype:tnode;
  1629. var
  1630. paras: tcallparanode;
  1631. begin
  1632. result:=nil;
  1633. resulttypepass(left);
  1634. resulttypepass(right);
  1635. set_varstate(left,vs_used,true);
  1636. set_varstate(right,vs_used,true);
  1637. if codegenerror then
  1638. exit;
  1639. if (right.resulttype.def.deftype=classrefdef) then
  1640. begin
  1641. { left must be a class }
  1642. if is_class(left.resulttype.def) then
  1643. begin
  1644. { the operands must be related }
  1645. if (not(tobjectdef(left.resulttype.def).is_related(
  1646. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1647. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1648. tobjectdef(left.resulttype.def)))) then
  1649. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1650. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1651. end
  1652. else
  1653. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1654. { call fpc_do_is helper }
  1655. paras := ccallparanode.create(
  1656. left,
  1657. ccallparanode.create(
  1658. right,nil));
  1659. result := ccallnode.createintern('fpc_do_is',paras);
  1660. left := nil;
  1661. right := nil;
  1662. end
  1663. else if is_interface(right.resulttype.def) then
  1664. begin
  1665. { left is a class }
  1666. if is_class(left.resulttype.def) then
  1667. begin
  1668. { the operands must be related }
  1669. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1670. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1671. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1672. end
  1673. { left is an interface }
  1674. else if is_interface(left.resulttype.def) then
  1675. begin
  1676. { the operands must be related }
  1677. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1678. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1679. CGMessage(type_e_mismatch);
  1680. end
  1681. else
  1682. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1683. { call fpc_do_is helper }
  1684. paras := ccallparanode.create(
  1685. left,
  1686. ccallparanode.create(
  1687. right,nil));
  1688. result := ccallnode.createintern('fpc_do_is',paras);
  1689. left := nil;
  1690. right := nil;
  1691. end
  1692. else
  1693. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1694. resulttype:=booltype;
  1695. end;
  1696. function tisnode.pass_1 : tnode;
  1697. begin
  1698. internalerror(200204254);
  1699. result:=nil;
  1700. end;
  1701. { dummy pass_2, it will never be called, but we need one since }
  1702. { you can't instantiate an abstract class }
  1703. procedure tisnode.pass_2;
  1704. begin
  1705. end;
  1706. {*****************************************************************************
  1707. TASNODE
  1708. *****************************************************************************}
  1709. constructor tasnode.create(l,r : tnode);
  1710. begin
  1711. inherited create(asn,l,r);
  1712. call := nil;
  1713. end;
  1714. destructor tasnode.destroy;
  1715. begin
  1716. call.free;
  1717. inherited destroy;
  1718. end;
  1719. function tasnode.det_resulttype:tnode;
  1720. var
  1721. hp : tnode;
  1722. begin
  1723. result:=nil;
  1724. resulttypepass(right);
  1725. resulttypepass(left);
  1726. set_varstate(right,vs_used,true);
  1727. set_varstate(left,vs_used,true);
  1728. if codegenerror then
  1729. exit;
  1730. if (right.resulttype.def.deftype=classrefdef) then
  1731. begin
  1732. { left must be a class }
  1733. if is_class(left.resulttype.def) then
  1734. begin
  1735. { the operands must be related }
  1736. if (not(tobjectdef(left.resulttype.def).is_related(
  1737. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1738. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1739. tobjectdef(left.resulttype.def)))) then
  1740. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1741. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1742. end
  1743. else
  1744. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1745. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1746. end
  1747. else if is_interface(right.resulttype.def) then
  1748. begin
  1749. { left is a class }
  1750. if is_class(left.resulttype.def) then
  1751. begin
  1752. { the operands must be related
  1753. no, because the class instance could be a child class of the current one which
  1754. implements additional interfaces (FK)
  1755. b:=false;
  1756. o:=tobjectdef(left.resulttype.def);
  1757. while assigned(o) do
  1758. begin
  1759. if assigned(o.implementedinterfaces) and
  1760. (o.implementedinterfaces.searchintf(right.resulttype.def)<>-1) then
  1761. begin
  1762. b:=true;
  1763. break;
  1764. end;
  1765. o:=o.childof;
  1766. end;
  1767. if not(b) then
  1768. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1769. }
  1770. end
  1771. { left is an interface }
  1772. else if is_interface(left.resulttype.def) then
  1773. begin
  1774. { the operands must be related
  1775. we don't necessarily know how the both interfaces are implemented, so we can't do this check (FK)
  1776. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1777. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1778. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1779. }
  1780. end
  1781. else
  1782. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1783. resulttype:=right.resulttype;
  1784. { load the GUID of the interface }
  1785. if (right.nodetype=typen) then
  1786. begin
  1787. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  1788. begin
  1789. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  1790. right.free;
  1791. right:=hp;
  1792. end
  1793. else
  1794. internalerror(200206282);
  1795. resulttypepass(right);
  1796. end;
  1797. end
  1798. else
  1799. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1800. end;
  1801. function tasnode.getcopy: tnode;
  1802. begin
  1803. result := inherited getcopy;
  1804. if assigned(call) then
  1805. tasnode(result).call := call.getcopy
  1806. else
  1807. tasnode(result).call := nil;
  1808. end;
  1809. function tasnode.pass_1 : tnode;
  1810. var
  1811. procname: string;
  1812. begin
  1813. result:=nil;
  1814. if not assigned(call) then
  1815. begin
  1816. if is_class(left.resulttype.def) and
  1817. (right.resulttype.def.deftype=classrefdef) then
  1818. call := ccallnode.createinternres('fpc_do_as',
  1819. ccallparanode.create(left,ccallparanode.create(right,nil)),
  1820. resulttype)
  1821. else
  1822. begin
  1823. if is_class(left.resulttype.def) then
  1824. procname := 'fpc_class_as_intf'
  1825. else
  1826. procname := 'fpc_intf_as';
  1827. call := ccallnode.createinternres(procname,
  1828. ccallparanode.create(right,ccallparanode.create(left,nil)),
  1829. resulttype);
  1830. end;
  1831. left := nil;
  1832. right := nil;
  1833. firstpass(call);
  1834. if codegenerror then
  1835. exit;
  1836. expectloc:=call.expectloc;
  1837. registers32:=call.registers32;
  1838. registersfpu:=call.registersfpu;
  1839. {$ifdef SUPPORT_MMX}
  1840. registersmmx:=call.registersmmx;
  1841. {$endif SUPPORT_MMX}
  1842. end;
  1843. end;
  1844. begin
  1845. ctypeconvnode:=ttypeconvnode;
  1846. casnode:=tasnode;
  1847. cisnode:=tisnode;
  1848. end.
  1849. {
  1850. $Log$
  1851. Revision 1.129 2003-10-31 18:42:03 peter
  1852. * don't call proc_to_procvar for explicit typecasts
  1853. Revision 1.128 2003/10/29 22:01:20 florian
  1854. * fixed passing of dyn. arrays to open array parameters
  1855. Revision 1.127 2003/10/28 15:36:01 peter
  1856. * absolute to object field supported, fixes tb0458
  1857. Revision 1.126 2003/10/23 14:44:07 peter
  1858. * splitted buildderef and buildderefimpl to fix interface crc
  1859. calculation
  1860. Revision 1.125 2003/10/22 20:40:00 peter
  1861. * write derefdata in a separate ppu entry
  1862. Revision 1.124 2003/10/21 18:16:13 peter
  1863. * IncompatibleTypes() added that will include unit names when
  1864. the typenames are the same
  1865. Revision 1.123 2003/10/09 14:39:03 peter
  1866. * allow explicit typecasts from classrefdef, fixes 2728
  1867. Revision 1.122 2003/10/08 19:19:45 peter
  1868. * set_varstate cleanup
  1869. Revision 1.121 2003/10/07 14:30:27 peter
  1870. * fix 2720
  1871. Revision 1.120 2003/10/01 20:34:48 peter
  1872. * procinfo unit contains tprocinfo
  1873. * cginfo renamed to cgbase
  1874. * moved cgmessage to verbose
  1875. * fixed ppc and sparc compiles
  1876. Revision 1.119 2003/09/25 14:57:51 peter
  1877. * fix different expectloc
  1878. Revision 1.118 2003/09/06 22:27:08 florian
  1879. * fixed web bug 2669
  1880. * cosmetic fix in printnode
  1881. * tobjectdef.gettypename implemented
  1882. Revision 1.117 2003/09/03 15:55:01 peter
  1883. * NEWRA branch merged
  1884. Revision 1.116 2003/08/10 17:25:23 peter
  1885. * fixed some reported bugs
  1886. Revision 1.115 2003/06/05 20:05:55 peter
  1887. * removed changesettype because that will change the definition
  1888. of the setdef forever and can result in a different between
  1889. original interface and current implementation definition
  1890. Revision 1.114 2003/06/04 17:55:09 jonas
  1891. * disable fpuregable for fpu variables typecasted to non fpu-type
  1892. Revision 1.113 2003/06/04 17:29:01 jonas
  1893. * fixed void_to_(int,pointer) typeconversion
  1894. Revision 1.112 2003/06/03 21:05:48 peter
  1895. * fix check for procedure without parameters
  1896. * calling constructor as member will not allocate memory
  1897. Revision 1.111 2003/05/11 21:37:03 peter
  1898. * moved implicit exception frame from ncgutil to psub
  1899. * constructor/destructor helpers moved from cobj/ncgutil to psub
  1900. Revision 1.110 2003/05/09 17:47:02 peter
  1901. * self moved to hidden parameter
  1902. * removed hdisposen,hnewn,selfn
  1903. Revision 1.109 2003/04/27 11:21:33 peter
  1904. * aktprocdef renamed to current_procdef
  1905. * procinfo renamed to current_procinfo
  1906. * procinfo will now be stored in current_module so it can be
  1907. cleaned up properly
  1908. * gen_main_procsym changed to create_main_proc and release_main_proc
  1909. to also generate a tprocinfo structure
  1910. * fixed unit implicit initfinal
  1911. Revision 1.108 2003/04/23 20:16:04 peter
  1912. + added currency support based on int64
  1913. + is_64bit for use in cg units instead of is_64bitint
  1914. * removed cgmessage from n386add, replace with internalerrors
  1915. Revision 1.107 2003/04/23 13:13:08 peter
  1916. * fix checking of procdef type which was broken since loadn returned
  1917. pointertype for tp procvar
  1918. Revision 1.106 2003/04/23 10:10:07 peter
  1919. * expectloc fixes
  1920. Revision 1.105 2003/04/22 23:50:23 peter
  1921. * firstpass uses expectloc
  1922. * checks if there are differences between the expectloc and
  1923. location.loc from secondpass in EXTDEBUG
  1924. Revision 1.104 2003/04/22 09:52:30 peter
  1925. * do not convert procvars with void return to callnode
  1926. Revision 1.103 2003/03/17 18:54:23 peter
  1927. * fix missing self setting for method to procvar conversion in
  1928. tp_procvar mode
  1929. Revision 1.102 2003/02/15 22:15:57 carl
  1930. * generic conversaion routines only work on signed types
  1931. Revision 1.101 2003/01/16 22:13:52 peter
  1932. * convert_l3 convertlevel added. This level is used for conversions
  1933. where information can be lost like converting widestring->ansistring
  1934. or dword->byte
  1935. Revision 1.100 2003/01/15 01:44:32 peter
  1936. * merged methodpointer fixes from 1.0.x
  1937. Revision 1.99 2003/01/09 21:43:39 peter
  1938. * constant string conversion fixed, it's now equal to both
  1939. shortstring, ansistring and the typeconvnode will return
  1940. te_equal but still return convtype to change the constnode
  1941. Revision 1.98 2003/01/05 22:41:40 peter
  1942. * move code that checks for longint-pointer conversion hint
  1943. Revision 1.97 2003/01/03 12:15:56 daniel
  1944. * Removed ifdefs around notifications
  1945. ifdefs around for loop optimizations remain
  1946. Revision 1.96 2002/12/22 16:34:49 peter
  1947. * proc-procvar crash fixed (tw2277)
  1948. Revision 1.95 2002/12/20 16:01:26 peter
  1949. * don't allow class(classref) conversion
  1950. Revision 1.94 2002/12/05 14:27:26 florian
  1951. * some variant <-> dyn. array stuff
  1952. Revision 1.93 2002/11/30 10:45:14 carl
  1953. * fix bug with checking of duplicated items in sets (new sets bug only)
  1954. Revision 1.92 2002/11/27 19:43:21 carl
  1955. * updated notes and hints
  1956. Revision 1.91 2002/11/27 13:11:38 peter
  1957. * more currency fixes, taddcurr runs now successfull
  1958. Revision 1.90 2002/11/27 11:29:21 peter
  1959. * when converting from and to currency divide or multiple the
  1960. result by 10000
  1961. Revision 1.89 2002/11/25 17:43:18 peter
  1962. * splitted defbase in defutil,symutil,defcmp
  1963. * merged isconvertable and is_equal into compare_defs(_ext)
  1964. * made operator search faster by walking the list only once
  1965. Revision 1.88 2002/11/17 16:31:56 carl
  1966. * memory optimization (3-4%) : cleanup of tai fields,
  1967. cleanup of tdef and tsym fields.
  1968. * make it work for m68k
  1969. Revision 1.87 2002/10/10 16:07:57 florian
  1970. + several widestring/pwidechar related stuff added
  1971. Revision 1.86 2002/10/06 16:10:23 florian
  1972. * when compiling <interface> as <interface> we can't assume
  1973. anything about relation
  1974. Revision 1.85 2002/10/05 12:43:25 carl
  1975. * fixes for Delphi 6 compilation
  1976. (warning : Some features do not work under Delphi)
  1977. Revision 1.84 2002/10/02 20:23:50 florian
  1978. - removed the relation check for <class> as <interface> because we don't
  1979. know the runtime type of <class>! It could be a child class of the given type
  1980. which implements additional interfaces
  1981. Revision 1.83 2002/10/02 20:17:14 florian
  1982. + the as operator for <class> as <interface> has to check the parent classes as well
  1983. Revision 1.82 2002/09/30 07:00:47 florian
  1984. * fixes to common code to get the alpha compiler compiled applied
  1985. Revision 1.81 2002/09/16 14:11:13 peter
  1986. * add argument to equal_paras() to support default values or not
  1987. Revision 1.80 2002/09/07 20:40:23 carl
  1988. * cardinal -> longword
  1989. Revision 1.79 2002/09/07 15:25:03 peter
  1990. * old logs removed and tabs fixed
  1991. Revision 1.78 2002/09/07 12:16:04 carl
  1992. * second part bug report 1996 fix, testrange in cordconstnode
  1993. only called if option is set (also make parsing a tiny faster)
  1994. Revision 1.77 2002/09/05 05:56:07 jonas
  1995. - reverted my last commit, it was completely bogus :(
  1996. Revision 1.75 2002/09/02 19:24:42 peter
  1997. * array of char support for Str()
  1998. Revision 1.74 2002/09/01 08:01:16 daniel
  1999. * Removed sets from Tcallnode.det_resulttype
  2000. + Added read/write notifications of variables. These will be usefull
  2001. for providing information for several optimizations. For example
  2002. the value of the loop variable of a for loop does matter is the
  2003. variable is read after the for loop, but if it's no longer used
  2004. or written, it doesn't matter and this can be used to optimize
  2005. the loop code generation.
  2006. Revision 1.73 2002/08/23 16:14:49 peter
  2007. * tempgen cleanup
  2008. * tt_noreuse temp type added that will be used in genentrycode
  2009. Revision 1.72 2002/08/20 18:23:33 jonas
  2010. * the as node again uses a compilerproc
  2011. + (untested) support for interface "as" statements
  2012. Revision 1.71 2002/08/19 19:36:43 peter
  2013. * More fixes for cross unit inlining, all tnodes are now implemented
  2014. * Moved pocall_internconst to po_internconst because it is not a
  2015. calling type at all and it conflicted when inlining of these small
  2016. functions was requested
  2017. Revision 1.70 2002/08/17 09:23:36 florian
  2018. * first part of current_procinfo rewrite
  2019. Revision 1.69 2002/08/14 19:26:55 carl
  2020. + generic int_to_real type conversion
  2021. + generic unaryminus node
  2022. Revision 1.68 2002/08/11 16:08:55 florian
  2023. + support of explicit type case boolean->char
  2024. Revision 1.67 2002/08/11 15:28:00 florian
  2025. + support of explicit type case <any ordinal type>->pointer
  2026. (delphi mode only)
  2027. Revision 1.66 2002/08/09 07:33:01 florian
  2028. * a couple of interface related fixes
  2029. Revision 1.65 2002/07/29 21:23:42 florian
  2030. * more fixes for the ppc
  2031. + wrappers for the tcnvnode.first_* stuff introduced
  2032. Revision 1.64 2002/07/23 12:34:30 daniel
  2033. * Readded old set code. To use it define 'oldset'. Activated by default
  2034. for ppc.
  2035. Revision 1.63 2002/07/23 09:51:22 daniel
  2036. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2037. are worth comitting.
  2038. Revision 1.62 2002/07/22 11:48:04 daniel
  2039. * Sets are now internally sets.
  2040. Revision 1.61 2002/07/20 17:16:02 florian
  2041. + source code page support
  2042. Revision 1.60 2002/07/20 11:57:54 florian
  2043. * types.pas renamed to defbase.pas because D6 contains a types
  2044. unit so this would conflicts if D6 programms are compiled
  2045. + Willamette/SSE2 instructions to assembler added
  2046. Revision 1.59 2002/07/01 16:23:53 peter
  2047. * cg64 patch
  2048. * basics for currency
  2049. * asnode updates for class and interface (not finished)
  2050. Revision 1.58 2002/05/18 13:34:09 peter
  2051. * readded missing revisions
  2052. }