htypechk.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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. procedure test_protected_sym(sym : tsym);
  83. procedure test_protected(p : tnode);
  84. function is_procsym_load(p:tnode):boolean;
  85. function is_procsym_call(p:tnode):boolean;
  86. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  87. {
  88. type
  89. tvarstaterequire = (vsr_can_be_undefined,vsr_must_be_valid,
  90. vsr_is_used_after,vsr_must_be_valid_and_is_used_after); }
  91. { sets varsym varstate field correctly }
  92. procedure unset_varstate(p : tnode);
  93. procedure set_varstate(p : tnode;must_be_valid : boolean);
  94. { sets the callunique flag, if the node is a vecn, }
  95. { takes care of type casts etc. }
  96. procedure set_unique(p : tnode);
  97. { sets funcret_is_valid to true, if p contains a funcref node }
  98. procedure set_funcret_is_valid(p : tnode);
  99. function valid_for_formal_var(p : tnode) : boolean;
  100. function valid_for_formal_const(p : tnode) : boolean;
  101. function valid_for_var(p:tnode):boolean;
  102. function valid_for_assignment(p:tnode):boolean;
  103. implementation
  104. uses
  105. globtype,systems,
  106. cutils,verbose,globals,
  107. symconst,symsym,symtable,
  108. types,cpubase,
  109. ncnv,nld,
  110. nmem,ncal,nmat,
  111. {$ifdef newcg}
  112. cgbase
  113. {$else}
  114. hcodegen
  115. {$endif}
  116. ;
  117. type
  118. TValidAssign=(Valid_Property,Valid_Void);
  119. TValidAssigns=set of TValidAssign;
  120. { ld is the left type definition
  121. rd the right type definition
  122. dd the result type definition or voiddef if unkown }
  123. function isbinaryoperatoroverloadable(ld, rd, dd : tdef;
  124. treetyp : tnodetype) : boolean;
  125. begin
  126. isbinaryoperatoroverloadable:=
  127. (treetyp=starstarn) or
  128. (ld.deftype=recorddef) or
  129. (rd.deftype=recorddef) or
  130. (ld.deftype=variantdef) or
  131. (rd.deftype=variantdef) or
  132. ((rd.deftype=pointerdef) and
  133. not(is_pchar(rd) and
  134. (is_chararray(ld) or
  135. (ld.deftype=stringdef) or
  136. (treetyp=addn))) and
  137. (not(ld.deftype in [pointerdef,objectdef,classrefdef,procvardef]) or
  138. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,subn])
  139. ) and
  140. (not is_integer(ld) or not (treetyp in [addn,subn]))
  141. ) or
  142. ((ld.deftype=pointerdef) and
  143. not(is_pchar(ld) and
  144. (is_chararray(rd) or
  145. (rd.deftype=stringdef) or
  146. (treetyp=addn))) and
  147. (not(rd.deftype in [stringdef,pointerdef,objectdef,classrefdef,procvardef]) and
  148. ((not is_integer(rd) and (rd.deftype<>objectdef)
  149. and (rd.deftype<>classrefdef)) or
  150. not (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn])
  151. )
  152. )
  153. ) or
  154. { array def, but not mmx or chararray+[char,string,chararray] }
  155. ((ld.deftype=arraydef) and
  156. not((cs_mmx in aktlocalswitches) and
  157. is_mmx_able_array(ld)) 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_chararray(rd) and
  170. (is_char(ld) or
  171. is_pchar(ld) or
  172. (ld.deftype=stringdef) or
  173. is_chararray(ld)))
  174. ) or
  175. { <> and = are defined for classes }
  176. (
  177. (ld.deftype=objectdef) and
  178. not((treetyp in [equaln,unequaln]) and is_class_or_interface(ld))
  179. ) or
  180. (
  181. (rd.deftype=objectdef) and
  182. not((treetyp in [equaln,unequaln]) and is_class_or_interface(rd))
  183. )
  184. or
  185. { allow other operators that + on strings }
  186. (
  187. (is_char(rd) or
  188. is_pchar(rd) or
  189. (rd.deftype=stringdef) or
  190. is_chararray(rd) or
  191. is_char(ld) or
  192. is_pchar(ld) or
  193. (ld.deftype=stringdef) or
  194. is_chararray(ld)
  195. ) and
  196. not(treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  197. not(is_pchar(ld) and
  198. (is_integer(rd) or (rd.deftype=pointerdef)) and
  199. (treetyp=subn)
  200. )
  201. );
  202. end;
  203. function isunaryoperatoroverloadable(rd,dd : tdef;
  204. treetyp : tnodetype) : boolean;
  205. begin
  206. isunaryoperatoroverloadable:=false;
  207. { what assignment overloading should be allowed ?? }
  208. if (treetyp=assignn) then
  209. begin
  210. isunaryoperatoroverloadable:=true;
  211. { this already get tbs0261 to fail
  212. isunaryoperatoroverloadable:=not is_equal(rd,dd); PM }
  213. end
  214. { should we force that rd and dd are equal ?? }
  215. else if (treetyp=subn { unaryminusn }) then
  216. begin
  217. isunaryoperatoroverloadable:=
  218. not is_integer(rd) and not (rd.deftype=floatdef)
  219. {$ifdef SUPPORT_MMX}
  220. and not ((cs_mmx in aktlocalswitches) and
  221. is_mmx_able_array(rd))
  222. {$endif SUPPORT_MMX}
  223. ;
  224. end
  225. else if (treetyp=notn) then
  226. begin
  227. isunaryoperatoroverloadable:=not is_integer(rd) and not is_boolean(rd)
  228. {$ifdef SUPPORT_MMX}
  229. and not ((cs_mmx in aktlocalswitches) and
  230. is_mmx_able_array(rd))
  231. {$endif SUPPORT_MMX}
  232. ;
  233. end;
  234. end;
  235. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  236. var
  237. ld,rd,dd : tdef;
  238. i : longint;
  239. begin
  240. case pf.parast.symindex.count of
  241. 2 : begin
  242. isoperatoracceptable:=false;
  243. for i:=1 to tok2nodes do
  244. if tok2node[i].tok=optoken then
  245. begin
  246. ld:=tvarsym(pf.parast.symindex.first).vartype.def;
  247. rd:=tvarsym(pf.parast.symindex.first.indexnext).vartype.def;
  248. dd:=pf.rettype.def;
  249. isoperatoracceptable:=
  250. tok2node[i].op_overloading_supported and
  251. isbinaryoperatoroverloadable(ld,rd,dd,tok2node[i].nod);
  252. break;
  253. end;
  254. end;
  255. 1 : begin
  256. rd:=tvarsym(pf.parast.symindex.first).vartype.def;
  257. dd:=pf.rettype.def;
  258. for i:=1 to tok2nodes do
  259. if tok2node[i].tok=optoken then
  260. begin
  261. isoperatoracceptable:=
  262. tok2node[i].op_overloading_supported and
  263. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  264. break;
  265. end;
  266. end;
  267. else
  268. isoperatoracceptable:=false;
  269. end;
  270. end;
  271. function isbinaryoverloaded(var t : tnode) : boolean;
  272. var
  273. rd,ld : tdef;
  274. optoken : ttoken;
  275. ht : tnode;
  276. begin
  277. isbinaryoverloaded:=false;
  278. { overloaded operator ? }
  279. { load easier access variables }
  280. rd:=tbinarynode(t).right.resulttype.def;
  281. ld:=tbinarynode(t).left.resulttype.def;
  282. if isbinaryoperatoroverloadable(ld,rd,voidtype.def,t.nodetype) then
  283. begin
  284. isbinaryoverloaded:=true;
  285. {!!!!!!!!! handle paras }
  286. case t.nodetype of
  287. addn:
  288. optoken:=_PLUS;
  289. subn:
  290. optoken:=_MINUS;
  291. muln:
  292. optoken:=_STAR;
  293. starstarn:
  294. optoken:=_STARSTAR;
  295. slashn:
  296. optoken:=_SLASH;
  297. ltn:
  298. optoken:=tokens._lt;
  299. gtn:
  300. optoken:=tokens._gt;
  301. lten:
  302. optoken:=_lte;
  303. gten:
  304. optoken:=_gte;
  305. equaln,unequaln :
  306. optoken:=_EQUAL;
  307. symdifn :
  308. optoken:=_SYMDIF;
  309. modn :
  310. optoken:=_OP_MOD;
  311. orn :
  312. optoken:=_OP_OR;
  313. xorn :
  314. optoken:=_OP_XOR;
  315. andn :
  316. optoken:=_OP_AND;
  317. divn :
  318. optoken:=_OP_DIV;
  319. shln :
  320. optoken:=_OP_SHL;
  321. shrn :
  322. optoken:=_OP_SHR;
  323. else
  324. exit;
  325. end;
  326. { the nil as symtable signs firstcalln that this is
  327. an overloaded operator }
  328. ht:=ccallnode.create(nil,overloaded_operators[optoken],nil,nil);
  329. { we have to convert p^.left and p^.right into
  330. callparanodes }
  331. if tcallnode(ht).symtableprocentry=nil then
  332. begin
  333. CGMessage(parser_e_operator_not_overloaded);
  334. ht.free;
  335. isbinaryoverloaded:=false;
  336. exit;
  337. end;
  338. inc(tcallnode(ht).symtableprocentry.refs);
  339. { we need copies, because the originals will be destroyed when we give a }
  340. { changed node back to firstpass! (JM) }
  341. if assigned(tbinarynode(t).left) then
  342. if assigned(tbinarynode(t).right) then
  343. tcallnode(ht).left :=
  344. ccallparanode.create(tbinarynode(t).right.getcopy,
  345. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  346. else
  347. tcallnode(ht).left :=
  348. ccallparanode.create(nil,
  349. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  350. else if assigned(tbinarynode(t).right) then
  351. tcallnode(ht).left :=
  352. ccallparanode.create(tbinarynode(t).right.getcopy,
  353. ccallparanode.create(nil,nil));
  354. if t.nodetype=unequaln then
  355. ht:=cnotnode.create(ht);
  356. t:=ht;
  357. end;
  358. end;
  359. {****************************************************************************
  360. Register Calculation
  361. ****************************************************************************}
  362. { marks an lvalue as "unregable" }
  363. procedure make_not_regable(p : tnode);
  364. begin
  365. case p.nodetype of
  366. typeconvn :
  367. make_not_regable(ttypeconvnode(p).left);
  368. loadn :
  369. if tloadnode(p).symtableentry.typ=varsym then
  370. tvarsym(tloadnode(p).symtableentry).varoptions:=tvarsym(tloadnode(p).symtableentry).varoptions-[vo_regable,vo_fpuregable];
  371. end;
  372. end;
  373. { calculates the needed registers for a binary operator }
  374. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  375. begin
  376. p.left_right_max;
  377. { Only when the difference between the left and right registers < the
  378. wanted registers allocate the amount of registers }
  379. if assigned(p.left) then
  380. begin
  381. if assigned(p.right) then
  382. begin
  383. if (abs(p.left.registers32-p.right.registers32)<r32) then
  384. inc(p.registers32,r32);
  385. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  386. inc(p.registersfpu,fpu);
  387. {$ifdef SUPPORT_MMX}
  388. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  389. inc(p.registersmmx,mmx);
  390. {$endif SUPPORT_MMX}
  391. { the following is a little bit guessing but I think }
  392. { it's the only way to solve same internalerrors: }
  393. { if the left and right node both uses registers }
  394. { and return a mem location, but the current node }
  395. { doesn't use an integer register we get probably }
  396. { trouble when restoring a node }
  397. if (p.left.registers32=p.right.registers32) and
  398. (p.registers32=p.left.registers32) and
  399. (p.registers32>0) and
  400. (p.left.location.loc in [LOC_REFERENCE,LOC_MEM]) and
  401. (p.right.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  402. inc(p.registers32);
  403. end
  404. else
  405. begin
  406. if (p.left.registers32<r32) then
  407. inc(p.registers32,r32);
  408. if (p.left.registersfpu<fpu) then
  409. inc(p.registersfpu,fpu);
  410. {$ifdef SUPPORT_MMX}
  411. if (p.left.registersmmx<mmx) then
  412. inc(p.registersmmx,mmx);
  413. {$endif SUPPORT_MMX}
  414. end;
  415. end;
  416. { error CGMessage, if more than 8 floating point }
  417. { registers are needed }
  418. if p.registersfpu>maxfpuregs then
  419. CGMessage(cg_e_too_complex_expr);
  420. end;
  421. {****************************************************************************
  422. Subroutine Handling
  423. ****************************************************************************}
  424. { protected field handling
  425. protected field can not appear in
  426. var parameters of function !!
  427. this can only be done after we have determined the
  428. overloaded function
  429. this is the reason why it is not in the parser, PM }
  430. procedure test_protected_sym(sym : tsym);
  431. begin
  432. if (sp_protected in sym.symoptions) and
  433. (
  434. (
  435. (sym.owner.symtabletype=globalsymtable) and
  436. (sym.owner.unitid<>0)
  437. ) or
  438. (
  439. (sym.owner.symtabletype=objectsymtable) and
  440. (tobjectdef(sym.owner.defowner).owner.symtabletype=globalsymtable) and
  441. (tobjectdef(sym.owner.defowner).owner.unitid<>0)
  442. )
  443. ) then
  444. CGMessage(parser_e_cant_access_protected_member);
  445. end;
  446. procedure test_protected(p : tnode);
  447. begin
  448. case p.nodetype of
  449. loadn : test_protected_sym(tloadnode(p).symtableentry);
  450. typeconvn : test_protected(ttypeconvnode(p).left);
  451. derefn : test_protected(tderefnode(p).left);
  452. subscriptn : begin
  453. { test_protected(p.left);
  454. Is a field of a protected var
  455. also protected ??? PM }
  456. test_protected_sym(tsubscriptnode(p).vs);
  457. end;
  458. end;
  459. end;
  460. function is_procsym_load(p:tnode):boolean;
  461. begin
  462. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry.typ=procsym)) or
  463. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  464. and (tloadnode(taddrnode(p).left).symtableentry.typ=procsym)) ;
  465. end;
  466. { change a proc call to a procload for assignment to a procvar }
  467. { this can only happen for proc/function without arguments }
  468. function is_procsym_call(p:tnode):boolean;
  469. begin
  470. is_procsym_call:=(p.nodetype=calln) and (tcallnode(p).left=nil) and
  471. (((tcallnode(p).symtableprocentry.typ=procsym) and (tcallnode(p).right=nil)) or
  472. (assigned(tcallnode(p).right) and (tcallnode(tcallnode(p).right).symtableprocentry.typ=varsym)));
  473. end;
  474. { local routines can't be assigned to procvars }
  475. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  476. begin
  477. if (from_def.symtablelevel>1) and (to_def.deftype=procvardef) then
  478. CGMessage(type_e_cannot_local_proc_to_procvar);
  479. end;
  480. procedure set_varstate(p : tnode;must_be_valid : boolean);
  481. var
  482. hsym : tvarsym;
  483. begin
  484. while assigned(p) do
  485. begin
  486. if (nf_varstateset in p.flags) then
  487. exit;
  488. include(p.flags,nf_varstateset);
  489. case p.nodetype of
  490. typeconvn :
  491. begin
  492. case ttypeconvnode(p).convtype of
  493. tc_cchar_2_pchar,
  494. tc_cstring_2_pchar,
  495. tc_array_2_pointer :
  496. must_be_valid:=false;
  497. tc_pchar_2_string,
  498. tc_pointer_2_array :
  499. must_be_valid:=true;
  500. end;
  501. p:=tunarynode(p).left;
  502. end;
  503. subscriptn :
  504. p:=tunarynode(p).left;
  505. vecn:
  506. begin
  507. set_varstate(tbinarynode(p).right,true);
  508. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  509. must_be_valid:=true;
  510. p:=tunarynode(p).left;
  511. end;
  512. { do not parse calln }
  513. calln :
  514. break;
  515. callparan :
  516. begin
  517. set_varstate(tbinarynode(p).right,must_be_valid);
  518. p:=tunarynode(p).left;
  519. end;
  520. loadn :
  521. begin
  522. if (tloadnode(p).symtableentry.typ=varsym) then
  523. begin
  524. hsym:=tvarsym(tloadnode(p).symtableentry);
  525. if must_be_valid and (nf_first in p.flags) then
  526. begin
  527. if (hsym.varstate=vs_declared_and_first_found) or
  528. (hsym.varstate=vs_set_but_first_not_passed) then
  529. begin
  530. if (assigned(hsym.owner) and
  531. assigned(aktprocsym) and
  532. (hsym.owner = aktprocsym.definition.localst)) then
  533. begin
  534. if tloadnode(p).symtable.symtabletype=localsymtable then
  535. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  536. else
  537. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  538. end;
  539. end;
  540. end;
  541. if (nf_first in p.flags) then
  542. begin
  543. if hsym.varstate=vs_declared_and_first_found then
  544. begin
  545. { this can only happen at left of an assignment, no ? PM }
  546. if (parsing_para_level=0) and not must_be_valid then
  547. hsym.varstate:=vs_assigned
  548. else
  549. hsym.varstate:=vs_used;
  550. end
  551. else
  552. if hsym.varstate=vs_set_but_first_not_passed then
  553. hsym.varstate:=vs_used;
  554. exclude(p.flags,nf_first);
  555. end
  556. else
  557. begin
  558. if (hsym.varstate=vs_assigned) and
  559. (must_be_valid or (parsing_para_level>0) or
  560. (p.resulttype.def.deftype=procvardef)) then
  561. hsym.varstate:=vs_used;
  562. if (hsym.varstate=vs_declared_and_first_found) and
  563. (must_be_valid or (parsing_para_level>0) or
  564. (p.resulttype.def.deftype=procvardef)) then
  565. hsym.varstate:=vs_set_but_first_not_passed;
  566. end;
  567. end;
  568. break;
  569. end;
  570. funcretn:
  571. begin
  572. { no claim if setting higher return value_str }
  573. if must_be_valid and
  574. (procinfo=pprocinfo(tfuncretnode(p).funcretprocinfo)) and
  575. ((procinfo^.funcret_state=vs_declared) or
  576. ((nf_is_first_funcret in p.flags) and
  577. (procinfo^.funcret_state=vs_declared_and_first_found))) then
  578. begin
  579. CGMessage(sym_w_function_result_not_set);
  580. { avoid multiple warnings }
  581. procinfo^.funcret_state:=vs_assigned;
  582. end;
  583. if (nf_is_first_funcret in p.flags) and not must_be_valid then
  584. pprocinfo(tfuncretnode(p).funcretprocinfo)^.funcret_state:=vs_assigned;
  585. break;
  586. end;
  587. else
  588. break;
  589. end;{case }
  590. end;
  591. end;
  592. procedure unset_varstate(p : tnode);
  593. begin
  594. while assigned(p) do
  595. begin
  596. exclude(p.flags,nf_varstateset);
  597. case p.nodetype of
  598. typeconvn,
  599. subscriptn,
  600. vecn :
  601. p:=tunarynode(p).left;
  602. else
  603. break;
  604. end;
  605. end;
  606. end;
  607. procedure set_unique(p : tnode);
  608. begin
  609. while assigned(p) do
  610. begin
  611. case p.nodetype of
  612. vecn:
  613. begin
  614. include(p.flags,nf_callunique);
  615. break;
  616. end;
  617. typeconvn,
  618. subscriptn,
  619. derefn:
  620. p:=tunarynode(p).left;
  621. else
  622. break;
  623. end;
  624. end;
  625. end;
  626. procedure set_funcret_is_valid(p:tnode);
  627. begin
  628. while assigned(p) do
  629. begin
  630. case p.nodetype of
  631. funcretn:
  632. begin
  633. if (nf_is_first_funcret in p.flags) then
  634. pprocinfo(tfuncretnode(p).funcretprocinfo)^.funcret_state:=vs_assigned;
  635. break;
  636. end;
  637. vecn,
  638. {derefn,}
  639. typeconvn,
  640. subscriptn:
  641. p:=tunarynode(p).left;
  642. else
  643. break;
  644. end;
  645. end;
  646. end;
  647. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  648. var
  649. hp : tnode;
  650. gotwith,
  651. gotsubscript,
  652. gotpointer,
  653. gotclass,
  654. gotderef : boolean;
  655. begin
  656. valid_for_assign:=false;
  657. gotsubscript:=false;
  658. gotderef:=false;
  659. gotclass:=false;
  660. gotpointer:=false;
  661. gotwith:=false;
  662. hp:=p;
  663. if not(valid_void in opts) and
  664. is_void(hp.resulttype.def) then
  665. begin
  666. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  667. exit;
  668. end;
  669. while assigned(hp) do
  670. begin
  671. { property allowed? calln has a property check itself }
  672. if (nf_isproperty in hp.flags) then
  673. begin
  674. if (valid_property in opts) then
  675. valid_for_assign:=true
  676. else
  677. begin
  678. { check return type }
  679. case hp.resulttype.def.deftype of
  680. pointerdef :
  681. gotpointer:=true;
  682. objectdef :
  683. gotclass:=is_class_or_interface(hp.resulttype.def);
  684. recorddef, { handle record like class it needs a subscription }
  685. classrefdef :
  686. gotclass:=true;
  687. end;
  688. { 1. if it returns a pointer and we've found a deref,
  689. 2. if it returns a class or record and a subscription or with is found }
  690. if (gotpointer and gotderef) or
  691. (gotclass and (gotsubscript or gotwith)) then
  692. valid_for_assign:=true
  693. else
  694. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  695. end;
  696. exit;
  697. end;
  698. case hp.nodetype of
  699. derefn :
  700. begin
  701. gotderef:=true;
  702. hp:=tderefnode(hp).left;
  703. end;
  704. typeconvn :
  705. begin
  706. case hp.resulttype.def.deftype of
  707. pointerdef :
  708. gotpointer:=true;
  709. objectdef :
  710. gotclass:=is_class_or_interface(hp.resulttype.def);
  711. classrefdef :
  712. gotclass:=true;
  713. arraydef :
  714. begin
  715. { pointer -> array conversion is done then we need to see it
  716. as a deref, because a ^ is then not required anymore }
  717. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  718. gotderef:=true;
  719. end;
  720. end;
  721. hp:=ttypeconvnode(hp).left;
  722. end;
  723. vecn,
  724. asn :
  725. hp:=tunarynode(hp).left;
  726. subscriptn :
  727. begin
  728. gotsubscript:=true;
  729. { a class/interface access is an implicit }
  730. { dereferencing }
  731. hp:=tsubscriptnode(hp).left;
  732. if is_class_or_interface(hp.resulttype.def) then
  733. gotderef:=true;
  734. end;
  735. subn,
  736. addn :
  737. begin
  738. { Allow add/sub operators on a pointer, or an integer
  739. and a pointer typecast and deref has been found }
  740. if (hp.resulttype.def.deftype=pointerdef) or
  741. (is_integer(hp.resulttype.def) and gotpointer and gotderef) then
  742. valid_for_assign:=true
  743. else
  744. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  745. exit;
  746. end;
  747. addrn :
  748. begin
  749. if gotderef or
  750. (nf_procvarload in hp.flags) then
  751. valid_for_assign:=true
  752. else
  753. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  754. exit;
  755. end;
  756. selfn,
  757. funcretn :
  758. begin
  759. valid_for_assign:=true;
  760. exit;
  761. end;
  762. calln :
  763. begin
  764. { check return type }
  765. case hp.resulttype.def.deftype of
  766. pointerdef :
  767. gotpointer:=true;
  768. objectdef :
  769. gotclass:=is_class_or_interface(hp.resulttype.def);
  770. recorddef, { handle record like class it needs a subscription }
  771. classrefdef :
  772. gotclass:=true;
  773. end;
  774. { 1. if it returns a pointer and we've found a deref,
  775. 2. if it returns a class or record and a subscription or with is found }
  776. if (gotpointer and gotderef) or
  777. (gotclass and (gotsubscript or gotwith)) then
  778. valid_for_assign:=true
  779. else
  780. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  781. exit;
  782. end;
  783. loadn :
  784. begin
  785. case tloadnode(hp).symtableentry.typ of
  786. absolutesym,
  787. varsym :
  788. begin
  789. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  790. begin
  791. { allow p^:= constructions with p is const parameter }
  792. if gotderef then
  793. valid_for_assign:=true
  794. else
  795. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  796. exit;
  797. end;
  798. { Are we at a with symtable, then we need to process the
  799. withrefnode also to check for maybe a const load }
  800. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  801. begin
  802. { continue with processing the withref node }
  803. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  804. gotwith:=true;
  805. end
  806. else
  807. begin
  808. { set the assigned flag for varsyms }
  809. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  810. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  811. valid_for_assign:=true;
  812. exit;
  813. end;
  814. end;
  815. funcretsym,
  816. typedconstsym :
  817. begin
  818. valid_for_assign:=true;
  819. exit;
  820. end;
  821. end;
  822. end;
  823. else
  824. begin
  825. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  826. exit;
  827. end;
  828. end;
  829. end;
  830. end;
  831. function valid_for_var(p:tnode):boolean;
  832. begin
  833. valid_for_var:=valid_for_assign(p,[]);
  834. end;
  835. function valid_for_formal_var(p : tnode) : boolean;
  836. begin
  837. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  838. end;
  839. function valid_for_formal_const(p : tnode) : boolean;
  840. var
  841. v : boolean;
  842. begin
  843. { p must have been firstpass'd before }
  844. { accept about anything but not a statement ! }
  845. case p.nodetype of
  846. calln,
  847. statementn,
  848. addrn :
  849. begin
  850. { addrn is not allowed as this generate a constant value,
  851. but a tp procvar are allowed (PFV) }
  852. if nf_procvarload in p.flags then
  853. v:=true
  854. else
  855. v:=false;
  856. end;
  857. else
  858. v:=true;
  859. end;
  860. valid_for_formal_const:=v;
  861. end;
  862. function valid_for_assignment(p:tnode):boolean;
  863. begin
  864. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  865. end;
  866. end.
  867. {
  868. $Log$
  869. Revision 1.29 2001-06-04 18:04:36 peter
  870. * fixes to valid_for_assign for properties
  871. Revision 1.28 2001/06/04 11:48:02 peter
  872. * better const to var checking
  873. Revision 1.27 2001/05/18 22:57:08 peter
  874. * replace constant by cpu dependent value (merged)
  875. Revision 1.26 2001/05/08 08:52:05 jonas
  876. * fix from Peter to avoid excessive number of warnings
  877. Revision 1.25 2001/04/22 22:46:49 florian
  878. * more variant support
  879. Revision 1.24 2001/04/13 01:22:08 peter
  880. * symtable change to classes
  881. * range check generation and errors fixed, make cycle DEBUG=1 works
  882. * memory leaks fixed
  883. Revision 1.23 2001/04/02 21:20:29 peter
  884. * resulttype rewrite
  885. Revision 1.22 2001/02/20 21:46:26 peter
  886. * don't allow assign to void type (merged)
  887. Revision 1.21 2001/02/04 11:12:17 jonas
  888. * fixed web bug 1377 & const pointer arithmtic
  889. Revision 1.20 2000/12/09 13:04:05 florian
  890. * web bug 1207 fixed: field and properties of const classes can be
  891. changed
  892. Revision 1.19 2000/11/29 00:30:31 florian
  893. * unused units removed from uses clause
  894. * some changes for widestrings
  895. Revision 1.18 2000/11/28 17:14:33 jonas
  896. * fixed crash when trying to use an overloaded operator which is nowhere
  897. defined
  898. Revision 1.17 2000/11/28 14:04:03 jonas
  899. * fixed operator overloading problems
  900. Revision 1.16 2000/11/13 11:30:54 florian
  901. * some bugs with interfaces and NIL fixed
  902. Revision 1.15 2000/11/12 22:20:37 peter
  903. * create generic toutputsection for binary writers
  904. Revision 1.14 2000/11/04 14:25:19 florian
  905. + merged Attila's changes for interfaces, not tested yet
  906. Revision 1.13 2000/10/31 22:02:47 peter
  907. * symtable splitted, no real code changes
  908. Revision 1.12 2000/10/14 10:14:47 peter
  909. * moehrendorf oct 2000 rewrite
  910. Revision 1.11 2000/10/01 19:48:23 peter
  911. * lot of compile updates for cg11
  912. Revision 1.10 2000/09/29 15:45:23 florian
  913. * make cycle fixed
  914. Revision 1.9 2000/09/28 19:49:51 florian
  915. *** empty log message ***
  916. Revision 1.8 2000/09/27 18:14:31 florian
  917. * fixed a lot of syntax errors in the n*.pas stuff
  918. Revision 1.7 2000/09/26 20:06:13 florian
  919. * hmm, still a lot of work to get things compilable
  920. Revision 1.6 2000/09/24 15:06:17 peter
  921. * use defines.inc
  922. Revision 1.5 2000/08/27 16:11:51 peter
  923. * moved some util functions from globals,cobjects to cutils
  924. * splitted files into finput,fmodule
  925. Revision 1.4 2000/08/16 18:33:53 peter
  926. * splitted namedobjectitem.next into indexnext and listnext so it
  927. can be used in both lists
  928. * don't allow "word = word" type definitions (merged)
  929. Revision 1.3 2000/08/07 11:31:04 jonas
  930. * fixed bug in type conversions between enum subranges (it didn't take
  931. the packenum directive into account)
  932. + define PACKENUMFIXED symbol in options.pas
  933. (merged from fixes branch)
  934. Revision 1.2 2000/07/13 11:32:41 michael
  935. + removed logs
  936. }