htypechk.pas 40 KB

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