htypechk.pas 41 KB

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