ncnv.pas 94 KB

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