htypechk.pas 42 KB

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