ncnv.pas 124 KB

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