htypechk.pas 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,
  23. node,
  24. symtype,symdef;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. op_overloading_supported : boolean;
  30. end;
  31. const
  32. tok2nodes=25;
  33. tok2node:array[1..tok2nodes] of ttok2noderec=(
  34. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  35. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  36. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  37. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  38. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  39. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  40. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  41. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  42. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  43. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  44. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  45. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  46. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  47. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  48. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  49. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  50. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  51. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  52. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  53. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  54. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  55. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  56. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  57. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  58. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  59. );
  60. const
  61. { firstcallparan without varspez we don't count the ref }
  62. {$ifdef extdebug}
  63. count_ref : boolean = true;
  64. {$endif def extdebug}
  65. get_para_resulttype : boolean = false;
  66. allow_array_constructor : boolean = false;
  67. { is overloading of this operator allowed for this
  68. binary operator }
  69. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  70. { is overloading of this operator allowed for this
  71. unary operator }
  72. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  73. { check operator args and result type }
  74. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  75. function isbinaryoverloaded(var t : tnode) : boolean;
  76. { Register Allocation }
  77. procedure make_not_regable(p : tnode);
  78. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  79. { subroutine handling }
  80. function is_procsym_load(p:tnode):boolean;
  81. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  82. {
  83. type
  84. tvarstaterequire = (vsr_can_be_undefined,vsr_must_be_valid,
  85. vsr_is_used_after,vsr_must_be_valid_and_is_used_after); }
  86. { sets varsym varstate field correctly }
  87. procedure unset_varstate(p : tnode);
  88. procedure set_varstate(p : tnode;must_be_valid : boolean);
  89. { sets the callunique flag, if the node is a vecn, }
  90. { takes care of type casts etc. }
  91. procedure set_unique(p : tnode);
  92. { sets funcret_is_valid to true, if p contains a funcref node }
  93. procedure set_funcret_is_valid(p : tnode);
  94. function valid_for_formal_var(p : tnode) : boolean;
  95. function valid_for_formal_const(p : tnode) : boolean;
  96. function valid_for_var(p:tnode):boolean;
  97. function valid_for_assignment(p:tnode):boolean;
  98. implementation
  99. uses
  100. globtype,systems,
  101. cutils,verbose,globals,
  102. symconst,symsym,symtable,
  103. defutil,defcmp,cpubase,
  104. ncnv,nld,
  105. nmem,ncal,nmat,
  106. cgbase
  107. ;
  108. type
  109. TValidAssign=(Valid_Property,Valid_Void);
  110. TValidAssigns=set of TValidAssign;
  111. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  112. begin
  113. { everything is possible, the exceptions will be
  114. handled below }
  115. isbinaryoperatoroverloadable:=false;
  116. { power ** is always possible }
  117. if (treetyp=starstarn) then
  118. begin
  119. isbinaryoperatoroverloadable:=true;
  120. exit;
  121. end;
  122. case ld.deftype of
  123. recorddef,
  124. variantdef :
  125. begin
  126. isbinaryoperatoroverloadable:=true;
  127. exit;
  128. end;
  129. procvardef :
  130. begin
  131. if (rd.deftype in [pointerdef,procdef,procvardef]) and
  132. (treetyp in [equaln,unequaln]) then
  133. begin
  134. isbinaryoperatoroverloadable:=false;
  135. exit;
  136. end;
  137. end;
  138. pointerdef :
  139. begin
  140. if (rd.deftype in [pointerdef,objectdef,classrefdef,procvardef]) and
  141. (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn]) then
  142. begin
  143. isbinaryoperatoroverloadable:=false;
  144. exit;
  145. end;
  146. { don't allow operations on pointer/integer }
  147. if is_integer(rd) then
  148. begin
  149. isbinaryoperatoroverloadable:=false;
  150. exit;
  151. end;
  152. { don't allow pchar+string }
  153. if is_pchar(ld) and
  154. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  155. (is_chararray(rd) or
  156. is_char(rd) or
  157. (rd.deftype=stringdef)) then
  158. begin
  159. isbinaryoperatoroverloadable:=false;
  160. exit;
  161. end;
  162. isbinaryoperatoroverloadable:=true;
  163. end;
  164. arraydef :
  165. begin
  166. { not mmx }
  167. if (cs_mmx in aktlocalswitches) and
  168. is_mmx_able_array(ld) then
  169. begin
  170. isbinaryoperatoroverloadable:=false;
  171. exit;
  172. end;
  173. { not chararray+[char,string,chararray] }
  174. if is_chararray(ld) and
  175. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  176. (is_char(rd) or
  177. is_pchar(rd) or
  178. is_integer(rd) or
  179. (rd.deftype=stringdef) or
  180. is_chararray(rd)) then
  181. begin
  182. isbinaryoperatoroverloadable:=false;
  183. exit;
  184. end;
  185. { dynamic array compare with niln }
  186. if is_dynamic_array(ld) and
  187. (rt=niln) and
  188. (treetyp in [equaln,unequaln]) then
  189. begin
  190. isbinaryoperatoroverloadable:=false;
  191. exit;
  192. end;
  193. isbinaryoperatoroverloadable:=true;
  194. end;
  195. objectdef :
  196. begin
  197. { <> and = are defined for classes }
  198. if (treetyp in [equaln,unequaln]) and
  199. is_class_or_interface(ld) then
  200. begin
  201. isbinaryoperatoroverloadable:=false;
  202. exit;
  203. end;
  204. isbinaryoperatoroverloadable:=true;
  205. end;
  206. stringdef :
  207. begin
  208. if ((rd.deftype=stringdef) or
  209. is_char(rd) or
  210. is_pchar(rd) or
  211. is_chararray(rd)) and
  212. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) then
  213. begin
  214. isbinaryoperatoroverloadable:=false;
  215. exit;
  216. end;
  217. isbinaryoperatoroverloadable:=true;
  218. end;
  219. end;
  220. { Also check the right def. There can be some duplicated code
  221. that is never reached. But to place everything in one big
  222. case is unmaintainable }
  223. case rd.deftype of
  224. recorddef,
  225. variantdef :
  226. begin
  227. isbinaryoperatoroverloadable:=true;
  228. exit;
  229. end;
  230. procvardef :
  231. begin
  232. if (ld.deftype in [pointerdef,procdef,procvardef]) and
  233. (treetyp in [equaln,unequaln]) then
  234. begin
  235. isbinaryoperatoroverloadable:=false;
  236. exit;
  237. end;
  238. isbinaryoperatoroverloadable:=true;
  239. end;
  240. pointerdef :
  241. begin
  242. if (ld.deftype in [pointerdef,objectdef,classrefdef,procvardef]) and
  243. (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn]) then
  244. begin
  245. isbinaryoperatoroverloadable:=false;
  246. exit;
  247. end;
  248. { don't allow operations on pointer/integer }
  249. if is_integer(ld) then
  250. begin
  251. isbinaryoperatoroverloadable:=false;
  252. exit;
  253. end;
  254. { don't allow pchar+string }
  255. if is_pchar(rd) and
  256. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  257. (is_chararray(ld) or
  258. is_char(ld) or
  259. (ld.deftype=stringdef)) then
  260. begin
  261. isbinaryoperatoroverloadable:=false;
  262. exit;
  263. end;
  264. isbinaryoperatoroverloadable:=true;
  265. end;
  266. arraydef :
  267. begin
  268. { not mmx }
  269. if (cs_mmx in aktlocalswitches) and
  270. is_mmx_able_array(rd) then
  271. begin
  272. isbinaryoperatoroverloadable:=false;
  273. exit;
  274. end;
  275. { not chararray+[char,string,chararray] }
  276. if is_chararray(rd) and
  277. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  278. (is_char(ld) or
  279. is_pchar(ld) or
  280. is_integer(ld) or
  281. (ld.deftype=stringdef) or
  282. is_chararray(ld)) then
  283. begin
  284. isbinaryoperatoroverloadable:=false;
  285. exit;
  286. end;
  287. { dynamic array compare with niln }
  288. if is_dynamic_array(rd) and
  289. (lt=niln) and
  290. (treetyp in [equaln,unequaln]) then
  291. begin
  292. isbinaryoperatoroverloadable:=false;
  293. exit;
  294. end;
  295. isbinaryoperatoroverloadable:=true;
  296. end;
  297. objectdef :
  298. begin
  299. { <> and = are defined for classes }
  300. if (treetyp in [equaln,unequaln]) and
  301. is_class_or_interface(rd) then
  302. begin
  303. isbinaryoperatoroverloadable:=false;
  304. exit;
  305. end;
  306. isbinaryoperatoroverloadable:=true;
  307. end;
  308. stringdef :
  309. begin
  310. if ((ld.deftype=stringdef) or
  311. is_char(ld) or
  312. is_pchar(ld) or
  313. is_chararray(ld)) and
  314. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) then
  315. begin
  316. isbinaryoperatoroverloadable:=false;
  317. exit;
  318. end;
  319. isbinaryoperatoroverloadable:=true;
  320. end;
  321. end;
  322. end;
  323. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  324. begin
  325. isunaryoperatoroverloadable:=false;
  326. case treetyp of
  327. assignn :
  328. begin
  329. if (rd.deftype=orddef) and
  330. (dd.deftype=orddef) then
  331. begin
  332. isunaryoperatoroverloadable:=false;
  333. exit;
  334. end;
  335. isunaryoperatoroverloadable:=true;
  336. end;
  337. subn :
  338. begin
  339. if is_integer(rd) or
  340. (rd.deftype=floatdef) then
  341. begin
  342. isunaryoperatoroverloadable:=false;
  343. exit;
  344. end;
  345. {$ifdef SUPPORT_MMX}
  346. if (cs_mmx in aktlocalswitches) and
  347. is_mmx_able_array(rd) then
  348. begin
  349. isunaryoperatoroverloadable:=false;
  350. exit;
  351. end;
  352. {$endif SUPPORT_MMX}
  353. isunaryoperatoroverloadable:=true;
  354. end;
  355. notn :
  356. begin
  357. if is_integer(rd) or
  358. is_boolean(rd) then
  359. begin
  360. isunaryoperatoroverloadable:=false;
  361. exit;
  362. end;
  363. {$ifdef SUPPORT_MMX}
  364. if (cs_mmx in aktlocalswitches) and
  365. is_mmx_able_array(rd) then
  366. begin
  367. isunaryoperatoroverloadable:=false;
  368. exit;
  369. end;
  370. {$endif SUPPORT_MMX}
  371. isunaryoperatoroverloadable:=true;
  372. end;
  373. end;
  374. end;
  375. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  376. var
  377. ld,rd,dd : tdef;
  378. i : longint;
  379. begin
  380. case pf.parast.symindex.count of
  381. 2 : begin
  382. isoperatoracceptable:=false;
  383. for i:=1 to tok2nodes do
  384. if tok2node[i].tok=optoken then
  385. begin
  386. ld:=tvarsym(pf.parast.symindex.first).vartype.def;
  387. rd:=tvarsym(pf.parast.symindex.first.indexnext).vartype.def;
  388. dd:=pf.rettype.def;
  389. isoperatoracceptable:=
  390. tok2node[i].op_overloading_supported and
  391. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  392. break;
  393. end;
  394. end;
  395. 1 : begin
  396. rd:=tvarsym(pf.parast.symindex.first).vartype.def;
  397. dd:=pf.rettype.def;
  398. for i:=1 to tok2nodes do
  399. if tok2node[i].tok=optoken then
  400. begin
  401. isoperatoracceptable:=
  402. tok2node[i].op_overloading_supported and
  403. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  404. break;
  405. end;
  406. end;
  407. else
  408. isoperatoracceptable:=false;
  409. end;
  410. end;
  411. function isbinaryoverloaded(var t : tnode) : boolean;
  412. var
  413. rd,ld : tdef;
  414. optoken : ttoken;
  415. operpd : tprocdef;
  416. ht : tnode;
  417. begin
  418. isbinaryoverloaded:=false;
  419. operpd:=nil;
  420. { load easier access variables }
  421. ld:=tbinarynode(t).left.resulttype.def;
  422. rd:=tbinarynode(t).right.resulttype.def;
  423. if isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  424. begin
  425. isbinaryoverloaded:=true;
  426. case t.nodetype of
  427. equaln,
  428. unequaln :
  429. optoken:=_EQUAL;
  430. addn:
  431. optoken:=_PLUS;
  432. subn:
  433. optoken:=_MINUS;
  434. muln:
  435. optoken:=_STAR;
  436. starstarn:
  437. optoken:=_STARSTAR;
  438. slashn:
  439. optoken:=_SLASH;
  440. ltn:
  441. optoken:=tokens._lt;
  442. gtn:
  443. optoken:=tokens._gt;
  444. lten:
  445. optoken:=_lte;
  446. gten:
  447. optoken:=_gte;
  448. symdifn :
  449. optoken:=_SYMDIF;
  450. modn :
  451. optoken:=_OP_MOD;
  452. orn :
  453. optoken:=_OP_OR;
  454. xorn :
  455. optoken:=_OP_XOR;
  456. andn :
  457. optoken:=_OP_AND;
  458. divn :
  459. optoken:=_OP_DIV;
  460. shln :
  461. optoken:=_OP_SHL;
  462. shrn :
  463. optoken:=_OP_SHR;
  464. else
  465. exit;
  466. end;
  467. { check if the operator contains overloaded procdefs }
  468. if overloaded_operators[optoken]=nil then
  469. begin
  470. CGMessage(parser_e_operator_not_overloaded);
  471. isbinaryoverloaded:=false;
  472. exit;
  473. end;
  474. { Check if the assignment is available, if not then
  475. give a message that the types are not compatible }
  476. if optoken in [_EQUAL] then
  477. begin
  478. operpd:=overloaded_operators[optoken].search_procdef_binary_operator(ld,rd);
  479. if not assigned(operpd) then
  480. begin
  481. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  482. isbinaryoverloaded:=false;
  483. exit;
  484. end;
  485. end;
  486. { the nil as symtable signs firstcalln that this is
  487. an overloaded operator }
  488. ht:=ccallnode.create(nil,overloaded_operators[optoken],nil,nil);
  489. inc(tcallnode(ht).symtableprocentry.refs);
  490. { we already know the procdef to use for equal, so it can
  491. skip the overload choosing in callnode.det_resulttype }
  492. if assigned(operpd) then
  493. tcallnode(ht).procdefinition:=operpd;
  494. { we need copies, because the originals will be destroyed when we give a }
  495. { changed node back to firstpass! (JM) }
  496. if assigned(tbinarynode(t).left) then
  497. if assigned(tbinarynode(t).right) then
  498. tcallnode(ht).left :=
  499. ccallparanode.create(tbinarynode(t).right.getcopy,
  500. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  501. else
  502. tcallnode(ht).left :=
  503. ccallparanode.create(nil,
  504. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  505. else if assigned(tbinarynode(t).right) then
  506. tcallnode(ht).left :=
  507. ccallparanode.create(tbinarynode(t).right.getcopy,
  508. ccallparanode.create(nil,nil));
  509. if t.nodetype=unequaln then
  510. ht:=cnotnode.create(ht);
  511. t:=ht;
  512. end;
  513. end;
  514. {****************************************************************************
  515. Register Calculation
  516. ****************************************************************************}
  517. { marks an lvalue as "unregable" }
  518. procedure make_not_regable(p : tnode);
  519. begin
  520. case p.nodetype of
  521. typeconvn :
  522. make_not_regable(ttypeconvnode(p).left);
  523. loadn :
  524. if tloadnode(p).symtableentry.typ=varsym then
  525. tvarsym(tloadnode(p).symtableentry).varoptions:=tvarsym(tloadnode(p).symtableentry).varoptions-[vo_regable,vo_fpuregable];
  526. end;
  527. end;
  528. { calculates the needed registers for a binary operator }
  529. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  530. begin
  531. p.left_right_max;
  532. { Only when the difference between the left and right registers < the
  533. wanted registers allocate the amount of registers }
  534. if assigned(p.left) then
  535. begin
  536. if assigned(p.right) then
  537. begin
  538. { the location must be already filled in because we need it to }
  539. { calculate the necessary number of registers (JM) }
  540. if p.location.loc = LOC_INVALID then
  541. internalerror(200110101);
  542. if (abs(p.left.registers32-p.right.registers32)<r32) or
  543. ((p.location.loc = LOC_FPUREGISTER) and
  544. (p.right.registersfpu <= p.left.registersfpu) and
  545. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  546. (p.left.registers32 < p.right.registers32)) then
  547. inc(p.registers32,r32);
  548. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  549. inc(p.registersfpu,fpu);
  550. {$ifdef SUPPORT_MMX}
  551. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  552. inc(p.registersmmx,mmx);
  553. {$endif SUPPORT_MMX}
  554. { the following is a little bit guessing but I think }
  555. { it's the only way to solve same internalerrors: }
  556. { if the left and right node both uses registers }
  557. { and return a mem location, but the current node }
  558. { doesn't use an integer register we get probably }
  559. { trouble when restoring a node }
  560. if (p.left.registers32=p.right.registers32) and
  561. (p.registers32=p.left.registers32) and
  562. (p.registers32>0) and
  563. (p.left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  564. (p.right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  565. inc(p.registers32);
  566. end
  567. else
  568. begin
  569. if (p.left.registers32<r32) then
  570. inc(p.registers32,r32);
  571. if (p.left.registersfpu<fpu) then
  572. inc(p.registersfpu,fpu);
  573. {$ifdef SUPPORT_MMX}
  574. if (p.left.registersmmx<mmx) then
  575. inc(p.registersmmx,mmx);
  576. {$endif SUPPORT_MMX}
  577. end;
  578. end;
  579. { error CGMessage, if more than 8 floating point }
  580. { registers are needed }
  581. { if p.registersfpu>maxfpuregs then
  582. CGMessage(cg_e_too_complex_expr); now pushed if needed PM }
  583. end;
  584. {****************************************************************************
  585. Subroutine Handling
  586. ****************************************************************************}
  587. function is_procsym_load(p:tnode):boolean;
  588. begin
  589. { ignore vecn,subscriptn }
  590. repeat
  591. case p.nodetype of
  592. vecn :
  593. p:=tvecnode(p).left;
  594. subscriptn :
  595. p:=tsubscriptnode(p).left;
  596. else
  597. break;
  598. end;
  599. until false;
  600. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry.typ=procsym)) or
  601. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  602. and (tloadnode(taddrnode(p).left).symtableentry.typ=procsym)) ;
  603. end;
  604. { local routines can't be assigned to procvars }
  605. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  606. begin
  607. if (from_def.symtablelevel>1) and (to_def.deftype=procvardef) then
  608. CGMessage(type_e_cannot_local_proc_to_procvar);
  609. end;
  610. procedure set_varstate(p : tnode;must_be_valid : boolean);
  611. var
  612. hsym : tvarsym;
  613. begin
  614. while assigned(p) do
  615. begin
  616. if (nf_varstateset in p.flags) then
  617. exit;
  618. include(p.flags,nf_varstateset);
  619. case p.nodetype of
  620. typeconvn :
  621. begin
  622. case ttypeconvnode(p).convtype of
  623. tc_cchar_2_pchar,
  624. tc_cstring_2_pchar,
  625. tc_array_2_pointer :
  626. must_be_valid:=false;
  627. tc_pchar_2_string,
  628. tc_pointer_2_array :
  629. must_be_valid:=true;
  630. end;
  631. p:=tunarynode(p).left;
  632. end;
  633. subscriptn :
  634. p:=tunarynode(p).left;
  635. vecn:
  636. begin
  637. set_varstate(tbinarynode(p).right,true);
  638. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  639. must_be_valid:=true;
  640. p:=tunarynode(p).left;
  641. end;
  642. { do not parse calln }
  643. calln :
  644. break;
  645. callparan :
  646. begin
  647. set_varstate(tbinarynode(p).right,must_be_valid);
  648. p:=tunarynode(p).left;
  649. end;
  650. loadn :
  651. begin
  652. if (tloadnode(p).symtableentry.typ=varsym) then
  653. begin
  654. hsym:=tvarsym(tloadnode(p).symtableentry);
  655. if must_be_valid and (nf_first in p.flags) then
  656. begin
  657. if (hsym.varstate=vs_declared_and_first_found) or
  658. (hsym.varstate=vs_set_but_first_not_passed) then
  659. begin
  660. if (assigned(hsym.owner) and
  661. assigned(aktprocsym) and
  662. (hsym.owner = aktprocdef.localst)) then
  663. begin
  664. if tloadnode(p).symtable.symtabletype=localsymtable then
  665. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  666. else
  667. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  668. end;
  669. end;
  670. end;
  671. if (nf_first in p.flags) then
  672. begin
  673. if hsym.varstate=vs_declared_and_first_found then
  674. begin
  675. { this can only happen at left of an assignment, no ? PM }
  676. if (parsing_para_level=0) and not must_be_valid then
  677. hsym.varstate:=vs_assigned
  678. else
  679. hsym.varstate:=vs_used;
  680. end
  681. else
  682. if hsym.varstate=vs_set_but_first_not_passed then
  683. hsym.varstate:=vs_used;
  684. exclude(p.flags,nf_first);
  685. end
  686. else
  687. begin
  688. if (hsym.varstate=vs_assigned) and
  689. (must_be_valid or (parsing_para_level>0) or
  690. (p.resulttype.def.deftype=procvardef)) then
  691. hsym.varstate:=vs_used;
  692. if (hsym.varstate=vs_declared_and_first_found) and
  693. (must_be_valid or (parsing_para_level>0) or
  694. (p.resulttype.def.deftype=procvardef)) then
  695. hsym.varstate:=vs_set_but_first_not_passed;
  696. end;
  697. end;
  698. break;
  699. end;
  700. funcretn:
  701. begin
  702. { no claim if setting higher return value_str }
  703. if must_be_valid and
  704. (lexlevel=tfuncretnode(p).funcretsym.owner.symtablelevel) and
  705. ((tfuncretnode(p).funcretsym.funcretstate=vs_declared) or
  706. ((nf_is_first_funcret in p.flags) and
  707. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found))) then
  708. begin
  709. CGMessage(sym_w_function_result_not_set);
  710. { avoid multiple warnings }
  711. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  712. end;
  713. if (nf_is_first_funcret in p.flags) and not must_be_valid then
  714. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  715. break;
  716. end;
  717. else
  718. break;
  719. end;{case }
  720. end;
  721. end;
  722. procedure unset_varstate(p : tnode);
  723. begin
  724. while assigned(p) do
  725. begin
  726. exclude(p.flags,nf_varstateset);
  727. case p.nodetype of
  728. typeconvn,
  729. subscriptn,
  730. vecn :
  731. p:=tunarynode(p).left;
  732. else
  733. break;
  734. end;
  735. end;
  736. end;
  737. procedure set_unique(p : tnode);
  738. begin
  739. while assigned(p) do
  740. begin
  741. case p.nodetype of
  742. vecn:
  743. begin
  744. include(p.flags,nf_callunique);
  745. break;
  746. end;
  747. typeconvn,
  748. subscriptn,
  749. derefn:
  750. p:=tunarynode(p).left;
  751. else
  752. break;
  753. end;
  754. end;
  755. end;
  756. procedure set_funcret_is_valid(p:tnode);
  757. begin
  758. while assigned(p) do
  759. begin
  760. case p.nodetype of
  761. funcretn:
  762. begin
  763. if (nf_is_first_funcret in p.flags) or
  764. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found) then
  765. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  766. break;
  767. end;
  768. vecn,
  769. {derefn,}
  770. typeconvn,
  771. subscriptn:
  772. p:=tunarynode(p).left;
  773. else
  774. break;
  775. end;
  776. end;
  777. end;
  778. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  779. var
  780. hp : tnode;
  781. gotwith,
  782. gotsubscript,
  783. gotpointer,
  784. gotvec,
  785. gotclass,
  786. gotderef : boolean;
  787. fromdef,
  788. todef : tdef;
  789. begin
  790. valid_for_assign:=false;
  791. gotsubscript:=false;
  792. gotvec:=false;
  793. gotderef:=false;
  794. gotclass:=false;
  795. gotpointer:=false;
  796. gotwith:=false;
  797. hp:=p;
  798. if not(valid_void in opts) and
  799. is_void(hp.resulttype.def) then
  800. begin
  801. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  802. exit;
  803. end;
  804. while assigned(hp) do
  805. begin
  806. { property allowed? calln has a property check itself }
  807. if (nf_isproperty in hp.flags) then
  808. begin
  809. if (valid_property in opts) then
  810. valid_for_assign:=true
  811. else
  812. begin
  813. { check return type }
  814. case hp.resulttype.def.deftype of
  815. pointerdef :
  816. gotpointer:=true;
  817. objectdef :
  818. gotclass:=is_class_or_interface(hp.resulttype.def);
  819. recorddef, { handle record like class it needs a subscription }
  820. classrefdef :
  821. gotclass:=true;
  822. end;
  823. { 1. if it returns a pointer and we've found a deref,
  824. 2. if it returns a class or record and a subscription or with is found }
  825. if (gotpointer and gotderef) or
  826. (gotclass and (gotsubscript or gotwith)) then
  827. valid_for_assign:=true
  828. else
  829. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  830. end;
  831. exit;
  832. end;
  833. case hp.nodetype of
  834. temprefn :
  835. begin
  836. valid_for_assign := true;
  837. exit;
  838. end;
  839. derefn :
  840. begin
  841. gotderef:=true;
  842. hp:=tderefnode(hp).left;
  843. end;
  844. typeconvn :
  845. begin
  846. { typecast sizes must match, exceptions:
  847. - from formaldef
  848. - from void
  849. - typecast from pointer to array }
  850. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  851. todef:=hp.resulttype.def;
  852. if not((fromdef.deftype=formaldef) or
  853. is_void(fromdef) or
  854. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  855. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  856. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  857. (fromdef.size<>todef.size) then
  858. begin
  859. { in TP it is allowed to typecast to smaller types }
  860. if not(m_tp7 in aktmodeswitches) or
  861. (todef.size>fromdef.size) then
  862. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  863. end;
  864. case hp.resulttype.def.deftype of
  865. pointerdef :
  866. gotpointer:=true;
  867. objectdef :
  868. gotclass:=is_class_or_interface(hp.resulttype.def);
  869. classrefdef :
  870. gotclass:=true;
  871. arraydef :
  872. begin
  873. { pointer -> array conversion is done then we need to see it
  874. as a deref, because a ^ is then not required anymore }
  875. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  876. gotderef:=true;
  877. end;
  878. end;
  879. hp:=ttypeconvnode(hp).left;
  880. end;
  881. vecn :
  882. begin
  883. gotvec:=true;
  884. hp:=tunarynode(hp).left;
  885. end;
  886. asn :
  887. hp:=tunarynode(hp).left;
  888. subscriptn :
  889. begin
  890. gotsubscript:=true;
  891. { a class/interface access is an implicit }
  892. { dereferencing }
  893. hp:=tsubscriptnode(hp).left;
  894. if is_class_or_interface(hp.resulttype.def) then
  895. gotderef:=true;
  896. end;
  897. subn,
  898. addn :
  899. begin
  900. { Allow add/sub operators on a pointer, or an integer
  901. and a pointer typecast and deref has been found }
  902. if ((hp.resulttype.def.deftype=pointerdef) or
  903. (is_integer(hp.resulttype.def) and gotpointer)) and
  904. gotderef then
  905. valid_for_assign:=true
  906. else
  907. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  908. exit;
  909. end;
  910. addrn :
  911. begin
  912. if gotderef or
  913. (nf_procvarload in hp.flags) then
  914. valid_for_assign:=true
  915. else
  916. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  917. exit;
  918. end;
  919. selfn,
  920. funcretn :
  921. begin
  922. valid_for_assign:=true;
  923. exit;
  924. end;
  925. calln :
  926. begin
  927. { check return type }
  928. case hp.resulttype.def.deftype of
  929. arraydef :
  930. begin
  931. { dynamic arrays are allowed when there is also a
  932. vec node }
  933. if is_dynamic_array(hp.resulttype.def) and
  934. gotvec then
  935. begin
  936. gotderef:=true;
  937. gotpointer:=true;
  938. end;
  939. end;
  940. pointerdef :
  941. gotpointer:=true;
  942. objectdef :
  943. gotclass:=is_class_or_interface(hp.resulttype.def);
  944. recorddef, { handle record like class it needs a subscription }
  945. classrefdef :
  946. gotclass:=true;
  947. end;
  948. { 1. if it returns a pointer and we've found a deref,
  949. 2. if it returns a class or record and a subscription or with is found }
  950. if (gotpointer and gotderef) or
  951. (gotclass and (gotsubscript or gotwith)) then
  952. valid_for_assign:=true
  953. else
  954. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  955. exit;
  956. end;
  957. loadn :
  958. begin
  959. case tloadnode(hp).symtableentry.typ of
  960. absolutesym,
  961. varsym :
  962. begin
  963. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  964. begin
  965. { allow p^:= constructions with p is const parameter }
  966. if gotderef then
  967. valid_for_assign:=true
  968. else
  969. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  970. exit;
  971. end;
  972. { Are we at a with symtable, then we need to process the
  973. withrefnode also to check for maybe a const load }
  974. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  975. begin
  976. { continue with processing the withref node }
  977. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  978. gotwith:=true;
  979. end
  980. else
  981. begin
  982. { set the assigned flag for varsyms }
  983. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  984. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  985. valid_for_assign:=true;
  986. exit;
  987. end;
  988. end;
  989. funcretsym :
  990. begin
  991. valid_for_assign:=true;
  992. exit;
  993. end;
  994. typedconstsym :
  995. begin
  996. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  997. valid_for_assign:=true
  998. else
  999. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1000. exit;
  1001. end;
  1002. else
  1003. begin
  1004. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1005. exit;
  1006. end;
  1007. end;
  1008. end;
  1009. else
  1010. begin
  1011. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1012. exit;
  1013. end;
  1014. end;
  1015. end;
  1016. end;
  1017. function valid_for_var(p:tnode):boolean;
  1018. begin
  1019. valid_for_var:=valid_for_assign(p,[]);
  1020. end;
  1021. function valid_for_formal_var(p : tnode) : boolean;
  1022. begin
  1023. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  1024. end;
  1025. function valid_for_formal_const(p : tnode) : boolean;
  1026. var
  1027. v : boolean;
  1028. begin
  1029. { p must have been firstpass'd before }
  1030. { accept about anything but not a statement ! }
  1031. case p.nodetype of
  1032. calln,
  1033. statementn,
  1034. addrn :
  1035. begin
  1036. { addrn is not allowed as this generate a constant value,
  1037. but a tp procvar are allowed (PFV) }
  1038. if nf_procvarload in p.flags then
  1039. v:=true
  1040. else
  1041. v:=false;
  1042. end;
  1043. else
  1044. v:=true;
  1045. end;
  1046. valid_for_formal_const:=v;
  1047. end;
  1048. function valid_for_assignment(p:tnode):boolean;
  1049. begin
  1050. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  1051. end;
  1052. end.
  1053. {
  1054. $Log$
  1055. Revision 1.55 2002-12-27 18:06:32 peter
  1056. * fix overload error for dynarr:=nil
  1057. Revision 1.54 2002/12/22 16:34:49 peter
  1058. * proc-procvar crash fixed (tw2277)
  1059. Revision 1.53 2002/12/11 22:39:24 peter
  1060. * better error message when no operator is found for equal
  1061. Revision 1.52 2002/11/27 22:11:59 peter
  1062. * rewrote isbinaryoverloadable to use a case. it's now much easier
  1063. to understand what is happening
  1064. Revision 1.51 2002/11/25 17:43:17 peter
  1065. * splitted defbase in defutil,symutil,defcmp
  1066. * merged isconvertable and is_equal into compare_defs(_ext)
  1067. * made operator search faster by walking the list only once
  1068. Revision 1.50 2002/10/07 20:12:08 peter
  1069. * ugly hack to fix tb0411
  1070. Revision 1.49 2002/10/05 00:47:03 peter
  1071. * support dynamicarray<>nil
  1072. Revision 1.48 2002/10/04 21:13:59 peter
  1073. * ignore vecn,subscriptn when checking for a procvar loadn
  1074. Revision 1.47 2002/09/16 18:09:34 peter
  1075. * set_funcret_valid fixed when result was already used in a nested
  1076. procedure
  1077. Revision 1.46 2002/07/20 11:57:53 florian
  1078. * types.pas renamed to defbase.pas because D6 contains a types
  1079. unit so this would conflicts if D6 programms are compiled
  1080. + Willamette/SSE2 instructions to assembler added
  1081. Revision 1.45 2002/05/18 13:34:08 peter
  1082. * readded missing revisions
  1083. Revision 1.44 2002/05/16 19:46:37 carl
  1084. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1085. + try to fix temp allocation (still in ifdef)
  1086. + generic constructor calls
  1087. + start of tassembler / tmodulebase class cleanup
  1088. Revision 1.42 2002/04/02 17:11:28 peter
  1089. * tlocation,treference update
  1090. * LOC_CONSTANT added for better constant handling
  1091. * secondadd splitted in multiple routines
  1092. * location_force_reg added for loading a location to a register
  1093. of a specified size
  1094. * secondassignment parses now first the right and then the left node
  1095. (this is compatible with Kylix). This saves a lot of push/pop especially
  1096. with string operations
  1097. * adapted some routines to use the new cg methods
  1098. Revision 1.41 2002/01/16 09:33:46 jonas
  1099. * no longer allow assignments to pointer expressions (unless there's a
  1100. deref), reported by John Lee
  1101. }