ncnv.pas 120 KB

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