ncnv.pas 116 KB

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