htypechk.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. allow_array_constructor : boolean = false;
  66. { is overloading of this operator allowed for this
  67. binary operator }
  68. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  69. { is overloading of this operator allowed for this
  70. unary operator }
  71. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  72. { check operator args and result type }
  73. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  74. function isbinaryoverloaded(var t : tnode) : boolean;
  75. { Register Allocation }
  76. procedure make_not_regable(p : tnode);
  77. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  78. { subroutine handling }
  79. function is_procsym_load(p:tnode):boolean;
  80. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  81. {
  82. type
  83. tvarstaterequire = (vsr_can_be_undefined,vsr_must_be_valid,
  84. vsr_is_used_after,vsr_must_be_valid_and_is_used_after); }
  85. { sets varsym varstate field correctly }
  86. procedure unset_varstate(p : tnode);
  87. procedure set_varstate(p : tnode;must_be_valid : boolean);
  88. { sets the callunique flag, if the node is a vecn, }
  89. { takes care of type casts etc. }
  90. procedure set_unique(p : tnode);
  91. function valid_for_formal_var(p : tnode) : boolean;
  92. function valid_for_formal_const(p : tnode) : boolean;
  93. function valid_for_var(p:tnode):boolean;
  94. function valid_for_assignment(p:tnode):boolean;
  95. implementation
  96. uses
  97. globtype,systems,
  98. cutils,verbose,globals,
  99. symconst,symsym,symtable,
  100. defutil,defcmp,cpubase,
  101. ncnv,nld,
  102. nmem,ncal,nmat,
  103. cgbase,procinfo
  104. ;
  105. type
  106. TValidAssign=(Valid_Property,Valid_Void);
  107. TValidAssigns=set of TValidAssign;
  108. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  109. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  110. begin
  111. internal_check:=true;
  112. case ld.deftype of
  113. formaldef,
  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. inc(overloaded_operators[optoken].refs);
  402. ht:=ccallnode.create(nil,overloaded_operators[optoken],nil,nil);
  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.parast.symtablelevel>normal_function_level) and
  521. (to_def.deftype=procvardef) then
  522. CGMessage(type_e_cannot_local_proc_to_procvar);
  523. end;
  524. procedure set_varstate(p : tnode;must_be_valid : boolean);
  525. var
  526. hsym : tvarsym;
  527. begin
  528. while assigned(p) do
  529. begin
  530. if (nf_varstateset in p.flags) then
  531. exit;
  532. include(p.flags,nf_varstateset);
  533. case p.nodetype of
  534. typeconvn :
  535. begin
  536. case ttypeconvnode(p).convtype of
  537. tc_cchar_2_pchar,
  538. tc_cstring_2_pchar,
  539. tc_array_2_pointer :
  540. must_be_valid:=false;
  541. tc_pchar_2_string,
  542. tc_pointer_2_array :
  543. must_be_valid:=true;
  544. end;
  545. p:=tunarynode(p).left;
  546. end;
  547. subscriptn :
  548. p:=tunarynode(p).left;
  549. vecn:
  550. begin
  551. set_varstate(tbinarynode(p).right,true);
  552. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  553. must_be_valid:=true;
  554. p:=tunarynode(p).left;
  555. end;
  556. { do not parse calln }
  557. calln :
  558. break;
  559. callparan :
  560. begin
  561. set_varstate(tbinarynode(p).right,must_be_valid);
  562. p:=tunarynode(p).left;
  563. end;
  564. loadn :
  565. begin
  566. if (tloadnode(p).symtableentry.typ=varsym) then
  567. begin
  568. hsym:=tvarsym(tloadnode(p).symtableentry);
  569. if must_be_valid and (nf_first_use in p.flags) then
  570. begin
  571. if (hsym.varstate=vs_declared_and_first_found) or
  572. (hsym.varstate=vs_set_but_first_not_passed) then
  573. begin
  574. if (assigned(hsym.owner) and
  575. assigned(current_procinfo) and
  576. (hsym.owner=current_procinfo.procdef.localst)) then
  577. begin
  578. if (vo_is_funcret in hsym.varoptions) then
  579. CGMessage(sym_w_function_result_not_set)
  580. else
  581. if tloadnode(p).symtable.symtabletype=localsymtable then
  582. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  583. else
  584. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  585. end;
  586. end;
  587. end;
  588. if (nf_first_use in p.flags) then
  589. begin
  590. if hsym.varstate=vs_declared_and_first_found then
  591. begin
  592. { this can only happen at left of an assignment, no ? PM }
  593. if (parsing_para_level=0) and not must_be_valid then
  594. hsym.varstate:=vs_assigned
  595. else
  596. hsym.varstate:=vs_used;
  597. end
  598. else
  599. if hsym.varstate=vs_set_but_first_not_passed then
  600. hsym.varstate:=vs_used;
  601. exclude(p.flags,nf_first_use);
  602. end
  603. else
  604. begin
  605. if (hsym.varstate=vs_assigned) and
  606. (must_be_valid or (parsing_para_level>0) or
  607. (p.resulttype.def.deftype=procvardef)) then
  608. hsym.varstate:=vs_used;
  609. if (hsym.varstate=vs_declared_and_first_found) and
  610. (must_be_valid or (parsing_para_level>0) or
  611. (p.resulttype.def.deftype=procvardef)) then
  612. hsym.varstate:=vs_set_but_first_not_passed;
  613. end;
  614. end;
  615. break;
  616. end;
  617. else
  618. break;
  619. end;{case }
  620. end;
  621. end;
  622. procedure unset_varstate(p : tnode);
  623. begin
  624. while assigned(p) do
  625. begin
  626. exclude(p.flags,nf_varstateset);
  627. case p.nodetype of
  628. typeconvn,
  629. subscriptn,
  630. vecn :
  631. p:=tunarynode(p).left;
  632. else
  633. break;
  634. end;
  635. end;
  636. end;
  637. procedure set_unique(p : tnode);
  638. begin
  639. while assigned(p) do
  640. begin
  641. case p.nodetype of
  642. vecn:
  643. begin
  644. include(p.flags,nf_callunique);
  645. break;
  646. end;
  647. typeconvn,
  648. subscriptn,
  649. derefn:
  650. p:=tunarynode(p).left;
  651. else
  652. break;
  653. end;
  654. end;
  655. end;
  656. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  657. var
  658. hp : tnode;
  659. gotwith,
  660. gotsubscript,
  661. gotpointer,
  662. gotvec,
  663. gotclass,
  664. gotderef : boolean;
  665. fromdef,
  666. todef : tdef;
  667. begin
  668. valid_for_assign:=false;
  669. gotsubscript:=false;
  670. gotvec:=false;
  671. gotderef:=false;
  672. gotclass:=false;
  673. gotpointer:=false;
  674. gotwith:=false;
  675. hp:=p;
  676. if not(valid_void in opts) and
  677. is_void(hp.resulttype.def) then
  678. begin
  679. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  680. exit;
  681. end;
  682. while assigned(hp) do
  683. begin
  684. { property allowed? calln has a property check itself }
  685. if (nf_isproperty in hp.flags) then
  686. begin
  687. if (valid_property in opts) then
  688. valid_for_assign:=true
  689. else
  690. begin
  691. { check return type }
  692. case hp.resulttype.def.deftype of
  693. pointerdef :
  694. gotpointer:=true;
  695. objectdef :
  696. gotclass:=is_class_or_interface(hp.resulttype.def);
  697. recorddef, { handle record like class it needs a subscription }
  698. classrefdef :
  699. gotclass:=true;
  700. end;
  701. { 1. if it returns a pointer and we've found a deref,
  702. 2. if it returns a class or record and a subscription or with is found }
  703. if (gotpointer and gotderef) or
  704. (gotclass and (gotsubscript or gotwith)) then
  705. valid_for_assign:=true
  706. else
  707. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  708. end;
  709. exit;
  710. end;
  711. case hp.nodetype of
  712. temprefn :
  713. begin
  714. valid_for_assign := true;
  715. exit;
  716. end;
  717. derefn :
  718. begin
  719. gotderef:=true;
  720. hp:=tderefnode(hp).left;
  721. end;
  722. typeconvn :
  723. begin
  724. { typecast sizes must match, exceptions:
  725. - from formaldef
  726. - from void
  727. - from open array
  728. - typecast from pointer to array }
  729. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  730. todef:=hp.resulttype.def;
  731. if not((fromdef.deftype=formaldef) or
  732. is_void(fromdef) or
  733. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  734. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  735. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  736. is_open_array(fromdef) and
  737. (fromdef.size<>todef.size) then
  738. begin
  739. { in TP it is allowed to typecast to smaller types }
  740. if not(m_tp7 in aktmodeswitches) or
  741. (todef.size>fromdef.size) then
  742. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  743. end;
  744. case hp.resulttype.def.deftype of
  745. pointerdef :
  746. gotpointer:=true;
  747. objectdef :
  748. gotclass:=is_class_or_interface(hp.resulttype.def);
  749. classrefdef :
  750. gotclass:=true;
  751. arraydef :
  752. begin
  753. { pointer -> array conversion is done then we need to see it
  754. as a deref, because a ^ is then not required anymore }
  755. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  756. gotderef:=true;
  757. end;
  758. end;
  759. hp:=ttypeconvnode(hp).left;
  760. end;
  761. vecn :
  762. begin
  763. gotvec:=true;
  764. hp:=tunarynode(hp).left;
  765. end;
  766. asn :
  767. hp:=tunarynode(hp).left;
  768. subscriptn :
  769. begin
  770. gotsubscript:=true;
  771. { a class/interface access is an implicit }
  772. { dereferencing }
  773. hp:=tsubscriptnode(hp).left;
  774. if is_class_or_interface(hp.resulttype.def) then
  775. gotderef:=true;
  776. end;
  777. subn,
  778. addn :
  779. begin
  780. { Allow add/sub operators on a pointer, or an integer
  781. and a pointer typecast and deref has been found }
  782. if ((hp.resulttype.def.deftype=pointerdef) or
  783. (is_integer(hp.resulttype.def) and gotpointer)) and
  784. gotderef then
  785. valid_for_assign:=true
  786. else
  787. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  788. exit;
  789. end;
  790. addrn :
  791. begin
  792. if gotderef or
  793. (nf_procvarload in hp.flags) then
  794. valid_for_assign:=true
  795. else
  796. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  797. exit;
  798. end;
  799. calln :
  800. begin
  801. { check return type }
  802. case hp.resulttype.def.deftype of
  803. arraydef :
  804. begin
  805. { dynamic arrays are allowed when there is also a
  806. vec node }
  807. if is_dynamic_array(hp.resulttype.def) and
  808. gotvec then
  809. begin
  810. gotderef:=true;
  811. gotpointer:=true;
  812. end;
  813. end;
  814. pointerdef :
  815. gotpointer:=true;
  816. objectdef :
  817. gotclass:=is_class_or_interface(hp.resulttype.def);
  818. recorddef, { handle record like class it needs a subscription }
  819. classrefdef :
  820. gotclass:=true;
  821. end;
  822. { 1. if it returns a pointer and we've found a deref,
  823. 2. if it returns a class or record and a subscription or with is found }
  824. if (gotpointer and gotderef) or
  825. (gotclass and (gotsubscript or gotwith)) then
  826. valid_for_assign:=true
  827. else
  828. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  829. exit;
  830. end;
  831. loadn :
  832. begin
  833. case tloadnode(hp).symtableentry.typ of
  834. absolutesym,
  835. varsym :
  836. begin
  837. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  838. begin
  839. { allow p^:= constructions with p is const parameter }
  840. if gotderef then
  841. valid_for_assign:=true
  842. else
  843. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  844. exit;
  845. end;
  846. { Are we at a with symtable, then we need to process the
  847. withrefnode also to check for maybe a const load }
  848. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  849. begin
  850. { continue with processing the withref node }
  851. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  852. gotwith:=true;
  853. end
  854. else
  855. begin
  856. { set the assigned flag for varsyms }
  857. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  858. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  859. valid_for_assign:=true;
  860. exit;
  861. end;
  862. end;
  863. typedconstsym :
  864. begin
  865. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  866. valid_for_assign:=true
  867. else
  868. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  869. exit;
  870. end;
  871. else
  872. begin
  873. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  874. exit;
  875. end;
  876. end;
  877. end;
  878. else
  879. begin
  880. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  881. exit;
  882. end;
  883. end;
  884. end;
  885. end;
  886. function valid_for_var(p:tnode):boolean;
  887. begin
  888. valid_for_var:=valid_for_assign(p,[]);
  889. end;
  890. function valid_for_formal_var(p : tnode) : boolean;
  891. begin
  892. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  893. end;
  894. function valid_for_formal_const(p : tnode) : boolean;
  895. var
  896. v : boolean;
  897. begin
  898. { p must have been firstpass'd before }
  899. { accept about anything but not a statement ! }
  900. case p.nodetype of
  901. calln,
  902. statementn,
  903. addrn :
  904. begin
  905. { addrn is not allowed as this generate a constant value,
  906. but a tp procvar are allowed (PFV) }
  907. if nf_procvarload in p.flags then
  908. v:=true
  909. else
  910. v:=false;
  911. end;
  912. else
  913. v:=true;
  914. end;
  915. valid_for_formal_const:=v;
  916. end;
  917. function valid_for_assignment(p:tnode):boolean;
  918. begin
  919. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  920. end;
  921. end.
  922. {
  923. $Log$
  924. Revision 1.68 2003-10-05 21:21:52 peter
  925. * c style array of const generates callparanodes
  926. * varargs paraloc fixes
  927. Revision 1.67 2003/10/01 20:34:48 peter
  928. * procinfo unit contains tprocinfo
  929. * cginfo renamed to cgbase
  930. * moved cgmessage to verbose
  931. * fixed ppc and sparc compiles
  932. Revision 1.66 2003/08/23 18:52:18 peter
  933. * don't check size for open array in valid_for_assign
  934. Revision 1.65 2003/07/08 15:20:56 peter
  935. * don't allow add/assignments for formaldef
  936. * formaldef size changed to 0
  937. Revision 1.64 2003/06/13 21:19:30 peter
  938. * current_procdef removed, use current_procinfo.procdef instead
  939. Revision 1.63 2003/05/09 17:47:02 peter
  940. * self moved to hidden parameter
  941. * removed hdisposen,hnewn,selfn
  942. Revision 1.62 2003/04/27 11:21:32 peter
  943. * aktprocdef renamed to current_procinfo.procdef
  944. * procinfo renamed to current_procinfo
  945. * procinfo will now be stored in current_module so it can be
  946. cleaned up properly
  947. * gen_main_procsym changed to create_main_proc and release_main_proc
  948. to also generate a tprocinfo structure
  949. * fixed unit implicit initfinal
  950. Revision 1.61 2003/04/27 07:29:50 peter
  951. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  952. a new procdef declaration
  953. * aktprocsym removed
  954. * lexlevel removed, use symtable.symtablelevel instead
  955. * implicit init/final code uses the normal genentry/genexit
  956. * funcret state checking updated for new funcret handling
  957. Revision 1.60 2003/04/25 20:59:33 peter
  958. * removed funcretn,funcretsym, function result is now in varsym
  959. and aliases for result and function name are added using absolutesym
  960. * vs_hidden parameter for funcret passed in parameter
  961. * vs_hidden fixes
  962. * writenode changed to printnode and released from extdebug
  963. * -vp option added to generate a tree.log with the nodetree
  964. * nicer printnode for statements, callnode
  965. Revision 1.59 2003/04/22 23:50:22 peter
  966. * firstpass uses expectloc
  967. * checks if there are differences between the expectloc and
  968. location.loc from secondpass in EXTDEBUG
  969. Revision 1.58 2003/01/03 17:17:26 peter
  970. * use compare_def_ext to test if assignn operator is allowed
  971. Revision 1.57 2003/01/02 22:21:19 peter
  972. * fixed previous operator change
  973. Revision 1.56 2003/01/02 19:50:21 peter
  974. * fixed operator checking for objects
  975. * made binary operator checking simpeler
  976. Revision 1.55 2002/12/27 18:06:32 peter
  977. * fix overload error for dynarr:=nil
  978. Revision 1.54 2002/12/22 16:34:49 peter
  979. * proc-procvar crash fixed (tw2277)
  980. Revision 1.53 2002/12/11 22:39:24 peter
  981. * better error message when no operator is found for equal
  982. Revision 1.52 2002/11/27 22:11:59 peter
  983. * rewrote isbinaryoverloadable to use a case. it's now much easier
  984. to understand what is happening
  985. Revision 1.51 2002/11/25 17:43:17 peter
  986. * splitted defbase in defutil,symutil,defcmp
  987. * merged isconvertable and is_equal into compare_defs(_ext)
  988. * made operator search faster by walking the list only once
  989. Revision 1.50 2002/10/07 20:12:08 peter
  990. * ugly hack to fix tb0411
  991. Revision 1.49 2002/10/05 00:47:03 peter
  992. * support dynamicarray<>nil
  993. Revision 1.48 2002/10/04 21:13:59 peter
  994. * ignore vecn,subscriptn when checking for a procvar loadn
  995. Revision 1.47 2002/09/16 18:09:34 peter
  996. * set_funcret_valid fixed when result was already used in a nested
  997. procedure
  998. Revision 1.46 2002/07/20 11:57:53 florian
  999. * types.pas renamed to defbase.pas because D6 contains a types
  1000. unit so this would conflicts if D6 programms are compiled
  1001. + Willamette/SSE2 instructions to assembler added
  1002. Revision 1.45 2002/05/18 13:34:08 peter
  1003. * readded missing revisions
  1004. Revision 1.44 2002/05/16 19:46:37 carl
  1005. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1006. + try to fix temp allocation (still in ifdef)
  1007. + generic constructor calls
  1008. + start of tassembler / tmodulebase class cleanup
  1009. Revision 1.42 2002/04/02 17:11:28 peter
  1010. * tlocation,treference update
  1011. * LOC_CONSTANT added for better constant handling
  1012. * secondadd splitted in multiple routines
  1013. * location_force_reg added for loading a location to a register
  1014. of a specified size
  1015. * secondassignment parses now first the right and then the left node
  1016. (this is compatible with Kylix). This saves a lot of push/pop especially
  1017. with string operations
  1018. * adapted some routines to use the new cg methods
  1019. Revision 1.41 2002/01/16 09:33:46 jonas
  1020. * no longer allow assignments to pointer expressions (unless there's a
  1021. deref), reported by John Lee
  1022. }