ncnv.pas 133 KB

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