ncnv.pas 99 KB

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