htypechk.pas 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit exports some help routines for the type checking
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit htypechk;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. tokens,
  23. {$ifdef cg11}
  24. node,
  25. {$else cg11}
  26. tree,
  27. {$endif cg11}
  28. symtable;
  29. type
  30. {$ifdef cg11}
  31. Ttok2nodeRec=record
  32. tok : ttoken;
  33. nod : tnodetype;
  34. op_overloading_supported : boolean;
  35. {$else cg11}
  36. Ttok2nodeRec=record
  37. tok : ttoken;
  38. nod : ttreetyp;
  39. op_overloading_supported : boolean;
  40. {$endif cg11}
  41. end;
  42. const
  43. tok2nodes=25;
  44. tok2node:array[1..tok2nodes] of ttok2noderec=(
  45. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  46. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  47. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  48. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  49. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  50. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  51. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  52. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  53. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  54. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  55. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  56. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  57. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  58. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  59. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  60. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  61. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  62. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  63. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  64. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  65. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  66. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  67. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  68. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  69. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  70. );
  71. const
  72. { firstcallparan without varspez we don't count the ref }
  73. {$ifdef extdebug}
  74. count_ref : boolean = true;
  75. {$endif def extdebug}
  76. get_para_resulttype : boolean = false;
  77. allow_array_constructor : boolean = false;
  78. {$ifdef cg11}
  79. { if someones has a lot of time he might change this into methods }
  80. { tnode and its decendends }
  81. { Conversion }
  82. function isconvertable(def_from,def_to : pdef;
  83. var doconv : tconverttype;fromtreetype : tnodetype;
  84. explicit : boolean) : byte;
  85. { is overloading of this operator allowed for this
  86. binary operator }
  87. function isbinaryoperatoroverloadable(ld, rd,dd : pdef;
  88. treetyp : tnodetype) : boolean;
  89. { is overloading of this operator allowed for this
  90. unary operator }
  91. function isunaryoperatoroverloadable(rd,dd : pdef;
  92. treetyp : tnodetype) : boolean;
  93. { check operator args and result type }
  94. function isoperatoracceptable(pf : pprocdef; optoken : ttoken) : boolean;
  95. { Register Allocation }
  96. procedure make_not_regable(p : tnode);
  97. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  98. { subroutine handling }
  99. procedure test_protected_sym(sym : psym);
  100. procedure test_protected(p : tnode);
  101. function valid_for_formal_var(p : tnode) : boolean;
  102. function valid_for_formal_const(p : tnode) : boolean;
  103. function is_procsym_load(p:tnode):boolean;
  104. function is_procsym_call(p:tnode):boolean;
  105. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  106. procedure test_local_to_procvar(from_def:pprocvardef;to_def:pdef);
  107. function valid_for_assign(p:tnode;allowprop:boolean):boolean;
  108. {$else cg11}
  109. { Conversion }
  110. function isconvertable(def_from,def_to : pdef;
  111. var doconv : tconverttype;fromtreetype : ttreetyp;
  112. explicit : boolean) : byte;
  113. { is overloading of this operator allowed for this
  114. binary operator }
  115. function isbinaryoperatoroverloadable(ld, rd,dd : pdef;
  116. treetyp : ttreetyp) : boolean;
  117. { is overloading of this operator allowed for this
  118. unary operator }
  119. function isunaryoperatoroverloadable(rd,dd : pdef;
  120. treetyp : ttreetyp) : boolean;
  121. { check operator args and result type }
  122. function isoperatoracceptable(pf : pprocdef; optoken : ttoken) : boolean;
  123. { Register Allocation }
  124. procedure make_not_regable(p : ptree);
  125. procedure left_right_max(p : ptree);
  126. procedure calcregisters(p : ptree;r32,fpu,mmx : word);
  127. { subroutine handling }
  128. procedure test_protected_sym(sym : psym);
  129. procedure test_protected(p : ptree);
  130. function valid_for_formal_var(p : ptree) : boolean;
  131. function valid_for_formal_const(p : ptree) : boolean;
  132. function is_procsym_load(p:Ptree):boolean;
  133. function is_procsym_call(p:Ptree):boolean;
  134. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  135. procedure test_local_to_procvar(from_def:pprocvardef;to_def:pdef);
  136. function valid_for_assign(p:ptree;allowprop:boolean):boolean;
  137. {$endif cg11}
  138. implementation
  139. uses
  140. globtype,systems,
  141. cutils,cobjects,verbose,globals,
  142. symconst,
  143. types,pass_1,cpubase,
  144. {$ifdef cg11}
  145. ncnv,nld,
  146. nmem,ncal,
  147. {$endif cg11}
  148. {$ifdef newcg}
  149. cgbase
  150. {$else}
  151. hcodegen
  152. {$endif}
  153. ;
  154. {$ifdef cg11}
  155. {****************************************************************************
  156. Convert
  157. ****************************************************************************}
  158. { Returns:
  159. 0 - Not convertable
  160. 1 - Convertable
  161. 2 - Convertable, but not first choice }
  162. function isconvertable(def_from,def_to : pdef;
  163. var doconv : tconverttype;fromtreetype : tnodetype;
  164. explicit : boolean) : byte;
  165. { Tbasetype: uauto,uvoid,uchar,
  166. u8bit,u16bit,u32bit,
  167. s8bit,s16bit,s32,
  168. bool8bit,bool16bit,bool32bit,
  169. u64bit,s64bitint }
  170. type
  171. tbasedef=(bvoid,bchar,bint,bbool);
  172. const
  173. basedeftbl:array[tbasetype] of tbasedef =
  174. (bvoid,bvoid,bchar,
  175. bint,bint,bint,
  176. bint,bint,bint,
  177. bbool,bbool,bbool,bint,bint,bchar);
  178. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  179. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  180. (tc_not_possible,tc_equal,tc_not_possible,tc_not_possible),
  181. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  182. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  183. var
  184. b : byte;
  185. hd1,hd2 : pdef;
  186. hct : tconverttype;
  187. begin
  188. { safety check }
  189. if not(assigned(def_from) and assigned(def_to)) then
  190. begin
  191. isconvertable:=0;
  192. exit;
  193. end;
  194. { tp7 procvar def support, in tp7 a procvar is always called, if the
  195. procvar is passed explicit a addrn would be there }
  196. if (m_tp_procvar in aktmodeswitches) and
  197. (def_from^.deftype=procvardef) and
  198. (fromtreetype=loadn) then
  199. begin
  200. def_from:=pprocvardef(def_from)^.rettype.def;
  201. end;
  202. { we walk the wanted (def_to) types and check then the def_from
  203. types if there is a conversion possible }
  204. b:=0;
  205. case def_to^.deftype of
  206. orddef :
  207. begin
  208. case def_from^.deftype of
  209. orddef :
  210. begin
  211. doconv:=basedefconverts[basedeftbl[porddef(def_from)^.typ],basedeftbl[porddef(def_to)^.typ]];
  212. b:=1;
  213. if (doconv=tc_not_possible) or
  214. ((doconv=tc_int_2_bool) and
  215. (not explicit) and
  216. (not is_boolean(def_from))) or
  217. ((doconv=tc_bool_2_int) and
  218. (not explicit) and
  219. (not is_boolean(def_to))) then
  220. b:=0;
  221. end;
  222. enumdef :
  223. begin
  224. { needed for char(enum) }
  225. if explicit then
  226. begin
  227. doconv:=tc_int_2_int;
  228. b:=1;
  229. end;
  230. end;
  231. end;
  232. end;
  233. stringdef :
  234. begin
  235. case def_from^.deftype of
  236. stringdef :
  237. begin
  238. doconv:=tc_string_2_string;
  239. b:=1;
  240. end;
  241. orddef :
  242. begin
  243. { char to string}
  244. if is_char(def_from) then
  245. begin
  246. doconv:=tc_char_2_string;
  247. b:=1;
  248. end;
  249. end;
  250. arraydef :
  251. begin
  252. { array of char to string, the length check is done by the firstpass of this node }
  253. if is_chararray(def_from) then
  254. begin
  255. doconv:=tc_chararray_2_string;
  256. if (not(cs_ansistrings in aktlocalswitches) and
  257. is_shortstring(def_to)) or
  258. ((cs_ansistrings in aktlocalswitches) and
  259. is_ansistring(def_to)) then
  260. b:=1
  261. else
  262. b:=2;
  263. end;
  264. end;
  265. pointerdef :
  266. begin
  267. { pchar can be assigned to short/ansistrings,
  268. but not in tp7 compatible mode }
  269. if is_pchar(def_from) and not(m_tp7 in aktmodeswitches) then
  270. begin
  271. doconv:=tc_pchar_2_string;
  272. b:=1;
  273. end;
  274. end;
  275. end;
  276. end;
  277. floatdef :
  278. begin
  279. case def_from^.deftype of
  280. orddef :
  281. begin { ordinal to real }
  282. if is_integer(def_from) then
  283. begin
  284. if pfloatdef(def_to)^.typ=f32bit then
  285. doconv:=tc_int_2_fix
  286. else
  287. doconv:=tc_int_2_real;
  288. b:=1;
  289. end;
  290. end;
  291. floatdef :
  292. begin { 2 float types ? }
  293. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  294. doconv:=tc_equal
  295. else
  296. begin
  297. if pfloatdef(def_from)^.typ=f32bit then
  298. doconv:=tc_fix_2_real
  299. else
  300. if pfloatdef(def_to)^.typ=f32bit then
  301. doconv:=tc_real_2_fix
  302. else
  303. doconv:=tc_real_2_real;
  304. end;
  305. b:=1;
  306. end;
  307. end;
  308. end;
  309. enumdef :
  310. begin
  311. if (def_from^.deftype=enumdef) then
  312. begin
  313. hd1:=def_from;
  314. while assigned(penumdef(hd1)^.basedef) do
  315. hd1:=penumdef(hd1)^.basedef;
  316. hd2:=def_to;
  317. while assigned(penumdef(hd2)^.basedef) do
  318. hd2:=penumdef(hd2)^.basedef;
  319. if (hd1=hd2) then
  320. begin
  321. b:=1;
  322. { because of packenum they can have different sizes! (JM) }
  323. doconv:=tc_int_2_int;
  324. end;
  325. end;
  326. end;
  327. arraydef :
  328. begin
  329. { open array is also compatible with a single element of its base type }
  330. if is_open_array(def_to) and
  331. is_equal(parraydef(def_to)^.elementtype.def,def_from) then
  332. begin
  333. doconv:=tc_equal;
  334. b:=1;
  335. end
  336. else
  337. begin
  338. case def_from^.deftype of
  339. arraydef :
  340. begin
  341. { array constructor -> open array }
  342. if is_open_array(def_to) and
  343. is_array_constructor(def_from) then
  344. begin
  345. if is_void(parraydef(def_from)^.elementtype.def) or
  346. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  347. begin
  348. doconv:=tc_equal;
  349. b:=1;
  350. end
  351. else
  352. if isconvertable(parraydef(def_from)^.elementtype.def,
  353. parraydef(def_to)^.elementtype.def,hct,arrayconstructn,false)<>0 then
  354. begin
  355. doconv:=hct;
  356. b:=2;
  357. end;
  358. end;
  359. end;
  360. pointerdef :
  361. begin
  362. if is_zero_based_array(def_to) and
  363. is_equal(ppointerdef(def_from)^.pointertype.def,parraydef(def_to)^.elementtype.def) then
  364. begin
  365. doconv:=tc_pointer_2_array;
  366. b:=1;
  367. end;
  368. end;
  369. stringdef :
  370. begin
  371. { string to array of char}
  372. if (not(is_special_array(def_to)) or is_open_array(def_to)) and
  373. is_equal(parraydef(def_to)^.elementtype.def,cchardef) then
  374. begin
  375. doconv:=tc_string_2_chararray;
  376. b:=1;
  377. end;
  378. end;
  379. end;
  380. end;
  381. end;
  382. pointerdef :
  383. begin
  384. case def_from^.deftype of
  385. stringdef :
  386. begin
  387. { string constant (which can be part of array constructor)
  388. to zero terminated string constant }
  389. if (fromtreetype in [arrayconstructn,stringconstn]) and
  390. is_pchar(def_to) then
  391. begin
  392. doconv:=tc_cstring_2_pchar;
  393. b:=1;
  394. end;
  395. end;
  396. orddef :
  397. begin
  398. { char constant to zero terminated string constant }
  399. if (fromtreetype=ordconstn) then
  400. begin
  401. if is_equal(def_from,cchardef) and
  402. is_pchar(def_to) then
  403. begin
  404. doconv:=tc_cchar_2_pchar;
  405. b:=1;
  406. end
  407. else
  408. if is_integer(def_from) then
  409. begin
  410. doconv:=tc_cord_2_pointer;
  411. b:=1;
  412. end;
  413. end;
  414. end;
  415. arraydef :
  416. begin
  417. { chararray to pointer }
  418. if is_zero_based_array(def_from) and
  419. is_equal(parraydef(def_from)^.elementtype.def,ppointerdef(def_to)^.pointertype.def) then
  420. begin
  421. doconv:=tc_array_2_pointer;
  422. b:=1;
  423. end;
  424. end;
  425. pointerdef :
  426. begin
  427. { child class pointer can be assigned to anchestor pointers }
  428. if (
  429. (ppointerdef(def_from)^.pointertype.def^.deftype=objectdef) and
  430. (ppointerdef(def_to)^.pointertype.def^.deftype=objectdef) and
  431. pobjectdef(ppointerdef(def_from)^.pointertype.def)^.is_related(
  432. pobjectdef(ppointerdef(def_to)^.pointertype.def))
  433. ) or
  434. { all pointers can be assigned to void-pointer }
  435. is_equal(ppointerdef(def_to)^.pointertype.def,voiddef) or
  436. { in my opnion, is this not clean pascal }
  437. { well, but it's handy to use, it isn't ? (FK) }
  438. is_equal(ppointerdef(def_from)^.pointertype.def,voiddef) then
  439. begin
  440. doconv:=tc_equal;
  441. b:=1;
  442. end;
  443. end;
  444. procvardef :
  445. begin
  446. { procedure variable can be assigned to an void pointer }
  447. { Not anymore. Use the @ operator now.}
  448. if not(m_tp_procvar in aktmodeswitches) and
  449. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  450. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  451. begin
  452. doconv:=tc_equal;
  453. b:=1;
  454. end;
  455. end;
  456. classrefdef,
  457. objectdef :
  458. begin
  459. { class types and class reference type
  460. can be assigned to void pointers }
  461. if (
  462. ((def_from^.deftype=objectdef) and pobjectdef(def_from)^.is_class) or
  463. (def_from^.deftype=classrefdef)
  464. ) and
  465. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  466. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  467. begin
  468. doconv:=tc_equal;
  469. b:=1;
  470. end;
  471. end;
  472. end;
  473. end;
  474. setdef :
  475. begin
  476. { automatic arrayconstructor -> set conversion }
  477. if is_array_constructor(def_from) then
  478. begin
  479. doconv:=tc_arrayconstructor_2_set;
  480. b:=1;
  481. end;
  482. end;
  483. procvardef :
  484. begin
  485. { proc -> procvar }
  486. if (def_from^.deftype=procdef) then
  487. begin
  488. doconv:=tc_proc_2_procvar;
  489. if proc_to_procvar_equal(pprocdef(def_from),pprocvardef(def_to)) then
  490. b:=1;
  491. end
  492. else
  493. { for example delphi allows the assignement from pointers }
  494. { to procedure variables }
  495. if (m_pointer_2_procedure in aktmodeswitches) and
  496. (def_from^.deftype=pointerdef) and
  497. (ppointerdef(def_from)^.pointertype.def^.deftype=orddef) and
  498. (porddef(ppointerdef(def_from)^.pointertype.def)^.typ=uvoid) then
  499. begin
  500. doconv:=tc_equal;
  501. b:=1;
  502. end
  503. else
  504. { nil is compatible with procvars }
  505. if (fromtreetype=niln) then
  506. begin
  507. doconv:=tc_equal;
  508. b:=1;
  509. end;
  510. end;
  511. objectdef :
  512. begin
  513. { object pascal objects }
  514. if (def_from^.deftype=objectdef) {and
  515. pobjectdef(def_from)^.isclass and pobjectdef(def_to)^.isclass }then
  516. begin
  517. doconv:=tc_equal;
  518. if pobjectdef(def_from)^.is_related(pobjectdef(def_to)) then
  519. b:=1;
  520. end
  521. else
  522. { Class specific }
  523. if (pobjectdef(def_to)^.is_class) then
  524. begin
  525. { void pointer also for delphi mode }
  526. if (m_delphi in aktmodeswitches) and
  527. is_voidpointer(def_from) then
  528. begin
  529. doconv:=tc_equal;
  530. b:=1;
  531. end
  532. else
  533. { nil is compatible with class instances }
  534. if (fromtreetype=niln) and (pobjectdef(def_to)^.is_class) then
  535. begin
  536. doconv:=tc_equal;
  537. b:=1;
  538. end;
  539. end;
  540. end;
  541. classrefdef :
  542. begin
  543. { class reference types }
  544. if (def_from^.deftype=classrefdef) then
  545. begin
  546. doconv:=tc_equal;
  547. if pobjectdef(pclassrefdef(def_from)^.pointertype.def)^.is_related(
  548. pobjectdef(pclassrefdef(def_to)^.pointertype.def)) then
  549. b:=1;
  550. end
  551. else
  552. { nil is compatible with class references }
  553. if (fromtreetype=niln) then
  554. begin
  555. doconv:=tc_equal;
  556. b:=1;
  557. end;
  558. end;
  559. filedef :
  560. begin
  561. { typed files are all equal to the abstract file type
  562. name TYPEDFILE in system.pp in is_equal in types.pas
  563. the problem is that it sholud be also compatible to FILE
  564. but this would leed to a problem for ASSIGN RESET and REWRITE
  565. when trying to find the good overloaded function !!
  566. so all file function are doubled in system.pp
  567. this is not very beautiful !!}
  568. if (def_from^.deftype=filedef) and
  569. (
  570. (
  571. (pfiledef(def_from)^.filetyp = ft_typed) and
  572. (pfiledef(def_to)^.filetyp = ft_typed) and
  573. (
  574. (pfiledef(def_from)^.typedfiletype.def = pdef(voiddef)) or
  575. (pfiledef(def_to)^.typedfiletype.def = pdef(voiddef))
  576. )
  577. ) or
  578. (
  579. (
  580. (pfiledef(def_from)^.filetyp = ft_untyped) and
  581. (pfiledef(def_to)^.filetyp = ft_typed)
  582. ) or
  583. (
  584. (pfiledef(def_from)^.filetyp = ft_typed) and
  585. (pfiledef(def_to)^.filetyp = ft_untyped)
  586. )
  587. )
  588. ) then
  589. begin
  590. doconv:=tc_equal;
  591. b:=1;
  592. end
  593. end;
  594. else
  595. begin
  596. { assignment overwritten ?? }
  597. if assignment_overloaded(def_from,def_to)<>nil then
  598. b:=2;
  599. end;
  600. end;
  601. isconvertable:=b;
  602. end;
  603. { ld is the left type definition
  604. rd the right type definition
  605. dd the result type definition or voiddef if unkown }
  606. function isbinaryoperatoroverloadable(ld, rd, dd : pdef;
  607. treetyp : tnodetype) : boolean;
  608. begin
  609. isbinaryoperatoroverloadable:=
  610. (treetyp=starstarn) or
  611. (ld^.deftype=recorddef) or
  612. (rd^.deftype=recorddef) or
  613. ((rd^.deftype=pointerdef) and
  614. not(is_pchar(rd) and
  615. (is_chararray(ld) or
  616. (ld^.deftype=stringdef) or
  617. (treetyp=addn))) and
  618. (not(ld^.deftype in [pointerdef,objectdef,classrefdef,procvardef]) or
  619. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,subn])
  620. ) and
  621. (not is_integer(ld) or not (treetyp in [addn,subn]))
  622. ) or
  623. ((ld^.deftype=pointerdef) and
  624. not(is_pchar(ld) and
  625. (is_chararray(rd) or
  626. (rd^.deftype=stringdef) or
  627. (treetyp=addn))) and
  628. (not(rd^.deftype in [stringdef,pointerdef,objectdef,classrefdef,procvardef]) and
  629. ((not is_integer(rd) and (rd^.deftype<>objectdef)
  630. and (rd^.deftype<>classrefdef)) or
  631. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn])
  632. )
  633. )
  634. ) or
  635. { array def, but not mmx or chararray+[char,string,chararray] }
  636. ((ld^.deftype=arraydef) and
  637. not((cs_mmx in aktlocalswitches) and
  638. is_mmx_able_array(ld)) and
  639. not(is_chararray(ld) and
  640. (is_char(rd) or
  641. is_pchar(rd) or
  642. (rd^.deftype=stringdef) or
  643. is_chararray(rd)))
  644. ) or
  645. ((rd^.deftype=arraydef) and
  646. not((cs_mmx in aktlocalswitches) and
  647. is_mmx_able_array(rd)) and
  648. not(is_chararray(rd) and
  649. (is_char(ld) or
  650. is_pchar(ld) or
  651. (ld^.deftype=stringdef) or
  652. is_chararray(ld)))
  653. ) or
  654. { <> and = are defined for classes }
  655. ((ld^.deftype=objectdef) and
  656. (not(pobjectdef(ld)^.is_class) or
  657. not(treetyp in [equaln,unequaln])
  658. )
  659. ) or
  660. ((rd^.deftype=objectdef) and
  661. (not(pobjectdef(rd)^.is_class) or
  662. not(treetyp in [equaln,unequaln])
  663. )
  664. or
  665. { allow other operators that + on strings }
  666. (
  667. (is_char(rd) or
  668. is_pchar(rd) or
  669. (rd^.deftype=stringdef) or
  670. is_chararray(rd) or
  671. is_char(ld) or
  672. is_pchar(ld) or
  673. (ld^.deftype=stringdef) or
  674. is_chararray(ld)
  675. ) and
  676. not(treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  677. not(is_pchar(ld) and
  678. (is_integer(rd) or (rd^.deftype=pointerdef)) and
  679. (treetyp=subn)
  680. )
  681. )
  682. );
  683. end;
  684. function isunaryoperatoroverloadable(rd,dd : pdef;
  685. treetyp : tnodetype) : boolean;
  686. begin
  687. isunaryoperatoroverloadable:=false;
  688. { what assignment overloading should be allowed ?? }
  689. if (treetyp=assignn) then
  690. begin
  691. isunaryoperatoroverloadable:=true;
  692. { this already get tbs0261 to fail
  693. isunaryoperatoroverloadable:=not is_equal(rd,dd); PM }
  694. end
  695. { should we force that rd and dd are equal ?? }
  696. else if (treetyp=subn { unaryminusn }) then
  697. begin
  698. isunaryoperatoroverloadable:=
  699. not is_integer(rd) and not (rd^.deftype=floatdef)
  700. {$ifdef SUPPORT_MMX}
  701. and not ((cs_mmx in aktlocalswitches) and
  702. is_mmx_able_array(rd))
  703. {$endif SUPPORT_MMX}
  704. ;
  705. end
  706. else if (treetyp=notn) then
  707. begin
  708. isunaryoperatoroverloadable:=not is_integer(rd) and not is_boolean(rd)
  709. {$ifdef SUPPORT_MMX}
  710. and not ((cs_mmx in aktlocalswitches) and
  711. is_mmx_able_array(rd))
  712. {$endif SUPPORT_MMX}
  713. ;
  714. end;
  715. end;
  716. function isoperatoracceptable(pf : pprocdef; optoken : ttoken) : boolean;
  717. var
  718. ld,rd,dd : pdef;
  719. i : longint;
  720. begin
  721. case pf^.parast^.symindex^.count of
  722. 2 : begin
  723. isoperatoracceptable:=false;
  724. for i:=1 to tok2nodes do
  725. if tok2node[i].tok=optoken then
  726. begin
  727. ld:=pvarsym(pf^.parast^.symindex^.first)^.vartype.def;
  728. rd:=pvarsym(pf^.parast^.symindex^.first^.indexnext)^.vartype.def;
  729. dd:=pf^.rettype.def;
  730. isoperatoracceptable:=
  731. tok2node[i].op_overloading_supported and
  732. isbinaryoperatoroverloadable(ld,rd,dd,tok2node[i].nod);
  733. break;
  734. end;
  735. end;
  736. 1 : begin
  737. rd:=pvarsym(pf^.parast^.symindex^.first)^.vartype.def;
  738. dd:=pf^.rettype.def;
  739. for i:=1 to tok2nodes do
  740. if tok2node[i].tok=optoken then
  741. begin
  742. isoperatoracceptable:=
  743. tok2node[i].op_overloading_supported and
  744. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  745. break;
  746. end;
  747. end;
  748. else
  749. isoperatoracceptable:=false;
  750. end;
  751. end;
  752. {****************************************************************************
  753. Register Calculation
  754. ****************************************************************************}
  755. { marks an lvalue as "unregable" }
  756. procedure make_not_regable(p : tnode);
  757. begin
  758. case p.nodetype of
  759. typeconvn :
  760. make_not_regable(ttypeconvnode(p).left);
  761. loadn :
  762. if tloadnode(p).symtableentry^.typ=varsym then
  763. pvarsym(tloadnode(p).symtableentry)^.varoptions:=pvarsym(tloadnode(p).symtableentry)^.varoptions-[vo_regable,vo_fpuregable];
  764. end;
  765. end;
  766. { calculates the needed registers for a binary operator }
  767. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  768. begin
  769. p.left_right_max;
  770. { Only when the difference between the left and right registers < the
  771. wanted registers allocate the amount of registers }
  772. if assigned(p.left) then
  773. begin
  774. if assigned(p.right) then
  775. begin
  776. if (abs(p.left.registers32-p.right.registers32)<r32) then
  777. inc(p.registers32,r32);
  778. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  779. inc(p.registersfpu,fpu);
  780. {$ifdef SUPPORT_MMX}
  781. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  782. inc(p.registersmmx,mmx);
  783. {$endif SUPPORT_MMX}
  784. { the following is a little bit guessing but I think }
  785. { it's the only way to solve same internalerrors: }
  786. { if the left and right node both uses registers }
  787. { and return a mem location, but the current node }
  788. { doesn't use an integer register we get probably }
  789. { trouble when restoring a node }
  790. if (p.left.registers32=p.right.registers32) and
  791. (p.registers32=p.left.registers32) and
  792. (p.registers32>0) and
  793. (p.left.location.loc in [LOC_REFERENCE,LOC_MEM]) and
  794. (p.right.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  795. inc(p.registers32);
  796. end
  797. else
  798. begin
  799. if (p.left.registers32<r32) then
  800. inc(p.registers32,r32);
  801. if (p.left.registersfpu<fpu) then
  802. inc(p.registersfpu,fpu);
  803. {$ifdef SUPPORT_MMX}
  804. if (p.left.registersmmx<mmx) then
  805. inc(p.registersmmx,mmx);
  806. {$endif SUPPORT_MMX}
  807. end;
  808. end;
  809. { error CGMessage, if more than 8 floating point }
  810. { registers are needed }
  811. if p.registersfpu>8 then
  812. CGMessage(cg_e_too_complex_expr);
  813. end;
  814. {****************************************************************************
  815. Subroutine Handling
  816. ****************************************************************************}
  817. { protected field handling
  818. protected field can not appear in
  819. var parameters of function !!
  820. this can only be done after we have determined the
  821. overloaded function
  822. this is the reason why it is not in the parser, PM }
  823. procedure test_protected_sym(sym : psym);
  824. begin
  825. if (sp_protected in sym^.symoptions) and
  826. ((sym^.owner^.symtabletype=unitsymtable) or
  827. ((sym^.owner^.symtabletype=objectsymtable) and
  828. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable))
  829. ) then
  830. CGMessage(parser_e_cant_access_protected_member);
  831. end;
  832. procedure test_protected(p : tnode);
  833. begin
  834. case p.nodetype of
  835. loadn : test_protected_sym(tloadnode(p).symtableentry);
  836. typeconvn : test_protected(ttypeconvnode(p).left);
  837. derefn : test_protected(tderefnode(p).left);
  838. subscriptn : begin
  839. { test_protected(p.left);
  840. Is a field of a protected var
  841. also protected ??? PM }
  842. test_protected_sym(tsubscriptnode(p).vs);
  843. end;
  844. end;
  845. end;
  846. function valid_for_formal_var(p : tnode) : boolean;
  847. var
  848. v : boolean;
  849. begin
  850. case p.nodetype of
  851. loadn :
  852. v:=(tloadnode(p).symtableentry^.typ in [typedconstsym,varsym]);
  853. typeconvn :
  854. v:=valid_for_formal_var(ttypeconvnode(p).left);
  855. derefn,
  856. subscriptn,
  857. vecn,
  858. funcretn,
  859. selfn :
  860. v:=true;
  861. calln : { procvars are callnodes first }
  862. v:=assigned(tcallnode(p).right) and not assigned(tcallnode(p).left);
  863. addrn :
  864. begin
  865. { addrn is not allowed as this generate a constant value,
  866. but a tp procvar are allowed (PFV) }
  867. if nf_procvarload in p.flags then
  868. v:=true
  869. else
  870. v:=false;
  871. end;
  872. else
  873. v:=false;
  874. end;
  875. valid_for_formal_var:=v;
  876. end;
  877. function valid_for_formal_const(p : tnode) : boolean;
  878. var
  879. v : boolean;
  880. begin
  881. { p must have been firstpass'd before }
  882. { accept about anything but not a statement ! }
  883. case p.nodetype of
  884. calln,
  885. statementn,
  886. addrn :
  887. begin
  888. { addrn is not allowed as this generate a constant value,
  889. but a tp procvar are allowed (PFV) }
  890. if nf_procvarload in p.flags then
  891. v:=true
  892. else
  893. v:=false;
  894. end;
  895. else
  896. v:=true;
  897. end;
  898. valid_for_formal_const:=v;
  899. end;
  900. function is_procsym_load(p:tnode):boolean;
  901. begin
  902. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry^.typ=procsym)) or
  903. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  904. and (tloadnode(taddrnode(p).left).symtableentry^.typ=procsym)) ;
  905. end;
  906. { change a proc call to a procload for assignment to a procvar }
  907. { this can only happen for proc/function without arguments }
  908. function is_procsym_call(p:tnode):boolean;
  909. begin
  910. is_procsym_call:=(p.nodetype=calln) and (tcallnode(p).left=nil) and
  911. (((tcallnode(p).symtableprocentry^.typ=procsym) and (tcallnode(p).right=nil)) or
  912. (assigned(tcallnode(p).right) and (tcallnode(tcallnode(p).right).symtableprocentry^.typ=varsym)));
  913. end;
  914. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  915. var
  916. passproc : pprocdef;
  917. convtyp : tconverttype;
  918. begin
  919. assignment_overloaded:=nil;
  920. if assigned(overloaded_operators[_assignment]) then
  921. passproc:=overloaded_operators[_assignment]^.definition
  922. else
  923. exit;
  924. while passproc<>nil do
  925. begin
  926. if is_equal(passproc^.rettype.def,to_def) and
  927. (is_equal(pparaitem(passproc^.para^.first)^.paratype.def,from_def) or
  928. (isconvertable(from_def,pparaitem(passproc^.para^.first)^.paratype.def,convtyp,ordconstn,false)=1)) then
  929. begin
  930. assignment_overloaded:=passproc;
  931. break;
  932. end;
  933. passproc:=passproc^.nextoverloaded;
  934. end;
  935. end;
  936. { local routines can't be assigned to procvars }
  937. procedure test_local_to_procvar(from_def:pprocvardef;to_def:pdef);
  938. begin
  939. if (from_def^.symtablelevel>1) and (to_def^.deftype=procvardef) then
  940. CGMessage(type_e_cannot_local_proc_to_procvar);
  941. end;
  942. function valid_for_assign(p:tnode;allowprop:boolean):boolean;
  943. var
  944. hp : tnode;
  945. gotwith,
  946. gotsubscript,
  947. gotpointer,
  948. gotclass,
  949. gotderef : boolean;
  950. begin
  951. valid_for_assign:=false;
  952. gotsubscript:=false;
  953. gotderef:=false;
  954. gotclass:=false;
  955. gotpointer:=false;
  956. gotwith:=false;
  957. hp:=p;
  958. while assigned(hp) do
  959. begin
  960. { property allowed? calln has a property check itself }
  961. if (not allowprop) and
  962. (nf_isproperty in hp.flags) and
  963. (hp.nodetype<>calln) then
  964. begin
  965. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  966. exit;
  967. end;
  968. case hp.nodetype of
  969. derefn :
  970. begin
  971. gotderef:=true;
  972. hp:=tderefnode(hp).left;
  973. end;
  974. typeconvn :
  975. begin
  976. case hp.resulttype^.deftype of
  977. pointerdef :
  978. gotpointer:=true;
  979. objectdef :
  980. gotclass:=pobjectdef(hp.resulttype)^.is_class;
  981. classrefdef :
  982. gotclass:=true;
  983. arraydef :
  984. begin
  985. { pointer -> array conversion is done then we need to see it
  986. as a deref, because a ^ is then not required anymore }
  987. if (ttypeconvnode(hp).left.resulttype^.deftype=pointerdef) then
  988. gotderef:=true;
  989. end;
  990. end;
  991. hp:=ttypeconvnode(hp).left;
  992. end;
  993. vecn,
  994. asn :
  995. hp:=tunarynode(hp).left;
  996. subscriptn :
  997. begin
  998. gotsubscript:=true;
  999. hp:=tsubscriptnode(hp).left;
  1000. end;
  1001. subn,
  1002. addn :
  1003. begin
  1004. { Allow add/sub operators on a pointer, or an integer
  1005. and a pointer typecast and deref has been found }
  1006. if (hp.resulttype^.deftype=pointerdef) or
  1007. (is_integer(hp.resulttype) and gotpointer and gotderef) then
  1008. valid_for_assign:=true
  1009. else
  1010. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1011. exit;
  1012. end;
  1013. addrn :
  1014. begin
  1015. if not(gotderef) and
  1016. not(nf_procvarload in hp.flags) then
  1017. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  1018. exit;
  1019. end;
  1020. selfn,
  1021. funcretn :
  1022. begin
  1023. valid_for_assign:=true;
  1024. exit;
  1025. end;
  1026. calln :
  1027. begin
  1028. { check return type }
  1029. case hp.resulttype^.deftype of
  1030. pointerdef :
  1031. gotpointer:=true;
  1032. objectdef :
  1033. gotclass:=pobjectdef(hp.resulttype)^.is_class;
  1034. recorddef, { handle record like class it needs a subscription }
  1035. classrefdef :
  1036. gotclass:=true;
  1037. end;
  1038. { 1. if it returns a pointer and we've found a deref,
  1039. 2. if it returns a class or record and a subscription or with is found,
  1040. 3. property is allowed }
  1041. if (gotpointer and gotderef) or
  1042. (gotclass and (gotsubscript or gotwith)) or
  1043. ((nf_isproperty in hp.flags) and allowprop) then
  1044. valid_for_assign:=true
  1045. else
  1046. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  1047. exit;
  1048. end;
  1049. loadn :
  1050. begin
  1051. case tloadnode(hp).symtableentry^.typ of
  1052. absolutesym,
  1053. varsym :
  1054. begin
  1055. if (pvarsym(tloadnode(hp).symtableentry)^.varspez=vs_const) then
  1056. begin
  1057. { allow p^:= constructions with p is const parameter }
  1058. if gotderef then
  1059. valid_for_assign:=true
  1060. else
  1061. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1062. exit;
  1063. end;
  1064. { Are we at a with symtable, then we need to process the
  1065. withrefnode also to check for maybe a const load }
  1066. if (tloadnode(hp).symtable^.symtabletype=withsymtable) then
  1067. begin
  1068. { continue with processing the withref node }
  1069. hp:=tnode(pwithsymtable(tloadnode(hp).symtable)^.withrefnode);
  1070. gotwith:=true;
  1071. end
  1072. else
  1073. begin
  1074. { set the assigned flag for varsyms }
  1075. if (pvarsym(tloadnode(hp).symtableentry)^.varstate=vs_declared) then
  1076. pvarsym(tloadnode(hp).symtableentry)^.varstate:=vs_assigned;
  1077. valid_for_assign:=true;
  1078. exit;
  1079. end;
  1080. end;
  1081. funcretsym,
  1082. typedconstsym :
  1083. begin
  1084. valid_for_assign:=true;
  1085. exit;
  1086. end;
  1087. end;
  1088. end;
  1089. else
  1090. begin
  1091. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1092. exit;
  1093. end;
  1094. end;
  1095. end;
  1096. end;
  1097. {$else cg11}
  1098. {****************************************************************************
  1099. Convert
  1100. ****************************************************************************}
  1101. { Returns:
  1102. 0 - Not convertable
  1103. 1 - Convertable
  1104. 2 - Convertable, but not first choice }
  1105. function isconvertable(def_from,def_to : pdef;
  1106. var doconv : tconverttype;fromtreetype : ttreetyp;
  1107. explicit : boolean) : byte;
  1108. { Tbasetype: uauto,uvoid,uchar,
  1109. u8bit,u16bit,u32bit,
  1110. s8bit,s16bit,s32,
  1111. bool8bit,bool16bit,bool32bit,
  1112. u64bit,s64bitint }
  1113. type
  1114. tbasedef=(bvoid,bchar,bint,bbool);
  1115. const
  1116. basedeftbl:array[tbasetype] of tbasedef =
  1117. (bvoid,bvoid,bchar,
  1118. bint,bint,bint,
  1119. bint,bint,bint,
  1120. bbool,bbool,bbool,bint,bint,bchar);
  1121. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1122. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1123. (tc_not_possible,tc_equal,tc_not_possible,tc_not_possible),
  1124. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1125. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1126. var
  1127. b : byte;
  1128. hd1,hd2 : pdef;
  1129. hct : tconverttype;
  1130. begin
  1131. { safety check }
  1132. if not(assigned(def_from) and assigned(def_to)) then
  1133. begin
  1134. isconvertable:=0;
  1135. exit;
  1136. end;
  1137. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1138. procvar is passed explicit a addrn would be there }
  1139. if (m_tp_procvar in aktmodeswitches) and
  1140. (def_from^.deftype=procvardef) and
  1141. (fromtreetype=loadn) then
  1142. begin
  1143. def_from:=pprocvardef(def_from)^.rettype.def;
  1144. end;
  1145. { we walk the wanted (def_to) types and check then the def_from
  1146. types if there is a conversion possible }
  1147. b:=0;
  1148. case def_to^.deftype of
  1149. orddef :
  1150. begin
  1151. case def_from^.deftype of
  1152. orddef :
  1153. begin
  1154. doconv:=basedefconverts[basedeftbl[porddef(def_from)^.typ],basedeftbl[porddef(def_to)^.typ]];
  1155. b:=1;
  1156. if (doconv=tc_not_possible) or
  1157. ((doconv=tc_int_2_bool) and
  1158. (not explicit) and
  1159. (not is_boolean(def_from))) or
  1160. ((doconv=tc_bool_2_int) and
  1161. (not explicit) and
  1162. (not is_boolean(def_to))) then
  1163. b:=0;
  1164. end;
  1165. enumdef :
  1166. begin
  1167. { needed for char(enum) }
  1168. if explicit then
  1169. begin
  1170. doconv:=tc_int_2_int;
  1171. b:=1;
  1172. end;
  1173. end;
  1174. end;
  1175. end;
  1176. stringdef :
  1177. begin
  1178. case def_from^.deftype of
  1179. stringdef :
  1180. begin
  1181. doconv:=tc_string_2_string;
  1182. b:=1;
  1183. end;
  1184. orddef :
  1185. begin
  1186. { char to string}
  1187. if is_char(def_from) then
  1188. begin
  1189. doconv:=tc_char_2_string;
  1190. b:=1;
  1191. end;
  1192. end;
  1193. arraydef :
  1194. begin
  1195. { array of char to string, the length check is done by the firstpass of this node }
  1196. if is_chararray(def_from) then
  1197. begin
  1198. doconv:=tc_chararray_2_string;
  1199. if (not(cs_ansistrings in aktlocalswitches) and
  1200. is_shortstring(def_to)) or
  1201. ((cs_ansistrings in aktlocalswitches) and
  1202. is_ansistring(def_to)) then
  1203. b:=1
  1204. else
  1205. b:=2;
  1206. end;
  1207. end;
  1208. pointerdef :
  1209. begin
  1210. { pchar can be assigned to short/ansistrings,
  1211. but not in tp7 compatible mode }
  1212. if is_pchar(def_from) and not(m_tp7 in aktmodeswitches) then
  1213. begin
  1214. doconv:=tc_pchar_2_string;
  1215. b:=1;
  1216. end;
  1217. end;
  1218. end;
  1219. end;
  1220. floatdef :
  1221. begin
  1222. case def_from^.deftype of
  1223. orddef :
  1224. begin { ordinal to real }
  1225. if is_integer(def_from) then
  1226. begin
  1227. if pfloatdef(def_to)^.typ=f32bit then
  1228. doconv:=tc_int_2_fix
  1229. else
  1230. doconv:=tc_int_2_real;
  1231. b:=1;
  1232. end;
  1233. end;
  1234. floatdef :
  1235. begin { 2 float types ? }
  1236. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  1237. doconv:=tc_equal
  1238. else
  1239. begin
  1240. if pfloatdef(def_from)^.typ=f32bit then
  1241. doconv:=tc_fix_2_real
  1242. else
  1243. if pfloatdef(def_to)^.typ=f32bit then
  1244. doconv:=tc_real_2_fix
  1245. else
  1246. doconv:=tc_real_2_real;
  1247. end;
  1248. b:=1;
  1249. end;
  1250. end;
  1251. end;
  1252. enumdef :
  1253. begin
  1254. if (def_from^.deftype=enumdef) then
  1255. begin
  1256. hd1:=def_from;
  1257. while assigned(penumdef(hd1)^.basedef) do
  1258. hd1:=penumdef(hd1)^.basedef;
  1259. hd2:=def_to;
  1260. while assigned(penumdef(hd2)^.basedef) do
  1261. hd2:=penumdef(hd2)^.basedef;
  1262. if (hd1=hd2) then
  1263. begin
  1264. b:=1;
  1265. { because of packenum they can have different sizes! (JM) }
  1266. doconv:=tc_int_2_int;
  1267. end;
  1268. end;
  1269. end;
  1270. arraydef :
  1271. begin
  1272. { open array is also compatible with a single element of its base type }
  1273. if is_open_array(def_to) and
  1274. is_equal(parraydef(def_to)^.elementtype.def,def_from) then
  1275. begin
  1276. doconv:=tc_equal;
  1277. b:=1;
  1278. end
  1279. else
  1280. begin
  1281. case def_from^.deftype of
  1282. arraydef :
  1283. begin
  1284. { array constructor -> open array }
  1285. if is_open_array(def_to) and
  1286. is_array_constructor(def_from) then
  1287. begin
  1288. if is_void(parraydef(def_from)^.elementtype.def) or
  1289. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1290. begin
  1291. doconv:=tc_equal;
  1292. b:=1;
  1293. end
  1294. else
  1295. if isconvertable(parraydef(def_from)^.elementtype.def,
  1296. parraydef(def_to)^.elementtype.def,hct,arrayconstructn,false)<>0 then
  1297. begin
  1298. doconv:=hct;
  1299. b:=2;
  1300. end;
  1301. end;
  1302. end;
  1303. pointerdef :
  1304. begin
  1305. if is_zero_based_array(def_to) and
  1306. is_equal(ppointerdef(def_from)^.pointertype.def,parraydef(def_to)^.elementtype.def) then
  1307. begin
  1308. doconv:=tc_pointer_2_array;
  1309. b:=1;
  1310. end;
  1311. end;
  1312. stringdef :
  1313. begin
  1314. { string to array of char}
  1315. if (not(is_special_array(def_to)) or is_open_array(def_to)) and
  1316. is_equal(parraydef(def_to)^.elementtype.def,cchardef) then
  1317. begin
  1318. doconv:=tc_string_2_chararray;
  1319. b:=1;
  1320. end;
  1321. end;
  1322. end;
  1323. end;
  1324. end;
  1325. pointerdef :
  1326. begin
  1327. case def_from^.deftype of
  1328. stringdef :
  1329. begin
  1330. { string constant (which can be part of array constructor)
  1331. to zero terminated string constant }
  1332. if (fromtreetype in [arrayconstructn,stringconstn]) and
  1333. is_pchar(def_to) then
  1334. begin
  1335. doconv:=tc_cstring_2_pchar;
  1336. b:=1;
  1337. end;
  1338. end;
  1339. orddef :
  1340. begin
  1341. { char constant to zero terminated string constant }
  1342. if (fromtreetype=ordconstn) then
  1343. begin
  1344. if is_equal(def_from,cchardef) and
  1345. is_pchar(def_to) then
  1346. begin
  1347. doconv:=tc_cchar_2_pchar;
  1348. b:=1;
  1349. end
  1350. else
  1351. if is_integer(def_from) then
  1352. begin
  1353. doconv:=tc_cord_2_pointer;
  1354. b:=1;
  1355. end;
  1356. end;
  1357. end;
  1358. arraydef :
  1359. begin
  1360. { chararray to pointer }
  1361. if is_zero_based_array(def_from) and
  1362. is_equal(parraydef(def_from)^.elementtype.def,ppointerdef(def_to)^.pointertype.def) then
  1363. begin
  1364. doconv:=tc_array_2_pointer;
  1365. b:=1;
  1366. end;
  1367. end;
  1368. pointerdef :
  1369. begin
  1370. { child class pointer can be assigned to anchestor pointers }
  1371. if (
  1372. (ppointerdef(def_from)^.pointertype.def^.deftype=objectdef) and
  1373. (ppointerdef(def_to)^.pointertype.def^.deftype=objectdef) and
  1374. pobjectdef(ppointerdef(def_from)^.pointertype.def)^.is_related(
  1375. pobjectdef(ppointerdef(def_to)^.pointertype.def))
  1376. ) or
  1377. { all pointers can be assigned to void-pointer }
  1378. is_equal(ppointerdef(def_to)^.pointertype.def,voiddef) or
  1379. { in my opnion, is this not clean pascal }
  1380. { well, but it's handy to use, it isn't ? (FK) }
  1381. is_equal(ppointerdef(def_from)^.pointertype.def,voiddef) then
  1382. begin
  1383. doconv:=tc_equal;
  1384. b:=1;
  1385. end;
  1386. end;
  1387. procvardef :
  1388. begin
  1389. { procedure variable can be assigned to an void pointer }
  1390. { Not anymore. Use the @ operator now.}
  1391. if not(m_tp_procvar in aktmodeswitches) and
  1392. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1393. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1394. begin
  1395. doconv:=tc_equal;
  1396. b:=1;
  1397. end;
  1398. end;
  1399. classrefdef,
  1400. objectdef :
  1401. begin
  1402. { class types and class reference type
  1403. can be assigned to void pointers }
  1404. if (
  1405. ((def_from^.deftype=objectdef) and pobjectdef(def_from)^.is_class) or
  1406. (def_from^.deftype=classrefdef)
  1407. ) and
  1408. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1409. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1410. begin
  1411. doconv:=tc_equal;
  1412. b:=1;
  1413. end;
  1414. end;
  1415. end;
  1416. end;
  1417. setdef :
  1418. begin
  1419. { automatic arrayconstructor -> set conversion }
  1420. if is_array_constructor(def_from) then
  1421. begin
  1422. doconv:=tc_arrayconstructor_2_set;
  1423. b:=1;
  1424. end;
  1425. end;
  1426. procvardef :
  1427. begin
  1428. { proc -> procvar }
  1429. if (def_from^.deftype=procdef) then
  1430. begin
  1431. doconv:=tc_proc_2_procvar;
  1432. if proc_to_procvar_equal(pprocdef(def_from),pprocvardef(def_to)) then
  1433. b:=1;
  1434. end
  1435. else
  1436. { for example delphi allows the assignement from pointers }
  1437. { to procedure variables }
  1438. if (m_pointer_2_procedure in aktmodeswitches) and
  1439. (def_from^.deftype=pointerdef) and
  1440. (ppointerdef(def_from)^.pointertype.def^.deftype=orddef) and
  1441. (porddef(ppointerdef(def_from)^.pointertype.def)^.typ=uvoid) then
  1442. begin
  1443. doconv:=tc_equal;
  1444. b:=1;
  1445. end
  1446. else
  1447. { nil is compatible with procvars }
  1448. if (fromtreetype=niln) then
  1449. begin
  1450. doconv:=tc_equal;
  1451. b:=1;
  1452. end;
  1453. end;
  1454. objectdef :
  1455. begin
  1456. { object pascal objects }
  1457. if (def_from^.deftype=objectdef) {and
  1458. pobjectdef(def_from)^.isclass and pobjectdef(def_to)^.isclass }then
  1459. begin
  1460. doconv:=tc_equal;
  1461. if pobjectdef(def_from)^.is_related(pobjectdef(def_to)) then
  1462. b:=1;
  1463. end
  1464. else
  1465. { Class specific }
  1466. if (pobjectdef(def_to)^.is_class) then
  1467. begin
  1468. { void pointer also for delphi mode }
  1469. if (m_delphi in aktmodeswitches) and
  1470. is_voidpointer(def_from) then
  1471. begin
  1472. doconv:=tc_equal;
  1473. b:=1;
  1474. end
  1475. else
  1476. { nil is compatible with class instances }
  1477. if (fromtreetype=niln) and (pobjectdef(def_to)^.is_class) then
  1478. begin
  1479. doconv:=tc_equal;
  1480. b:=1;
  1481. end;
  1482. end;
  1483. end;
  1484. classrefdef :
  1485. begin
  1486. { class reference types }
  1487. if (def_from^.deftype=classrefdef) then
  1488. begin
  1489. doconv:=tc_equal;
  1490. if pobjectdef(pclassrefdef(def_from)^.pointertype.def)^.is_related(
  1491. pobjectdef(pclassrefdef(def_to)^.pointertype.def)) then
  1492. b:=1;
  1493. end
  1494. else
  1495. { nil is compatible with class references }
  1496. if (fromtreetype=niln) then
  1497. begin
  1498. doconv:=tc_equal;
  1499. b:=1;
  1500. end;
  1501. end;
  1502. filedef :
  1503. begin
  1504. { typed files are all equal to the abstract file type
  1505. name TYPEDFILE in system.pp in is_equal in types.pas
  1506. the problem is that it sholud be also compatible to FILE
  1507. but this would leed to a problem for ASSIGN RESET and REWRITE
  1508. when trying to find the good overloaded function !!
  1509. so all file function are doubled in system.pp
  1510. this is not very beautiful !!}
  1511. if (def_from^.deftype=filedef) and
  1512. (
  1513. (
  1514. (pfiledef(def_from)^.filetyp = ft_typed) and
  1515. (pfiledef(def_to)^.filetyp = ft_typed) and
  1516. (
  1517. (pfiledef(def_from)^.typedfiletype.def = pdef(voiddef)) or
  1518. (pfiledef(def_to)^.typedfiletype.def = pdef(voiddef))
  1519. )
  1520. ) or
  1521. (
  1522. (
  1523. (pfiledef(def_from)^.filetyp = ft_untyped) and
  1524. (pfiledef(def_to)^.filetyp = ft_typed)
  1525. ) or
  1526. (
  1527. (pfiledef(def_from)^.filetyp = ft_typed) and
  1528. (pfiledef(def_to)^.filetyp = ft_untyped)
  1529. )
  1530. )
  1531. ) then
  1532. begin
  1533. doconv:=tc_equal;
  1534. b:=1;
  1535. end
  1536. end;
  1537. else
  1538. begin
  1539. { assignment overwritten ?? }
  1540. if assignment_overloaded(def_from,def_to)<>nil then
  1541. b:=2;
  1542. end;
  1543. end;
  1544. isconvertable:=b;
  1545. end;
  1546. { ld is the left type definition
  1547. rd the right type definition
  1548. dd the result type definition or voiddef if unkown }
  1549. function isbinaryoperatoroverloadable(ld, rd, dd : pdef;
  1550. treetyp : ttreetyp) : boolean;
  1551. begin
  1552. isbinaryoperatoroverloadable:=
  1553. (treetyp=starstarn) or
  1554. (ld^.deftype=recorddef) or
  1555. (rd^.deftype=recorddef) or
  1556. ((rd^.deftype=pointerdef) and
  1557. not(is_pchar(rd) and
  1558. (is_chararray(ld) or
  1559. (ld^.deftype=stringdef) or
  1560. (treetyp=addn))) and
  1561. (not(ld^.deftype in [pointerdef,objectdef,classrefdef,procvardef]) or
  1562. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,subn])
  1563. ) and
  1564. (not is_integer(ld) or not (treetyp in [addn,subn]))
  1565. ) or
  1566. ((ld^.deftype=pointerdef) and
  1567. not(is_pchar(ld) and
  1568. (is_chararray(rd) or
  1569. (rd^.deftype=stringdef) or
  1570. (treetyp=addn))) and
  1571. (not(rd^.deftype in [stringdef,pointerdef,objectdef,classrefdef,procvardef]) and
  1572. ((not is_integer(rd) and (rd^.deftype<>objectdef)
  1573. and (rd^.deftype<>classrefdef)) or
  1574. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn])
  1575. )
  1576. )
  1577. ) or
  1578. { array def, but not mmx or chararray+[char,string,chararray] }
  1579. ((ld^.deftype=arraydef) and
  1580. not((cs_mmx in aktlocalswitches) and
  1581. is_mmx_able_array(ld)) and
  1582. not(is_chararray(ld) and
  1583. (is_char(rd) or
  1584. is_pchar(rd) or
  1585. (rd^.deftype=stringdef) or
  1586. is_chararray(rd)))
  1587. ) or
  1588. ((rd^.deftype=arraydef) and
  1589. not((cs_mmx in aktlocalswitches) and
  1590. is_mmx_able_array(rd)) and
  1591. not(is_chararray(rd) and
  1592. (is_char(ld) or
  1593. is_pchar(ld) or
  1594. (ld^.deftype=stringdef) or
  1595. is_chararray(ld)))
  1596. ) or
  1597. { <> and = are defined for classes }
  1598. ((ld^.deftype=objectdef) and
  1599. (not(pobjectdef(ld)^.is_class) or
  1600. not(treetyp in [equaln,unequaln])
  1601. )
  1602. ) or
  1603. ((rd^.deftype=objectdef) and
  1604. (not(pobjectdef(rd)^.is_class) or
  1605. not(treetyp in [equaln,unequaln])
  1606. )
  1607. or
  1608. { allow other operators that + on strings }
  1609. (
  1610. (is_char(rd) or
  1611. is_pchar(rd) or
  1612. (rd^.deftype=stringdef) or
  1613. is_chararray(rd) or
  1614. is_char(ld) or
  1615. is_pchar(ld) or
  1616. (ld^.deftype=stringdef) or
  1617. is_chararray(ld)
  1618. ) and
  1619. not(treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1620. not(is_pchar(ld) and
  1621. (is_integer(rd) or (rd^.deftype=pointerdef)) and
  1622. (treetyp=subn)
  1623. )
  1624. )
  1625. );
  1626. end;
  1627. function isunaryoperatoroverloadable(rd,dd : pdef;
  1628. treetyp : ttreetyp) : boolean;
  1629. begin
  1630. isunaryoperatoroverloadable:=false;
  1631. { what assignment overloading should be allowed ?? }
  1632. if (treetyp=assignn) then
  1633. begin
  1634. isunaryoperatoroverloadable:=true;
  1635. { this already get tbs0261 to fail
  1636. isunaryoperatoroverloadable:=not is_equal(rd,dd); PM }
  1637. end
  1638. { should we force that rd and dd are equal ?? }
  1639. else if (treetyp=subn { unaryminusn }) then
  1640. begin
  1641. isunaryoperatoroverloadable:=
  1642. not is_integer(rd) and not (rd^.deftype=floatdef)
  1643. {$ifdef SUPPORT_MMX}
  1644. and not ((cs_mmx in aktlocalswitches) and
  1645. is_mmx_able_array(rd))
  1646. {$endif SUPPORT_MMX}
  1647. ;
  1648. end
  1649. else if (treetyp=notn) then
  1650. begin
  1651. isunaryoperatoroverloadable:=not is_integer(rd) and not is_boolean(rd)
  1652. {$ifdef SUPPORT_MMX}
  1653. and not ((cs_mmx in aktlocalswitches) and
  1654. is_mmx_able_array(rd))
  1655. {$endif SUPPORT_MMX}
  1656. ;
  1657. end;
  1658. end;
  1659. function isoperatoracceptable(pf : pprocdef; optoken : ttoken) : boolean;
  1660. var
  1661. ld,rd,dd : pdef;
  1662. i : longint;
  1663. begin
  1664. case pf^.parast^.symindex^.count of
  1665. 2 : begin
  1666. isoperatoracceptable:=false;
  1667. for i:=1 to tok2nodes do
  1668. if tok2node[i].tok=optoken then
  1669. begin
  1670. ld:=pvarsym(pf^.parast^.symindex^.first)^.vartype.def;
  1671. rd:=pvarsym(pf^.parast^.symindex^.first^.indexnext)^.vartype.def;
  1672. dd:=pf^.rettype.def;
  1673. isoperatoracceptable:=
  1674. tok2node[i].op_overloading_supported and
  1675. isbinaryoperatoroverloadable(ld,rd,dd,tok2node[i].nod);
  1676. break;
  1677. end;
  1678. end;
  1679. 1 : begin
  1680. rd:=pvarsym(pf^.parast^.symindex^.first)^.vartype.def;
  1681. dd:=pf^.rettype.def;
  1682. for i:=1 to tok2nodes do
  1683. if tok2node[i].tok=optoken then
  1684. begin
  1685. isoperatoracceptable:=
  1686. tok2node[i].op_overloading_supported and
  1687. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  1688. break;
  1689. end;
  1690. end;
  1691. else
  1692. isoperatoracceptable:=false;
  1693. end;
  1694. end;
  1695. {****************************************************************************
  1696. Register Calculation
  1697. ****************************************************************************}
  1698. { marks an lvalue as "unregable" }
  1699. procedure make_not_regable(p : ptree);
  1700. begin
  1701. case p^.treetype of
  1702. typeconvn :
  1703. make_not_regable(p^.left);
  1704. loadn :
  1705. if p^.symtableentry^.typ=varsym then
  1706. pvarsym(p^.symtableentry)^.varoptions:=pvarsym(p^.symtableentry)^.varoptions-[vo_regable,vo_fpuregable];
  1707. end;
  1708. end;
  1709. procedure left_right_max(p : ptree);
  1710. begin
  1711. if assigned(p^.left) then
  1712. begin
  1713. if assigned(p^.right) then
  1714. begin
  1715. p^.registers32:=max(p^.left^.registers32,p^.right^.registers32);
  1716. p^.registersfpu:=max(p^.left^.registersfpu,p^.right^.registersfpu);
  1717. {$ifdef SUPPORT_MMX}
  1718. p^.registersmmx:=max(p^.left^.registersmmx,p^.right^.registersmmx);
  1719. {$endif SUPPORT_MMX}
  1720. end
  1721. else
  1722. begin
  1723. p^.registers32:=p^.left^.registers32;
  1724. p^.registersfpu:=p^.left^.registersfpu;
  1725. {$ifdef SUPPORT_MMX}
  1726. p^.registersmmx:=p^.left^.registersmmx;
  1727. {$endif SUPPORT_MMX}
  1728. end;
  1729. end;
  1730. end;
  1731. { calculates the needed registers for a binary operator }
  1732. procedure calcregisters(p : ptree;r32,fpu,mmx : word);
  1733. begin
  1734. left_right_max(p);
  1735. { Only when the difference between the left and right registers < the
  1736. wanted registers allocate the amount of registers }
  1737. if assigned(p^.left) then
  1738. begin
  1739. if assigned(p^.right) then
  1740. begin
  1741. if (abs(p^.left^.registers32-p^.right^.registers32)<r32) then
  1742. inc(p^.registers32,r32);
  1743. if (abs(p^.left^.registersfpu-p^.right^.registersfpu)<fpu) then
  1744. inc(p^.registersfpu,fpu);
  1745. {$ifdef SUPPORT_MMX}
  1746. if (abs(p^.left^.registersmmx-p^.right^.registersmmx)<mmx) then
  1747. inc(p^.registersmmx,mmx);
  1748. {$endif SUPPORT_MMX}
  1749. { the following is a little bit guessing but I think }
  1750. { it's the only way to solve same internalerrors: }
  1751. { if the left and right node both uses registers }
  1752. { and return a mem location, but the current node }
  1753. { doesn't use an integer register we get probably }
  1754. { trouble when restoring a node }
  1755. if (p^.left^.registers32=p^.right^.registers32) and
  1756. (p^.registers32=p^.left^.registers32) and
  1757. (p^.registers32>0) and
  1758. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) and
  1759. (p^.right^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1760. inc(p^.registers32);
  1761. end
  1762. else
  1763. begin
  1764. if (p^.left^.registers32<r32) then
  1765. inc(p^.registers32,r32);
  1766. if (p^.left^.registersfpu<fpu) then
  1767. inc(p^.registersfpu,fpu);
  1768. {$ifdef SUPPORT_MMX}
  1769. if (p^.left^.registersmmx<mmx) then
  1770. inc(p^.registersmmx,mmx);
  1771. {$endif SUPPORT_MMX}
  1772. end;
  1773. end;
  1774. { error CGMessage, if more than 8 floating point }
  1775. { registers are needed }
  1776. if p^.registersfpu>8 then
  1777. CGMessage(cg_e_too_complex_expr);
  1778. end;
  1779. {****************************************************************************
  1780. Subroutine Handling
  1781. ****************************************************************************}
  1782. { protected field handling
  1783. protected field can not appear in
  1784. var parameters of function !!
  1785. this can only be done after we have determined the
  1786. overloaded function
  1787. this is the reason why it is not in the parser, PM }
  1788. procedure test_protected_sym(sym : psym);
  1789. begin
  1790. if (sp_protected in sym^.symoptions) and
  1791. ((sym^.owner^.symtabletype=unitsymtable) or
  1792. ((sym^.owner^.symtabletype=objectsymtable) and
  1793. (pobjectdef(sym^.owner^.defowner)^.owner^.symtabletype=unitsymtable))
  1794. ) then
  1795. CGMessage(parser_e_cant_access_protected_member);
  1796. end;
  1797. procedure test_protected(p : ptree);
  1798. begin
  1799. case p^.treetype of
  1800. loadn : test_protected_sym(p^.symtableentry);
  1801. typeconvn : test_protected(p^.left);
  1802. derefn : test_protected(p^.left);
  1803. subscriptn : begin
  1804. { test_protected(p^.left);
  1805. Is a field of a protected var
  1806. also protected ??? PM }
  1807. test_protected_sym(p^.vs);
  1808. end;
  1809. end;
  1810. end;
  1811. function valid_for_formal_var(p : ptree) : boolean;
  1812. var
  1813. v : boolean;
  1814. begin
  1815. case p^.treetype of
  1816. loadn :
  1817. v:=(p^.symtableentry^.typ in [typedconstsym,varsym]);
  1818. typeconvn :
  1819. v:=valid_for_formal_var(p^.left);
  1820. derefn,
  1821. subscriptn,
  1822. vecn,
  1823. funcretn,
  1824. selfn :
  1825. v:=true;
  1826. calln : { procvars are callnodes first }
  1827. v:=assigned(p^.right) and not assigned(p^.left);
  1828. addrn :
  1829. begin
  1830. { addrn is not allowed as this generate a constant value,
  1831. but a tp procvar are allowed (PFV) }
  1832. if p^.procvarload then
  1833. v:=true
  1834. else
  1835. v:=false;
  1836. end;
  1837. else
  1838. v:=false;
  1839. end;
  1840. valid_for_formal_var:=v;
  1841. end;
  1842. function valid_for_formal_const(p : ptree) : boolean;
  1843. var
  1844. v : boolean;
  1845. begin
  1846. { p must have been firstpass'd before }
  1847. { accept about anything but not a statement ! }
  1848. case p^.treetype of
  1849. calln,
  1850. statementn,
  1851. addrn :
  1852. begin
  1853. { addrn is not allowed as this generate a constant value,
  1854. but a tp procvar are allowed (PFV) }
  1855. if p^.procvarload then
  1856. v:=true
  1857. else
  1858. v:=false;
  1859. end;
  1860. else
  1861. v:=true;
  1862. end;
  1863. valid_for_formal_const:=v;
  1864. end;
  1865. function is_procsym_load(p:Ptree):boolean;
  1866. begin
  1867. is_procsym_load:=((p^.treetype=loadn) and (p^.symtableentry^.typ=procsym)) or
  1868. ((p^.treetype=addrn) and (p^.left^.treetype=loadn)
  1869. and (p^.left^.symtableentry^.typ=procsym)) ;
  1870. end;
  1871. { change a proc call to a procload for assignment to a procvar }
  1872. { this can only happen for proc/function without arguments }
  1873. function is_procsym_call(p:Ptree):boolean;
  1874. begin
  1875. is_procsym_call:=(p^.treetype=calln) and (p^.left=nil) and
  1876. (((p^.symtableprocentry^.typ=procsym) and (p^.right=nil)) or
  1877. ((p^.right<>nil) and (p^.right^.symtableprocentry^.typ=varsym)));
  1878. end;
  1879. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  1880. var
  1881. passproc : pprocdef;
  1882. convtyp : tconverttype;
  1883. begin
  1884. assignment_overloaded:=nil;
  1885. if assigned(overloaded_operators[_assignment]) then
  1886. passproc:=overloaded_operators[_assignment]^.definition
  1887. else
  1888. exit;
  1889. while passproc<>nil do
  1890. begin
  1891. if is_equal(passproc^.rettype.def,to_def) and
  1892. (is_equal(pparaitem(passproc^.para^.first)^.paratype.def,from_def) or
  1893. (isconvertable(from_def,pparaitem(passproc^.para^.first)^.paratype.def,convtyp,ordconstn,false)=1)) then
  1894. begin
  1895. assignment_overloaded:=passproc;
  1896. break;
  1897. end;
  1898. passproc:=passproc^.nextoverloaded;
  1899. end;
  1900. end;
  1901. { local routines can't be assigned to procvars }
  1902. procedure test_local_to_procvar(from_def:pprocvardef;to_def:pdef);
  1903. begin
  1904. if (from_def^.symtablelevel>1) and (to_def^.deftype=procvardef) then
  1905. CGMessage(type_e_cannot_local_proc_to_procvar);
  1906. end;
  1907. function valid_for_assign(p:ptree;allowprop:boolean):boolean;
  1908. var
  1909. hp : ptree;
  1910. gotwith,
  1911. gotsubscript,
  1912. gotpointer,
  1913. gotclass,
  1914. gotderef : boolean;
  1915. begin
  1916. valid_for_assign:=false;
  1917. gotsubscript:=false;
  1918. gotderef:=false;
  1919. gotclass:=false;
  1920. gotpointer:=false;
  1921. gotwith:=false;
  1922. hp:=p;
  1923. while assigned(hp) do
  1924. begin
  1925. { property allowed? calln has a property check itself }
  1926. if (not allowprop) and
  1927. (hp^.isproperty) and
  1928. (hp^.treetype<>calln) then
  1929. begin
  1930. CGMessagePos(hp^.fileinfo,type_e_argument_cant_be_assigned);
  1931. exit;
  1932. end;
  1933. case hp^.treetype of
  1934. derefn :
  1935. begin
  1936. gotderef:=true;
  1937. hp:=hp^.left;
  1938. end;
  1939. typeconvn :
  1940. begin
  1941. case hp^.resulttype^.deftype of
  1942. pointerdef :
  1943. gotpointer:=true;
  1944. objectdef :
  1945. gotclass:=pobjectdef(hp^.resulttype)^.is_class;
  1946. classrefdef :
  1947. gotclass:=true;
  1948. arraydef :
  1949. begin
  1950. { pointer -> array conversion is done then we need to see it
  1951. as a deref, because a ^ is then not required anymore }
  1952. if (hp^.left^.resulttype^.deftype=pointerdef) then
  1953. gotderef:=true;
  1954. end;
  1955. end;
  1956. hp:=hp^.left;
  1957. end;
  1958. vecn,
  1959. asn :
  1960. hp:=hp^.left;
  1961. subscriptn :
  1962. begin
  1963. gotsubscript:=true;
  1964. hp:=hp^.left;
  1965. end;
  1966. subn,
  1967. addn :
  1968. begin
  1969. { Allow add/sub operators on a pointer, or an integer
  1970. and a pointer typecast and deref has been found }
  1971. if (hp^.resulttype^.deftype=pointerdef) or
  1972. (is_integer(hp^.resulttype) and gotpointer and gotderef) then
  1973. valid_for_assign:=true
  1974. else
  1975. CGMessagePos(hp^.fileinfo,type_e_variable_id_expected);
  1976. exit;
  1977. end;
  1978. addrn :
  1979. begin
  1980. if not(gotderef) and
  1981. not(hp^.procvarload) then
  1982. CGMessagePos(hp^.fileinfo,type_e_no_assign_to_addr);
  1983. exit;
  1984. end;
  1985. selfn,
  1986. funcretn :
  1987. begin
  1988. valid_for_assign:=true;
  1989. exit;
  1990. end;
  1991. calln :
  1992. begin
  1993. { check return type }
  1994. case hp^.resulttype^.deftype of
  1995. pointerdef :
  1996. gotpointer:=true;
  1997. objectdef :
  1998. gotclass:=pobjectdef(hp^.resulttype)^.is_class;
  1999. recorddef, { handle record like class it needs a subscription }
  2000. classrefdef :
  2001. gotclass:=true;
  2002. end;
  2003. { 1. if it returns a pointer and we've found a deref,
  2004. 2. if it returns a class or record and a subscription or with is found,
  2005. 3. property is allowed }
  2006. if (gotpointer and gotderef) or
  2007. (gotclass and (gotsubscript or gotwith)) or
  2008. (hp^.isproperty and allowprop) then
  2009. valid_for_assign:=true
  2010. else
  2011. CGMessagePos(hp^.fileinfo,type_e_argument_cant_be_assigned);
  2012. exit;
  2013. end;
  2014. loadn :
  2015. begin
  2016. case hp^.symtableentry^.typ of
  2017. absolutesym,
  2018. varsym :
  2019. begin
  2020. if (pvarsym(hp^.symtableentry)^.varspez=vs_const) then
  2021. begin
  2022. { allow p^:= constructions with p is const parameter }
  2023. if gotderef then
  2024. valid_for_assign:=true
  2025. else
  2026. CGMessagePos(hp^.fileinfo,type_e_no_assign_to_const);
  2027. exit;
  2028. end;
  2029. { Are we at a with symtable, then we need to process the
  2030. withrefnode also to check for maybe a const load }
  2031. if (hp^.symtable^.symtabletype=withsymtable) then
  2032. begin
  2033. { continue with processing the withref node }
  2034. hp:=ptree(pwithsymtable(hp^.symtable)^.withrefnode);
  2035. gotwith:=true;
  2036. end
  2037. else
  2038. begin
  2039. { set the assigned flag for varsyms }
  2040. if (pvarsym(hp^.symtableentry)^.varstate=vs_declared) then
  2041. pvarsym(hp^.symtableentry)^.varstate:=vs_assigned;
  2042. valid_for_assign:=true;
  2043. exit;
  2044. end;
  2045. end;
  2046. funcretsym,
  2047. typedconstsym :
  2048. begin
  2049. valid_for_assign:=true;
  2050. exit;
  2051. end;
  2052. end;
  2053. end;
  2054. else
  2055. begin
  2056. CGMessagePos(hp^.fileinfo,type_e_variable_id_expected);
  2057. exit;
  2058. end;
  2059. end;
  2060. end;
  2061. end;
  2062. {$endif cg11}
  2063. end.
  2064. {
  2065. $Log$
  2066. Revision 1.10 2000-09-29 15:45:23 florian
  2067. * make cycle fixed
  2068. Revision 1.9 2000/09/28 19:49:51 florian
  2069. *** empty log message ***
  2070. Revision 1.8 2000/09/27 18:14:31 florian
  2071. * fixed a lot of syntax errors in the n*.pas stuff
  2072. Revision 1.7 2000/09/26 20:06:13 florian
  2073. * hmm, still a lot of work to get things compilable
  2074. Revision 1.6 2000/09/24 15:06:17 peter
  2075. * use defines.inc
  2076. Revision 1.5 2000/08/27 16:11:51 peter
  2077. * moved some util functions from globals,cobjects to cutils
  2078. * splitted files into finput,fmodule
  2079. Revision 1.4 2000/08/16 18:33:53 peter
  2080. * splitted namedobjectitem.next into indexnext and listnext so it
  2081. can be used in both lists
  2082. * don't allow "word = word" type definitions (merged)
  2083. Revision 1.3 2000/08/07 11:31:04 jonas
  2084. * fixed bug in type conversions between enum subranges (it didn't take
  2085. the packenum directive into account)
  2086. + define PACKENUMFIXED symbol in options.pas
  2087. (merged from fixes branch)
  2088. Revision 1.2 2000/07/13 11:32:41 michael
  2089. + removed logs
  2090. }