ncnv.pas 115 KB

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