htypechk.pas 42 KB

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