htypechk.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit exports some help routines for the type checking
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit htypechk;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,
  23. node,
  24. symtype,symdef;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. op_overloading_supported : boolean;
  30. end;
  31. const
  32. tok2nodes=25;
  33. tok2node:array[1..tok2nodes] of ttok2noderec=(
  34. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  35. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  36. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  37. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  38. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  39. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  40. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  41. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  42. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  43. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  44. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  45. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  46. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  47. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  48. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  49. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  50. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  51. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  52. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  53. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  54. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  55. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  56. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  57. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  58. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  59. );
  60. const
  61. { firstcallparan without varspez we don't count the ref }
  62. {$ifdef extdebug}
  63. count_ref : boolean = true;
  64. {$endif def extdebug}
  65. get_para_resulttype : boolean = false;
  66. allow_array_constructor : boolean = false;
  67. { is overloading of this operator allowed for this
  68. binary operator }
  69. function isbinaryoperatoroverloadable(ld, rd,dd : tdef;
  70. treetyp : tnodetype) : boolean;
  71. { is overloading of this operator allowed for this
  72. unary operator }
  73. function isunaryoperatoroverloadable(rd,dd : tdef;
  74. treetyp : tnodetype) : boolean;
  75. { check operator args and result type }
  76. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  77. function isbinaryoverloaded(var t : tnode) : boolean;
  78. { Register Allocation }
  79. procedure make_not_regable(p : tnode);
  80. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  81. { subroutine handling }
  82. function is_procsym_load(p:tnode):boolean;
  83. function is_procsym_call(p:tnode):boolean;
  84. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  85. {
  86. type
  87. tvarstaterequire = (vsr_can_be_undefined,vsr_must_be_valid,
  88. vsr_is_used_after,vsr_must_be_valid_and_is_used_after); }
  89. { sets varsym varstate field correctly }
  90. procedure unset_varstate(p : tnode);
  91. procedure set_varstate(p : tnode;must_be_valid : boolean);
  92. { sets the callunique flag, if the node is a vecn, }
  93. { takes care of type casts etc. }
  94. procedure set_unique(p : tnode);
  95. { sets funcret_is_valid to true, if p contains a funcref node }
  96. procedure set_funcret_is_valid(p : tnode);
  97. function valid_for_formal_var(p : tnode) : boolean;
  98. function valid_for_formal_const(p : tnode) : boolean;
  99. function valid_for_var(p:tnode):boolean;
  100. function valid_for_assignment(p:tnode):boolean;
  101. implementation
  102. uses
  103. globtype,systems,
  104. cutils,verbose,globals,
  105. symconst,symsym,symtable,
  106. defbase,cpubase,
  107. ncnv,nld,
  108. nmem,ncal,nmat,
  109. cgbase
  110. ;
  111. type
  112. TValidAssign=(Valid_Property,Valid_Void);
  113. TValidAssigns=set of TValidAssign;
  114. { ld is the left type definition
  115. rd the right type definition
  116. dd the result type definition or voiddef if unkown }
  117. function isbinaryoperatoroverloadable(ld, rd, dd : tdef;
  118. treetyp : tnodetype) : boolean;
  119. begin
  120. isbinaryoperatoroverloadable:=
  121. (treetyp=starstarn) or
  122. (ld.deftype=recorddef) or
  123. (rd.deftype=recorddef) or
  124. (ld.deftype=variantdef) or
  125. (rd.deftype=variantdef) or
  126. ((rd.deftype=pointerdef) and
  127. not(is_dynamic_array(ld) and
  128. is_voidpointer(rd)) and
  129. not(is_pchar(rd) and
  130. (is_chararray(ld) or
  131. (ld.deftype=stringdef) or
  132. (treetyp=addn))) and
  133. (not(ld.deftype in [pointerdef,objectdef,classrefdef,procvardef]) or
  134. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,subn])
  135. ) and
  136. (not is_integer(ld) or not (treetyp in [addn,subn]))
  137. ) or
  138. ((ld.deftype=pointerdef) and
  139. not(is_dynamic_array(rd) and
  140. is_voidpointer(ld)) and
  141. not(is_pchar(ld) and
  142. (is_chararray(rd) or
  143. (rd.deftype=stringdef) or
  144. (treetyp=addn))) and
  145. (not(rd.deftype in [stringdef,pointerdef,objectdef,classrefdef,procvardef]) and
  146. ((not is_integer(rd) and (rd.deftype<>objectdef)
  147. and (rd.deftype<>classrefdef)) or
  148. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn])
  149. )
  150. )
  151. ) or
  152. { array def, but not mmx or chararray+[char,string,chararray] }
  153. ((ld.deftype=arraydef) and
  154. not((cs_mmx in aktlocalswitches) and
  155. is_mmx_able_array(ld)) and
  156. not(is_dynamic_array(ld) and
  157. is_voidpointer(rd)) and
  158. not(is_chararray(ld) and
  159. (is_char(rd) or
  160. is_pchar(rd) or
  161. { char array + int = pchar + int, fix for web bug 1377 (JM) }
  162. is_integer(rd) or
  163. (rd.deftype=stringdef) or
  164. is_chararray(rd)))
  165. ) or
  166. ((rd.deftype=arraydef) and
  167. not((cs_mmx in aktlocalswitches) and
  168. is_mmx_able_array(rd)) and
  169. not(is_dynamic_array(rd) and
  170. is_voidpointer(ld)) and
  171. not(is_chararray(rd) and
  172. (is_char(ld) or
  173. is_pchar(ld) or
  174. (ld.deftype=stringdef) or
  175. is_chararray(ld)))
  176. ) or
  177. { <> and = are defined for classes }
  178. (
  179. (ld.deftype=objectdef) and
  180. not((treetyp in [equaln,unequaln]) and is_class_or_interface(ld))
  181. ) or
  182. (
  183. (rd.deftype=objectdef) and
  184. not((treetyp in [equaln,unequaln]) and is_class_or_interface(rd))
  185. )
  186. or
  187. { allow other operators that + on strings }
  188. (
  189. (is_char(rd) or
  190. is_pchar(rd) or
  191. (rd.deftype=stringdef) or
  192. is_chararray(rd) or
  193. is_char(ld) or
  194. is_pchar(ld) or
  195. (ld.deftype=stringdef) or
  196. is_chararray(ld)
  197. ) and
  198. not(treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  199. not(is_pchar(ld) and
  200. (is_integer(rd) or (rd.deftype=pointerdef)) and
  201. (treetyp=subn)
  202. )
  203. );
  204. end;
  205. function isunaryoperatoroverloadable(rd,dd : tdef;
  206. treetyp : tnodetype) : boolean;
  207. begin
  208. isunaryoperatoroverloadable:=false;
  209. { what assignment overloading should be allowed ?? }
  210. if (treetyp=assignn) then
  211. begin
  212. isunaryoperatoroverloadable:=true;
  213. { this already get tbs0261 to fail
  214. isunaryoperatoroverloadable:=not is_equal(rd,dd); PM }
  215. end
  216. { should we force that rd and dd are equal ?? }
  217. else if (treetyp=subn { unaryminusn }) then
  218. begin
  219. isunaryoperatoroverloadable:=
  220. not is_integer(rd) and not (rd.deftype=floatdef)
  221. {$ifdef SUPPORT_MMX}
  222. and not ((cs_mmx in aktlocalswitches) and
  223. is_mmx_able_array(rd))
  224. {$endif SUPPORT_MMX}
  225. ;
  226. end
  227. else if (treetyp=notn) then
  228. begin
  229. isunaryoperatoroverloadable:=not is_integer(rd) and not is_boolean(rd)
  230. {$ifdef SUPPORT_MMX}
  231. and not ((cs_mmx in aktlocalswitches) and
  232. is_mmx_able_array(rd))
  233. {$endif SUPPORT_MMX}
  234. ;
  235. end;
  236. end;
  237. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  238. var
  239. ld,rd,dd : tdef;
  240. i : longint;
  241. begin
  242. case pf.parast.symindex.count of
  243. 2 : begin
  244. isoperatoracceptable:=false;
  245. for i:=1 to tok2nodes do
  246. if tok2node[i].tok=optoken then
  247. begin
  248. ld:=tvarsym(pf.parast.symindex.first).vartype.def;
  249. rd:=tvarsym(pf.parast.symindex.first.indexnext).vartype.def;
  250. dd:=pf.rettype.def;
  251. isoperatoracceptable:=
  252. tok2node[i].op_overloading_supported and
  253. isbinaryoperatoroverloadable(ld,rd,dd,tok2node[i].nod);
  254. break;
  255. end;
  256. end;
  257. 1 : begin
  258. rd:=tvarsym(pf.parast.symindex.first).vartype.def;
  259. dd:=pf.rettype.def;
  260. for i:=1 to tok2nodes do
  261. if tok2node[i].tok=optoken then
  262. begin
  263. isoperatoracceptable:=
  264. tok2node[i].op_overloading_supported and
  265. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  266. break;
  267. end;
  268. end;
  269. else
  270. isoperatoracceptable:=false;
  271. end;
  272. end;
  273. function isbinaryoverloaded(var t : tnode) : boolean;
  274. var
  275. rd,ld : tdef;
  276. optoken : ttoken;
  277. ht : tnode;
  278. begin
  279. isbinaryoverloaded:=false;
  280. { overloaded operator ? }
  281. { load easier access variables }
  282. rd:=tbinarynode(t).right.resulttype.def;
  283. ld:=tbinarynode(t).left.resulttype.def;
  284. if isbinaryoperatoroverloadable(ld,rd,voidtype.def,t.nodetype) then
  285. begin
  286. isbinaryoverloaded:=true;
  287. {!!!!!!!!! handle paras }
  288. case t.nodetype of
  289. addn:
  290. optoken:=_PLUS;
  291. subn:
  292. optoken:=_MINUS;
  293. muln:
  294. optoken:=_STAR;
  295. starstarn:
  296. optoken:=_STARSTAR;
  297. slashn:
  298. optoken:=_SLASH;
  299. ltn:
  300. optoken:=tokens._lt;
  301. gtn:
  302. optoken:=tokens._gt;
  303. lten:
  304. optoken:=_lte;
  305. gten:
  306. optoken:=_gte;
  307. equaln,unequaln :
  308. optoken:=_EQUAL;
  309. symdifn :
  310. optoken:=_SYMDIF;
  311. modn :
  312. optoken:=_OP_MOD;
  313. orn :
  314. optoken:=_OP_OR;
  315. xorn :
  316. optoken:=_OP_XOR;
  317. andn :
  318. optoken:=_OP_AND;
  319. divn :
  320. optoken:=_OP_DIV;
  321. shln :
  322. optoken:=_OP_SHL;
  323. shrn :
  324. optoken:=_OP_SHR;
  325. else
  326. exit;
  327. end;
  328. { the nil as symtable signs firstcalln that this is
  329. an overloaded operator }
  330. ht:=ccallnode.create(nil,overloaded_operators[optoken],nil,nil);
  331. { we have to convert p^.left and p^.right into
  332. callparanodes }
  333. if tcallnode(ht).symtableprocentry=nil then
  334. begin
  335. CGMessage(parser_e_operator_not_overloaded);
  336. ht.free;
  337. isbinaryoverloaded:=false;
  338. exit;
  339. end;
  340. inc(tcallnode(ht).symtableprocentry.refs);
  341. { we need copies, because the originals will be destroyed when we give a }
  342. { changed node back to firstpass! (JM) }
  343. if assigned(tbinarynode(t).left) then
  344. if assigned(tbinarynode(t).right) then
  345. tcallnode(ht).left :=
  346. ccallparanode.create(tbinarynode(t).right.getcopy,
  347. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  348. else
  349. tcallnode(ht).left :=
  350. ccallparanode.create(nil,
  351. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  352. else if assigned(tbinarynode(t).right) then
  353. tcallnode(ht).left :=
  354. ccallparanode.create(tbinarynode(t).right.getcopy,
  355. ccallparanode.create(nil,nil));
  356. if t.nodetype=unequaln then
  357. ht:=cnotnode.create(ht);
  358. t:=ht;
  359. end;
  360. end;
  361. {****************************************************************************
  362. Register Calculation
  363. ****************************************************************************}
  364. { marks an lvalue as "unregable" }
  365. procedure make_not_regable(p : tnode);
  366. begin
  367. case p.nodetype of
  368. typeconvn :
  369. make_not_regable(ttypeconvnode(p).left);
  370. loadn :
  371. if tloadnode(p).symtableentry.typ=varsym then
  372. tvarsym(tloadnode(p).symtableentry).varoptions:=tvarsym(tloadnode(p).symtableentry).varoptions-[vo_regable,vo_fpuregable];
  373. end;
  374. end;
  375. { calculates the needed registers for a binary operator }
  376. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  377. begin
  378. p.left_right_max;
  379. { Only when the difference between the left and right registers < the
  380. wanted registers allocate the amount of registers }
  381. if assigned(p.left) then
  382. begin
  383. if assigned(p.right) then
  384. begin
  385. { the location must be already filled in because we need it to }
  386. { calculate the necessary number of registers (JM) }
  387. if p.location.loc = LOC_INVALID then
  388. internalerror(200110101);
  389. if (abs(p.left.registers32-p.right.registers32)<r32) or
  390. ((p.location.loc = LOC_FPUREGISTER) and
  391. (p.right.registersfpu <= p.left.registersfpu) and
  392. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  393. (p.left.registers32 < p.right.registers32)) then
  394. inc(p.registers32,r32);
  395. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  396. inc(p.registersfpu,fpu);
  397. {$ifdef SUPPORT_MMX}
  398. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  399. inc(p.registersmmx,mmx);
  400. {$endif SUPPORT_MMX}
  401. { the following is a little bit guessing but I think }
  402. { it's the only way to solve same internalerrors: }
  403. { if the left and right node both uses registers }
  404. { and return a mem location, but the current node }
  405. { doesn't use an integer register we get probably }
  406. { trouble when restoring a node }
  407. if (p.left.registers32=p.right.registers32) and
  408. (p.registers32=p.left.registers32) and
  409. (p.registers32>0) and
  410. (p.left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  411. (p.right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  412. inc(p.registers32);
  413. end
  414. else
  415. begin
  416. if (p.left.registers32<r32) then
  417. inc(p.registers32,r32);
  418. if (p.left.registersfpu<fpu) then
  419. inc(p.registersfpu,fpu);
  420. {$ifdef SUPPORT_MMX}
  421. if (p.left.registersmmx<mmx) then
  422. inc(p.registersmmx,mmx);
  423. {$endif SUPPORT_MMX}
  424. end;
  425. end;
  426. { error CGMessage, if more than 8 floating point }
  427. { registers are needed }
  428. { if p.registersfpu>maxfpuregs then
  429. CGMessage(cg_e_too_complex_expr); now pushed if needed PM }
  430. end;
  431. {****************************************************************************
  432. Subroutine Handling
  433. ****************************************************************************}
  434. function is_procsym_load(p:tnode):boolean;
  435. begin
  436. { ignore vecn,subscriptn }
  437. repeat
  438. case p.nodetype of
  439. vecn :
  440. p:=tvecnode(p).left;
  441. subscriptn :
  442. p:=tsubscriptnode(p).left;
  443. else
  444. break;
  445. end;
  446. until false;
  447. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry.typ=procsym)) or
  448. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  449. and (tloadnode(taddrnode(p).left).symtableentry.typ=procsym)) ;
  450. end;
  451. { change a proc call to a procload for assignment to a procvar }
  452. { this can only happen for proc/function without arguments }
  453. function is_procsym_call(p:tnode):boolean;
  454. begin
  455. is_procsym_call:=(p.nodetype=calln) and (tcallnode(p).left=nil) and
  456. (((tcallnode(p).symtableprocentry.typ=procsym) and (tcallnode(p).right=nil)) or
  457. (assigned(tcallnode(p).right) and (tcallnode(tcallnode(p).right).symtableprocentry.typ=varsym)));
  458. end;
  459. { local routines can't be assigned to procvars }
  460. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  461. begin
  462. if (from_def.symtablelevel>1) and (to_def.deftype=procvardef) then
  463. CGMessage(type_e_cannot_local_proc_to_procvar);
  464. end;
  465. procedure set_varstate(p : tnode;must_be_valid : boolean);
  466. var
  467. hsym : tvarsym;
  468. begin
  469. while assigned(p) do
  470. begin
  471. if (nf_varstateset in p.flags) then
  472. exit;
  473. include(p.flags,nf_varstateset);
  474. case p.nodetype of
  475. typeconvn :
  476. begin
  477. case ttypeconvnode(p).convtype of
  478. tc_cchar_2_pchar,
  479. tc_cstring_2_pchar,
  480. tc_array_2_pointer :
  481. must_be_valid:=false;
  482. tc_pchar_2_string,
  483. tc_pointer_2_array :
  484. must_be_valid:=true;
  485. end;
  486. p:=tunarynode(p).left;
  487. end;
  488. subscriptn :
  489. p:=tunarynode(p).left;
  490. vecn:
  491. begin
  492. set_varstate(tbinarynode(p).right,true);
  493. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  494. must_be_valid:=true;
  495. p:=tunarynode(p).left;
  496. end;
  497. { do not parse calln }
  498. calln :
  499. break;
  500. callparan :
  501. begin
  502. set_varstate(tbinarynode(p).right,must_be_valid);
  503. p:=tunarynode(p).left;
  504. end;
  505. loadn :
  506. begin
  507. if (tloadnode(p).symtableentry.typ=varsym) then
  508. begin
  509. hsym:=tvarsym(tloadnode(p).symtableentry);
  510. if must_be_valid and (nf_first in p.flags) then
  511. begin
  512. if (hsym.varstate=vs_declared_and_first_found) or
  513. (hsym.varstate=vs_set_but_first_not_passed) then
  514. begin
  515. if (assigned(hsym.owner) and
  516. assigned(aktprocsym) and
  517. (hsym.owner = aktprocdef.localst)) then
  518. begin
  519. if tloadnode(p).symtable.symtabletype=localsymtable then
  520. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  521. else
  522. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  523. end;
  524. end;
  525. end;
  526. if (nf_first in p.flags) then
  527. begin
  528. if hsym.varstate=vs_declared_and_first_found then
  529. begin
  530. { this can only happen at left of an assignment, no ? PM }
  531. if (parsing_para_level=0) and not must_be_valid then
  532. hsym.varstate:=vs_assigned
  533. else
  534. hsym.varstate:=vs_used;
  535. end
  536. else
  537. if hsym.varstate=vs_set_but_first_not_passed then
  538. hsym.varstate:=vs_used;
  539. exclude(p.flags,nf_first);
  540. end
  541. else
  542. begin
  543. if (hsym.varstate=vs_assigned) and
  544. (must_be_valid or (parsing_para_level>0) or
  545. (p.resulttype.def.deftype=procvardef)) then
  546. hsym.varstate:=vs_used;
  547. if (hsym.varstate=vs_declared_and_first_found) and
  548. (must_be_valid or (parsing_para_level>0) or
  549. (p.resulttype.def.deftype=procvardef)) then
  550. hsym.varstate:=vs_set_but_first_not_passed;
  551. end;
  552. end;
  553. break;
  554. end;
  555. funcretn:
  556. begin
  557. { no claim if setting higher return value_str }
  558. if must_be_valid and
  559. (lexlevel=tfuncretnode(p).funcretsym.owner.symtablelevel) and
  560. ((tfuncretnode(p).funcretsym.funcretstate=vs_declared) or
  561. ((nf_is_first_funcret in p.flags) and
  562. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found))) then
  563. begin
  564. CGMessage(sym_w_function_result_not_set);
  565. { avoid multiple warnings }
  566. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  567. end;
  568. if (nf_is_first_funcret in p.flags) and not must_be_valid then
  569. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  570. break;
  571. end;
  572. else
  573. break;
  574. end;{case }
  575. end;
  576. end;
  577. procedure unset_varstate(p : tnode);
  578. begin
  579. while assigned(p) do
  580. begin
  581. exclude(p.flags,nf_varstateset);
  582. case p.nodetype of
  583. typeconvn,
  584. subscriptn,
  585. vecn :
  586. p:=tunarynode(p).left;
  587. else
  588. break;
  589. end;
  590. end;
  591. end;
  592. procedure set_unique(p : tnode);
  593. begin
  594. while assigned(p) do
  595. begin
  596. case p.nodetype of
  597. vecn:
  598. begin
  599. include(p.flags,nf_callunique);
  600. break;
  601. end;
  602. typeconvn,
  603. subscriptn,
  604. derefn:
  605. p:=tunarynode(p).left;
  606. else
  607. break;
  608. end;
  609. end;
  610. end;
  611. procedure set_funcret_is_valid(p:tnode);
  612. begin
  613. while assigned(p) do
  614. begin
  615. case p.nodetype of
  616. funcretn:
  617. begin
  618. if (nf_is_first_funcret in p.flags) or
  619. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found) then
  620. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  621. break;
  622. end;
  623. vecn,
  624. {derefn,}
  625. typeconvn,
  626. subscriptn:
  627. p:=tunarynode(p).left;
  628. else
  629. break;
  630. end;
  631. end;
  632. end;
  633. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  634. var
  635. hp : tnode;
  636. gotwith,
  637. gotsubscript,
  638. gotpointer,
  639. gotclass,
  640. gotderef : boolean;
  641. fromdef,
  642. todef : tdef;
  643. begin
  644. valid_for_assign:=false;
  645. gotsubscript:=false;
  646. gotderef:=false;
  647. gotclass:=false;
  648. gotpointer:=false;
  649. gotwith:=false;
  650. hp:=p;
  651. if not(valid_void in opts) and
  652. is_void(hp.resulttype.def) then
  653. begin
  654. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  655. exit;
  656. end;
  657. while assigned(hp) do
  658. begin
  659. { property allowed? calln has a property check itself }
  660. if (nf_isproperty in hp.flags) then
  661. begin
  662. if (valid_property in opts) then
  663. valid_for_assign:=true
  664. else
  665. begin
  666. { check return type }
  667. case hp.resulttype.def.deftype of
  668. pointerdef :
  669. gotpointer:=true;
  670. objectdef :
  671. gotclass:=is_class_or_interface(hp.resulttype.def);
  672. recorddef, { handle record like class it needs a subscription }
  673. classrefdef :
  674. gotclass:=true;
  675. end;
  676. { 1. if it returns a pointer and we've found a deref,
  677. 2. if it returns a class or record and a subscription or with is found }
  678. if (gotpointer and gotderef) or
  679. (gotclass and (gotsubscript or gotwith)) then
  680. valid_for_assign:=true
  681. else
  682. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  683. end;
  684. exit;
  685. end;
  686. case hp.nodetype of
  687. temprefn :
  688. begin
  689. valid_for_assign := true;
  690. exit;
  691. end;
  692. derefn :
  693. begin
  694. gotderef:=true;
  695. hp:=tderefnode(hp).left;
  696. end;
  697. typeconvn :
  698. begin
  699. { typecast sizes must match, exceptions:
  700. - from formaldef
  701. - from void
  702. - typecast from pointer to array }
  703. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  704. todef:=hp.resulttype.def;
  705. if not((fromdef.deftype=formaldef) or
  706. is_void(fromdef) or
  707. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  708. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  709. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  710. (fromdef.size<>todef.size) then
  711. begin
  712. { in TP it is allowed to typecast to smaller types }
  713. if not(m_tp7 in aktmodeswitches) or
  714. (todef.size>fromdef.size) then
  715. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  716. end;
  717. case hp.resulttype.def.deftype of
  718. pointerdef :
  719. gotpointer:=true;
  720. objectdef :
  721. gotclass:=is_class_or_interface(hp.resulttype.def);
  722. classrefdef :
  723. gotclass:=true;
  724. arraydef :
  725. begin
  726. { pointer -> array conversion is done then we need to see it
  727. as a deref, because a ^ is then not required anymore }
  728. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  729. gotderef:=true;
  730. end;
  731. end;
  732. hp:=ttypeconvnode(hp).left;
  733. end;
  734. vecn,
  735. asn :
  736. hp:=tunarynode(hp).left;
  737. subscriptn :
  738. begin
  739. gotsubscript:=true;
  740. { a class/interface access is an implicit }
  741. { dereferencing }
  742. hp:=tsubscriptnode(hp).left;
  743. if is_class_or_interface(hp.resulttype.def) then
  744. gotderef:=true;
  745. end;
  746. subn,
  747. addn :
  748. begin
  749. { Allow add/sub operators on a pointer, or an integer
  750. and a pointer typecast and deref has been found }
  751. if ((hp.resulttype.def.deftype=pointerdef) or
  752. (is_integer(hp.resulttype.def) and gotpointer)) and
  753. gotderef then
  754. valid_for_assign:=true
  755. else
  756. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  757. exit;
  758. end;
  759. addrn :
  760. begin
  761. if gotderef or
  762. (nf_procvarload in hp.flags) then
  763. valid_for_assign:=true
  764. else
  765. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  766. exit;
  767. end;
  768. selfn,
  769. funcretn :
  770. begin
  771. valid_for_assign:=true;
  772. exit;
  773. end;
  774. calln :
  775. begin
  776. { check return type }
  777. case hp.resulttype.def.deftype of
  778. pointerdef :
  779. gotpointer:=true;
  780. objectdef :
  781. gotclass:=is_class_or_interface(hp.resulttype.def);
  782. recorddef, { handle record like class it needs a subscription }
  783. classrefdef :
  784. gotclass:=true;
  785. end;
  786. { 1. if it returns a pointer and we've found a deref,
  787. 2. if it returns a class or record and a subscription or with is found }
  788. if (gotpointer and gotderef) or
  789. (gotclass and (gotsubscript or gotwith)) then
  790. valid_for_assign:=true
  791. else
  792. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  793. exit;
  794. end;
  795. loadn :
  796. begin
  797. case tloadnode(hp).symtableentry.typ of
  798. absolutesym,
  799. varsym :
  800. begin
  801. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  802. begin
  803. { allow p^:= constructions with p is const parameter }
  804. if gotderef then
  805. valid_for_assign:=true
  806. else
  807. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  808. exit;
  809. end;
  810. { Are we at a with symtable, then we need to process the
  811. withrefnode also to check for maybe a const load }
  812. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  813. begin
  814. { continue with processing the withref node }
  815. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  816. gotwith:=true;
  817. end
  818. else
  819. begin
  820. { set the assigned flag for varsyms }
  821. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  822. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  823. valid_for_assign:=true;
  824. exit;
  825. end;
  826. end;
  827. funcretsym :
  828. begin
  829. valid_for_assign:=true;
  830. exit;
  831. end;
  832. typedconstsym :
  833. begin
  834. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  835. valid_for_assign:=true
  836. else
  837. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  838. exit;
  839. end;
  840. else
  841. begin
  842. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  843. exit;
  844. end;
  845. end;
  846. end;
  847. else
  848. begin
  849. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  850. exit;
  851. end;
  852. end;
  853. end;
  854. end;
  855. function valid_for_var(p:tnode):boolean;
  856. begin
  857. valid_for_var:=valid_for_assign(p,[]);
  858. end;
  859. function valid_for_formal_var(p : tnode) : boolean;
  860. begin
  861. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  862. end;
  863. function valid_for_formal_const(p : tnode) : boolean;
  864. var
  865. v : boolean;
  866. begin
  867. { p must have been firstpass'd before }
  868. { accept about anything but not a statement ! }
  869. case p.nodetype of
  870. calln,
  871. statementn,
  872. addrn :
  873. begin
  874. { addrn is not allowed as this generate a constant value,
  875. but a tp procvar are allowed (PFV) }
  876. if nf_procvarload in p.flags then
  877. v:=true
  878. else
  879. v:=false;
  880. end;
  881. else
  882. v:=true;
  883. end;
  884. valid_for_formal_const:=v;
  885. end;
  886. function valid_for_assignment(p:tnode):boolean;
  887. begin
  888. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  889. end;
  890. end.
  891. {
  892. $Log$
  893. Revision 1.49 2002-10-05 00:47:03 peter
  894. * support dynamicarray<>nil
  895. Revision 1.48 2002/10/04 21:13:59 peter
  896. * ignore vecn,subscriptn when checking for a procvar loadn
  897. Revision 1.47 2002/09/16 18:09:34 peter
  898. * set_funcret_valid fixed when result was already used in a nested
  899. procedure
  900. Revision 1.46 2002/07/20 11:57:53 florian
  901. * types.pas renamed to defbase.pas because D6 contains a types
  902. unit so this would conflicts if D6 programms are compiled
  903. + Willamette/SSE2 instructions to assembler added
  904. Revision 1.45 2002/05/18 13:34:08 peter
  905. * readded missing revisions
  906. Revision 1.44 2002/05/16 19:46:37 carl
  907. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  908. + try to fix temp allocation (still in ifdef)
  909. + generic constructor calls
  910. + start of tassembler / tmodulebase class cleanup
  911. Revision 1.42 2002/04/02 17:11:28 peter
  912. * tlocation,treference update
  913. * LOC_CONSTANT added for better constant handling
  914. * secondadd splitted in multiple routines
  915. * location_force_reg added for loading a location to a register
  916. of a specified size
  917. * secondassignment parses now first the right and then the left node
  918. (this is compatible with Kylix). This saves a lot of push/pop especially
  919. with string operations
  920. * adapted some routines to use the new cg methods
  921. Revision 1.41 2002/01/16 09:33:46 jonas
  922. * no longer allow assignments to pointer expressions (unless there's a
  923. deref), reported by John Lee
  924. }