pexpr.pas 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Does parsing of expression for Free Pascal
  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 pexpr;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. node,
  24. globals,
  25. cpuinfo;
  26. { reads a whole expression }
  27. function expr : tnode;
  28. { reads an expression without assignements and .. }
  29. function comp_expr(accept_equal : boolean):tnode;
  30. { reads a single factor }
  31. function factor(getaddr : boolean) : tnode;
  32. procedure string_dec(var t: ttype);
  33. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  34. { the ID token has to be consumed before calling this function }
  35. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callnflags:tnodeflags);
  36. {$ifdef int64funcresok}
  37. function get_intconst:TConstExprInt;
  38. {$else int64funcresok}
  39. function get_intconst:longint;
  40. {$endif int64funcresok}
  41. function get_stringconst:string;
  42. implementation
  43. uses
  44. {$ifdef delphi}
  45. SysUtils,
  46. {$endif}
  47. { common }
  48. cutils,
  49. { global }
  50. globtype,tokens,verbose,
  51. systems,widestr,
  52. { symtable }
  53. symconst,symbase,symdef,symsym,symtable,defutil,defcmp,
  54. { pass 1 }
  55. pass_1,htypechk,
  56. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  57. { parser }
  58. scanner,
  59. pbase,pinline,
  60. { codegen }
  61. cgbase
  62. ;
  63. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  64. read as - (1**4) and not (-1)**4 PM }
  65. type
  66. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  67. const
  68. highest_precedence = oppower;
  69. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  70. const
  71. got_addrn : boolean = false;
  72. anon_inherited : boolean = false;
  73. procedure string_dec(var t: ttype);
  74. { reads a string type with optional length }
  75. { and returns a pointer to the string }
  76. { definition }
  77. var
  78. p : tnode;
  79. begin
  80. t:=cshortstringtype;
  81. consume(_STRING);
  82. if token=_LECKKLAMMER then
  83. begin
  84. consume(_LECKKLAMMER);
  85. p:=comp_expr(true);
  86. if not is_constintnode(p) then
  87. begin
  88. Message(cg_e_illegal_expression);
  89. { error recovery }
  90. consume(_RECKKLAMMER);
  91. end
  92. else
  93. begin
  94. if (tordconstnode(p).value<=0) then
  95. begin
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. end;
  99. consume(_RECKKLAMMER);
  100. if tordconstnode(p).value>255 then
  101. begin
  102. { longstring is currently unsupported (CEC)! }
  103. { t.setdef(tstringdef.createlong(tordconstnode(p).value))}
  104. Message(parser_e_invalid_string_size);
  105. tordconstnode(p).value:=255;
  106. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  107. end
  108. else
  109. if tordconstnode(p).value<>255 then
  110. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  111. end;
  112. p.free;
  113. end
  114. else
  115. begin
  116. if cs_ansistrings in aktlocalswitches then
  117. t:=cansistringtype
  118. else
  119. t:=cshortstringtype;
  120. end;
  121. end;
  122. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  123. var
  124. p1,p2 : tnode;
  125. end_of_paras : ttoken;
  126. prev_in_args : boolean;
  127. old_allow_array_constructor : boolean;
  128. begin
  129. if in_prop_paras then
  130. end_of_paras:=_RECKKLAMMER
  131. else
  132. end_of_paras:=_RKLAMMER;
  133. if token=end_of_paras then
  134. begin
  135. parse_paras:=nil;
  136. exit;
  137. end;
  138. { save old values }
  139. prev_in_args:=in_args;
  140. old_allow_array_constructor:=allow_array_constructor;
  141. { set para parsing values }
  142. in_args:=true;
  143. inc(parsing_para_level);
  144. allow_array_constructor:=true;
  145. p2:=nil;
  146. while true do
  147. begin
  148. p1:=comp_expr(true);
  149. p2:=ccallparanode.create(p1,p2);
  150. { it's for the str(l:5,s); }
  151. if __colon and (token=_COLON) then
  152. begin
  153. consume(_COLON);
  154. p1:=comp_expr(true);
  155. p2:=ccallparanode.create(p1,p2);
  156. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  157. if token=_COLON then
  158. begin
  159. consume(_COLON);
  160. p1:=comp_expr(true);
  161. p2:=ccallparanode.create(p1,p2);
  162. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  163. end
  164. end;
  165. if token=_COMMA then
  166. consume(_COMMA)
  167. else
  168. break;
  169. end;
  170. allow_array_constructor:=old_allow_array_constructor;
  171. dec(parsing_para_level);
  172. in_args:=prev_in_args;
  173. parse_paras:=p2;
  174. end;
  175. procedure check_tp_procvar(var p : tnode);
  176. var
  177. hp,
  178. p1 : tnode;
  179. begin
  180. if (m_tp_procvar in aktmodeswitches) and
  181. (token<>_ASSIGNMENT) and
  182. (not got_addrn) and
  183. (block_type=bt_general) then
  184. begin
  185. { ignore vecn,subscriptn }
  186. hp:=p;
  187. repeat
  188. case hp.nodetype of
  189. vecn :
  190. hp:=tvecnode(hp).left;
  191. subscriptn :
  192. hp:=tsubscriptnode(hp).left;
  193. else
  194. break;
  195. end;
  196. until false;
  197. if (hp.nodetype=loadn) then
  198. begin
  199. { get the resulttype of p }
  200. do_resulttypepass(p);
  201. { convert the procvar load to a call:
  202. - not expecting a procvar
  203. - the procvar does not get arguments, when it
  204. requires arguments the callnode will fail
  205. Note: When arguments were passed there was no loadn }
  206. if (getprocvardef=nil) and
  207. (p.resulttype.def.deftype=procvardef) and
  208. (tprocvardef(p.resulttype.def).minparacount=0) then
  209. begin
  210. p1:=ccallnode.create_procvar(nil,p);
  211. resulttypepass(p1);
  212. p:=p1;
  213. end;
  214. end;
  215. end;
  216. end;
  217. function statement_syssym(l : longint) : tnode;
  218. var
  219. p1,p2,paras : tnode;
  220. err,
  221. prev_in_args : boolean;
  222. begin
  223. prev_in_args:=in_args;
  224. case l of
  225. in_new_x :
  226. begin
  227. if afterassignment or in_args then
  228. statement_syssym:=new_function
  229. else
  230. statement_syssym:=new_dispose_statement(true);
  231. end;
  232. in_dispose_x :
  233. begin
  234. statement_syssym:=new_dispose_statement(false);
  235. end;
  236. in_ord_x :
  237. begin
  238. consume(_LKLAMMER);
  239. in_args:=true;
  240. p1:=comp_expr(true);
  241. consume(_RKLAMMER);
  242. p1:=geninlinenode(in_ord_x,false,p1);
  243. statement_syssym := p1;
  244. end;
  245. in_exit :
  246. begin
  247. if try_to_consume(_LKLAMMER) then
  248. begin
  249. p1:=comp_expr(true);
  250. consume(_RKLAMMER);
  251. if (block_type=bt_except) then
  252. Message(parser_e_exit_with_argument_not__possible);
  253. if is_void(current_procdef.rettype.def) then
  254. Message(parser_e_void_function);
  255. end
  256. else
  257. p1:=nil;
  258. statement_syssym:=cexitnode.create(p1);
  259. end;
  260. in_break :
  261. begin
  262. statement_syssym:=cbreaknode.create;
  263. end;
  264. in_continue :
  265. begin
  266. statement_syssym:=ccontinuenode.create;
  267. end;
  268. in_typeof_x :
  269. begin
  270. consume(_LKLAMMER);
  271. in_args:=true;
  272. p1:=comp_expr(true);
  273. consume(_RKLAMMER);
  274. if p1.nodetype=typen then
  275. ttypenode(p1).allowed:=true;
  276. if (p1.resulttype.def.deftype = objectdef) then
  277. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  278. else
  279. begin
  280. Message(parser_e_class_id_expected);
  281. p1.destroy;
  282. statement_syssym:=cerrornode.create;
  283. end;
  284. end;
  285. in_sizeof_x :
  286. begin
  287. consume(_LKLAMMER);
  288. in_args:=true;
  289. p1:=comp_expr(true);
  290. consume(_RKLAMMER);
  291. if (p1.nodetype<>typen) and
  292. (
  293. (is_object(p1.resulttype.def) and
  294. (oo_has_constructor in tobjectdef(p1.resulttype.def).objectoptions)) or
  295. is_open_array(p1.resulttype.def) or
  296. is_open_string(p1.resulttype.def)
  297. ) then
  298. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  299. else
  300. begin
  301. statement_syssym:=cordconstnode.create(p1.resulttype.def.size,s32bittype,true);
  302. { p1 not needed !}
  303. p1.destroy;
  304. end;
  305. end;
  306. in_typeinfo_x :
  307. begin
  308. consume(_LKLAMMER);
  309. in_args:=true;
  310. p1:=comp_expr(true);
  311. if p1.nodetype=typen then
  312. ttypenode(p1).allowed:=true
  313. else
  314. begin
  315. p1.destroy;
  316. p1:=cerrornode.create;
  317. Message(parser_e_illegal_parameter_list);
  318. end;
  319. consume(_RKLAMMER);
  320. p2:=ccallparanode.create(p1,nil);
  321. p2:=geninlinenode(in_typeinfo_x,false,p2);
  322. statement_syssym:=p2;
  323. end;
  324. in_assigned_x :
  325. begin
  326. err:=false;
  327. consume(_LKLAMMER);
  328. in_args:=true;
  329. p1:=comp_expr(true);
  330. if not codegenerror then
  331. begin
  332. { With tp procvars we allways need to load a
  333. procvar when it is passed }
  334. if (m_tp_procvar in aktmodeswitches) and
  335. (p1.nodetype=calln) then
  336. load_procvar_from_calln(p1);
  337. case p1.resulttype.def.deftype of
  338. pointerdef,
  339. procvardef,
  340. classrefdef : ;
  341. objectdef :
  342. if not is_class_or_interface(p1.resulttype.def) then
  343. begin
  344. Message(parser_e_illegal_parameter_list);
  345. err:=true;
  346. end;
  347. else
  348. begin
  349. Message(parser_e_illegal_parameter_list);
  350. err:=true;
  351. end;
  352. end;
  353. end
  354. else
  355. err:=true;
  356. if not err then
  357. begin
  358. p2:=ccallparanode.create(p1,nil);
  359. p2:=geninlinenode(in_assigned_x,false,p2);
  360. end
  361. else
  362. begin
  363. p1.free;
  364. p2:=cerrornode.create;
  365. end;
  366. consume(_RKLAMMER);
  367. statement_syssym:=p2;
  368. end;
  369. in_addr_x :
  370. begin
  371. consume(_LKLAMMER);
  372. in_args:=true;
  373. p1:=comp_expr(true);
  374. p1:=caddrnode.create(p1);
  375. consume(_RKLAMMER);
  376. statement_syssym:=p1;
  377. end;
  378. in_ofs_x :
  379. begin
  380. consume(_LKLAMMER);
  381. in_args:=true;
  382. p1:=comp_expr(true);
  383. p1:=caddrnode.create(p1);
  384. do_resulttypepass(p1);
  385. { Ofs() returns a cardinal, not a pointer }
  386. p1.resulttype:=u32bittype;
  387. consume(_RKLAMMER);
  388. statement_syssym:=p1;
  389. end;
  390. in_seg_x :
  391. begin
  392. consume(_LKLAMMER);
  393. in_args:=true;
  394. p1:=comp_expr(true);
  395. p1:=geninlinenode(in_seg_x,false,p1);
  396. consume(_RKLAMMER);
  397. statement_syssym:=p1;
  398. end;
  399. in_high_x,
  400. in_low_x :
  401. begin
  402. consume(_LKLAMMER);
  403. in_args:=true;
  404. p1:=comp_expr(true);
  405. p2:=geninlinenode(l,false,p1);
  406. consume(_RKLAMMER);
  407. statement_syssym:=p2;
  408. end;
  409. in_succ_x,
  410. in_pred_x :
  411. begin
  412. consume(_LKLAMMER);
  413. in_args:=true;
  414. p1:=comp_expr(true);
  415. p2:=geninlinenode(l,false,p1);
  416. consume(_RKLAMMER);
  417. statement_syssym:=p2;
  418. end;
  419. in_inc_x,
  420. in_dec_x :
  421. begin
  422. consume(_LKLAMMER);
  423. in_args:=true;
  424. p1:=comp_expr(true);
  425. if token=_COMMA then
  426. begin
  427. consume(_COMMA);
  428. p2:=ccallparanode.create(comp_expr(true),nil);
  429. end
  430. else
  431. p2:=nil;
  432. p2:=ccallparanode.create(p1,p2);
  433. statement_syssym:=geninlinenode(l,false,p2);
  434. consume(_RKLAMMER);
  435. end;
  436. in_finalize_x:
  437. begin
  438. statement_syssym:=inline_finalize;
  439. end;
  440. in_copy_x:
  441. begin
  442. statement_syssym:=inline_copy;
  443. end;
  444. in_concat_x :
  445. begin
  446. consume(_LKLAMMER);
  447. in_args:=true;
  448. p2:=nil;
  449. while true do
  450. begin
  451. p1:=comp_expr(true);
  452. set_varstate(p1,true);
  453. if not((p1.resulttype.def.deftype=stringdef) or
  454. ((p1.resulttype.def.deftype=orddef) and
  455. (torddef(p1.resulttype.def).typ=uchar))) then
  456. Message(parser_e_illegal_parameter_list);
  457. if p2<>nil then
  458. p2:=caddnode.create(addn,p2,p1)
  459. else
  460. p2:=p1;
  461. if token=_COMMA then
  462. consume(_COMMA)
  463. else
  464. break;
  465. end;
  466. consume(_RKLAMMER);
  467. statement_syssym:=p2;
  468. end;
  469. in_read_x,
  470. in_readln_x :
  471. begin
  472. if token=_LKLAMMER then
  473. begin
  474. consume(_LKLAMMER);
  475. paras:=parse_paras(false,false);
  476. consume(_RKLAMMER);
  477. end
  478. else
  479. paras:=nil;
  480. p1:=geninlinenode(l,false,paras);
  481. statement_syssym := p1;
  482. end;
  483. in_setlength_x:
  484. begin
  485. statement_syssym := inline_setlength;
  486. end;
  487. in_length_x:
  488. begin
  489. consume(_LKLAMMER);
  490. in_args:=true;
  491. p1:=comp_expr(true);
  492. p2:=geninlinenode(l,false,p1);
  493. consume(_RKLAMMER);
  494. statement_syssym:=p2;
  495. end;
  496. in_write_x,
  497. in_writeln_x :
  498. begin
  499. if token=_LKLAMMER then
  500. begin
  501. consume(_LKLAMMER);
  502. paras:=parse_paras(true,false);
  503. consume(_RKLAMMER);
  504. end
  505. else
  506. paras:=nil;
  507. p1 := geninlinenode(l,false,paras);
  508. statement_syssym := p1;
  509. end;
  510. in_str_x_string :
  511. begin
  512. consume(_LKLAMMER);
  513. paras:=parse_paras(true,false);
  514. consume(_RKLAMMER);
  515. p1 := geninlinenode(l,false,paras);
  516. statement_syssym := p1;
  517. end;
  518. in_val_x:
  519. Begin
  520. consume(_LKLAMMER);
  521. in_args := true;
  522. p1:= ccallparanode.create(comp_expr(true), nil);
  523. consume(_COMMA);
  524. p2 := ccallparanode.create(comp_expr(true),p1);
  525. if (token = _COMMA) then
  526. Begin
  527. consume(_COMMA);
  528. p2 := ccallparanode.create(comp_expr(true),p2)
  529. End;
  530. consume(_RKLAMMER);
  531. p2 := geninlinenode(l,false,p2);
  532. statement_syssym := p2;
  533. End;
  534. in_include_x_y,
  535. in_exclude_x_y :
  536. begin
  537. consume(_LKLAMMER);
  538. in_args:=true;
  539. p1:=comp_expr(true);
  540. consume(_COMMA);
  541. p2:=comp_expr(true);
  542. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  543. consume(_RKLAMMER);
  544. end;
  545. in_assert_x_y :
  546. begin
  547. consume(_LKLAMMER);
  548. in_args:=true;
  549. p1:=comp_expr(true);
  550. if token=_COMMA then
  551. begin
  552. consume(_COMMA);
  553. p2:=comp_expr(true);
  554. end
  555. else
  556. begin
  557. { then insert an empty string }
  558. p2:=cstringconstnode.createstr('',st_default);
  559. end;
  560. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  561. consume(_RKLAMMER);
  562. end;
  563. else
  564. internalerror(15);
  565. end;
  566. in_args:=prev_in_args;
  567. end;
  568. function maybe_load_methodpointer(st:tsymtable;var p1:tnode):boolean;
  569. begin
  570. maybe_load_methodpointer:=false;
  571. if not assigned(p1) then
  572. begin
  573. case st.symtabletype of
  574. withsymtable :
  575. begin
  576. if (st.defowner.deftype=objectdef) then
  577. begin
  578. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  579. maybe_load_methodpointer:=true;
  580. end;
  581. end;
  582. objectsymtable :
  583. begin
  584. p1:=load_self;
  585. maybe_load_methodpointer:=true;
  586. end;
  587. end;
  588. end;
  589. end;
  590. { reads the parameter for a subroutine call }
  591. procedure do_proc_call(sym:tsym;st:tsymtable;getaddr:boolean;var again : boolean;var p1:tnode);
  592. var
  593. membercall,
  594. prevafterassn : boolean;
  595. vs : tvarsym;
  596. para,p2 : tnode;
  597. currpara : tparaitem;
  598. aprocdef : tprocdef;
  599. begin
  600. prevafterassn:=afterassignment;
  601. afterassignment:=false;
  602. membercall:=false;
  603. aprocdef:=nil;
  604. { when it is a call to a member we need to load the
  605. methodpointer first }
  606. membercall:=maybe_load_methodpointer(st,p1);
  607. { When we are expecting a procvar we also need
  608. to get the address in some cases }
  609. if assigned(getprocvardef) then
  610. begin
  611. if (block_type=bt_const) or
  612. getaddr then
  613. begin
  614. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  615. getaddr:=true;
  616. end
  617. else
  618. if (m_tp_procvar in aktmodeswitches) then
  619. begin
  620. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  621. if assigned(aprocdef) then
  622. getaddr:=true;
  623. end;
  624. end;
  625. { only need to get the address of the procedure? }
  626. if getaddr then
  627. begin
  628. { Retrieve info which procvar to call. For tp_procvar the
  629. aprocdef is already loaded above so we can reuse it }
  630. if not assigned(aprocdef) and
  631. assigned(getprocvardef) then
  632. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  633. { generate a methodcallnode or proccallnode }
  634. { we shouldn't convert things like @tcollection.load }
  635. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  636. if assigned(p1) then
  637. begin
  638. if (p1.nodetype<>typen) then
  639. tloadnode(p2).set_mp(p1)
  640. else
  641. p1.free;
  642. end;
  643. p1:=p2;
  644. { no postfix operators }
  645. again:=false;
  646. end
  647. else
  648. begin
  649. para:=nil;
  650. if anon_inherited then
  651. begin
  652. if not assigned(current_procdef) then
  653. internalerror(200305054);
  654. currpara:=tparaitem(current_procdef.para.first);
  655. while assigned(currpara) do
  656. begin
  657. if not currpara.is_hidden then
  658. begin
  659. vs:=tvarsym(currpara.parasym);
  660. { if there is a localcopy then use that }
  661. if assigned(vs.localvarsym) then
  662. vs:=vs.localvarsym;
  663. para:=ccallparanode.create(cloadnode.create(vs,vs.owner),para);
  664. end;
  665. currpara:=tparaitem(currpara.next);
  666. end;
  667. end
  668. else
  669. begin
  670. if try_to_consume(_LKLAMMER) then
  671. begin
  672. para:=parse_paras(false,false);
  673. consume(_RKLAMMER);
  674. end;
  675. end;
  676. p1:=ccallnode.create(para,tprocsym(sym),st,p1);
  677. { indicate if this call was generated by a member and
  678. no explicit self is used, this is needed to determine
  679. how to handle a destructor call (PFV) }
  680. if membercall then
  681. include(p1.flags,nf_member_call);
  682. end;
  683. afterassignment:=prevafterassn;
  684. end;
  685. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  686. var
  687. hp,hp2 : tnode;
  688. hpp : ^tnode;
  689. currprocdef : tprocdef;
  690. begin
  691. if not assigned(pv) then
  692. internalerror(200301121);
  693. if (m_tp_procvar in aktmodeswitches) then
  694. begin
  695. hp:=p2;
  696. hpp:=@p2;
  697. while assigned(hp) and
  698. (hp.nodetype=typeconvn) do
  699. begin
  700. hp:=ttypeconvnode(hp).left;
  701. { save orignal address of the old tree so we can replace the node }
  702. hpp:=@hp;
  703. end;
  704. if (hp.nodetype=calln) and
  705. { a procvar can't have parameters! }
  706. not assigned(tcallnode(hp).left) then
  707. begin
  708. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  709. if assigned(currprocdef) then
  710. begin
  711. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  712. if (po_methodpointer in pv.procoptions) then
  713. tloadnode(hp2).set_mp(tnode(tcallnode(hp).methodpointer).getcopy);
  714. hp.destroy;
  715. { replace the old callnode with the new loadnode }
  716. hpp^:=hp2;
  717. end;
  718. end;
  719. end;
  720. end;
  721. { the following procedure handles the access to a property symbol }
  722. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode; getaddr: boolean);
  723. procedure symlist_to_node(var p1:tnode;pl:tsymlist);
  724. var
  725. plist : psymlistitem;
  726. begin
  727. plist:=pl.firstsym;
  728. while assigned(plist) do
  729. begin
  730. case plist^.sltype of
  731. sl_load :
  732. begin
  733. { p1 can already contain the loadnode of
  734. the class variable. When there is no tree yet we
  735. may need to load it for with or objects }
  736. if not assigned(p1) then
  737. begin
  738. case st.symtabletype of
  739. withsymtable :
  740. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  741. objectsymtable :
  742. p1:=load_self;
  743. end;
  744. end;
  745. if assigned(p1) then
  746. p1:=csubscriptnode.create(plist^.sym,p1)
  747. else
  748. p1:=cloadnode.create(plist^.sym,st);
  749. end;
  750. sl_subscript :
  751. p1:=csubscriptnode.create(plist^.sym,p1);
  752. sl_vec :
  753. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,s32bittype,true));
  754. else
  755. internalerror(200110205);
  756. end;
  757. plist:=plist^.next;
  758. end;
  759. include(p1.flags,nf_isproperty);
  760. end;
  761. var
  762. paras : tnode;
  763. p2 : tnode;
  764. membercall : boolean;
  765. begin
  766. paras:=nil;
  767. { property parameters? read them only if the property really }
  768. { has parameters }
  769. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  770. begin
  771. if token=_LECKKLAMMER then
  772. begin
  773. consume(_LECKKLAMMER);
  774. paras:=parse_paras(false,true);
  775. consume(_RECKKLAMMER);
  776. end;
  777. end;
  778. { indexed property }
  779. if (ppo_indexed in tpropertysym(sym).propoptions) then
  780. begin
  781. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  782. paras:=ccallparanode.create(p2,paras);
  783. end;
  784. { we need only a write property if a := follows }
  785. { if not(afterassignment) and not(in_args) then }
  786. if token=_ASSIGNMENT then
  787. begin
  788. { write property: }
  789. if not tpropertysym(sym).writeaccess.empty then
  790. begin
  791. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  792. procsym :
  793. begin
  794. { generate the method call }
  795. membercall:=maybe_load_methodpointer(st,p1);
  796. p1:=ccallnode.create(paras,
  797. tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1);
  798. if membercall then
  799. include(tcallnode(p1).flags,nf_member_call);
  800. paras:=nil;
  801. consume(_ASSIGNMENT);
  802. { read the expression }
  803. if tpropertysym(sym).proptype.def.deftype=procvardef then
  804. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  805. p2:=comp_expr(true);
  806. if assigned(getprocvardef) then
  807. handle_procvar(getprocvardef,p2);
  808. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  809. include(tcallnode(p1).flags,nf_isproperty);
  810. getprocvardef:=nil;
  811. end;
  812. varsym :
  813. begin
  814. { generate access code }
  815. symlist_to_node(p1,tpropertysym(sym).writeaccess);
  816. consume(_ASSIGNMENT);
  817. { read the expression }
  818. p2:=comp_expr(true);
  819. p1:=cassignmentnode.create(p1,p2);
  820. end
  821. else
  822. begin
  823. p1:=cerrornode.create;
  824. Message(parser_e_no_procedure_to_access_property);
  825. end;
  826. end;
  827. end
  828. else
  829. begin
  830. p1:=cerrornode.create;
  831. Message(parser_e_no_procedure_to_access_property);
  832. end;
  833. end
  834. else
  835. begin
  836. { read property: }
  837. if not tpropertysym(sym).readaccess.empty then
  838. begin
  839. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  840. varsym :
  841. begin
  842. { generate access code }
  843. symlist_to_node(p1,tpropertysym(sym).readaccess);
  844. end;
  845. procsym :
  846. begin
  847. { generate the method call }
  848. membercall:=maybe_load_methodpointer(st,p1);
  849. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1);
  850. if membercall then
  851. include(tcallnode(p1).flags,nf_member_call);
  852. paras:=nil;
  853. include(p1.flags,nf_isproperty);
  854. end
  855. else
  856. begin
  857. p1:=cerrornode.create;
  858. Message(type_e_mismatch);
  859. end;
  860. end;
  861. end
  862. else
  863. begin
  864. { error, no function to read property }
  865. p1:=cerrornode.create;
  866. Message(parser_e_no_procedure_to_access_property);
  867. end;
  868. end;
  869. { release paras if not used }
  870. if assigned(paras) then
  871. paras.free;
  872. end;
  873. { the ID token has to be consumed before calling this function }
  874. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callnflags:tnodeflags);
  875. var
  876. static_name : string;
  877. isclassref : boolean;
  878. srsymtable : tsymtable;
  879. begin
  880. if sym=nil then
  881. begin
  882. { pattern is still valid unless
  883. there is another ID just after the ID of sym }
  884. Message1(sym_e_id_no_member,pattern);
  885. p1.free;
  886. p1:=cerrornode.create;
  887. { try to clean up }
  888. again:=false;
  889. end
  890. else
  891. begin
  892. if assigned(p1) then
  893. begin
  894. if not assigned(p1.resulttype.def) then
  895. do_resulttypepass(p1);
  896. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  897. end
  898. else
  899. isclassref:=false;
  900. { we assume, that only procsyms and varsyms are in an object }
  901. { symbol table, for classes, properties are allowed }
  902. case sym.typ of
  903. procsym:
  904. begin
  905. do_proc_call(sym,sym.owner,
  906. (getaddr and not(token in [_CARET,_POINT])),
  907. again,p1);
  908. { add provided flags }
  909. if (p1.nodetype=calln) then
  910. p1.flags:=p1.flags+callnflags;
  911. { we need to know which procedure is called }
  912. do_resulttypepass(p1);
  913. { now we know the real method e.g. we can check for a class method }
  914. if isclassref and
  915. (p1.nodetype=calln) and
  916. assigned(tcallnode(p1).procdefinition) and
  917. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  918. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  919. Message(parser_e_only_class_methods_via_class_ref);
  920. end;
  921. varsym:
  922. begin
  923. if isclassref then
  924. Message(parser_e_only_class_methods_via_class_ref);
  925. if (sp_static in sym.symoptions) then
  926. begin
  927. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  928. searchsym(static_name,sym,srsymtable);
  929. check_hints(sym);
  930. p1.free;
  931. p1:=cloadnode.create(sym,srsymtable);
  932. end
  933. else
  934. p1:=csubscriptnode.create(sym,p1);
  935. end;
  936. propertysym:
  937. begin
  938. if isclassref then
  939. Message(parser_e_only_class_methods_via_class_ref);
  940. handle_propertysym(sym,sym.owner,p1,getaddr);
  941. end;
  942. else internalerror(16);
  943. end;
  944. end;
  945. end;
  946. {****************************************************************************
  947. Factor
  948. ****************************************************************************}
  949. {$ifdef fpc}
  950. {$maxfpuregisters 0}
  951. {$endif fpc}
  952. function factor(getaddr : boolean) : tnode;
  953. {---------------------------------------------
  954. Factor_read_id
  955. ---------------------------------------------}
  956. procedure factor_read_id(var p1:tnode;var again:boolean);
  957. var
  958. pc : pchar;
  959. len : longint;
  960. srsym : tsym;
  961. possible_error : boolean;
  962. srsymtable : tsymtable;
  963. storesymtablestack : tsymtable;
  964. htype : ttype;
  965. static_name : string;
  966. begin
  967. { allow post fix operators }
  968. again:=true;
  969. consume_sym(srsym,srsymtable);
  970. { Access to funcret or need to call the function? }
  971. if (srsym.typ in [absolutesym,varsym]) and
  972. (vo_is_funcret in tvarsym(srsym).varoptions) and
  973. (
  974. (token=_LKLAMMER) or
  975. (not(m_fpc in aktmodeswitches) and
  976. (afterassignment or in_args) and
  977. not(vo_is_result in tvarsym(srsym).varoptions))
  978. ) then
  979. begin
  980. storesymtablestack:=symtablestack;
  981. symtablestack:=srsym.owner.next;
  982. searchsym(srsym.name,srsym,srsymtable);
  983. if not assigned(srsym) then
  984. srsym:=generrorsym;
  985. if (srsym.typ<>procsym) then
  986. Message(cg_e_illegal_expression);
  987. symtablestack:=storesymtablestack;
  988. end;
  989. begin
  990. { check semantics of private }
  991. if (srsym.typ in [propertysym,procsym,varsym]) and
  992. (srsym.owner.symtabletype=objectsymtable) then
  993. begin
  994. if (sp_private in srsym.symoptions) and
  995. (tobjectdef(srsym.owner.defowner).owner.symtabletype=globalsymtable) and
  996. (tobjectdef(srsym.owner.defowner).owner.unitid<>0) then
  997. Message(parser_e_cant_access_private_member);
  998. end;
  999. case srsym.typ of
  1000. absolutesym :
  1001. begin
  1002. p1:=cloadnode.create(srsym,srsymtable);
  1003. end;
  1004. varsym :
  1005. begin
  1006. { are we in a class method, we check here the
  1007. srsymtable, because a field in another object
  1008. also has objectsymtable. And withsymtable is
  1009. not possible for self in class methods (PFV) }
  1010. if (srsymtable.symtabletype=objectsymtable) and
  1011. assigned(current_procdef) and
  1012. (po_classmethod in current_procdef.procoptions) then
  1013. Message(parser_e_only_class_methods);
  1014. if (sp_static in srsym.symoptions) then
  1015. begin
  1016. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1017. searchsym(static_name,srsym,srsymtable);
  1018. check_hints(srsym);
  1019. end;
  1020. case srsymtable.symtabletype of
  1021. objectsymtable :
  1022. p1:=csubscriptnode.create(srsym,load_self);
  1023. withsymtable :
  1024. p1:=csubscriptnode.create(srsym,tnode(twithsymtable(srsymtable).withrefnode).getcopy);
  1025. else
  1026. p1:=cloadnode.create(srsym,srsymtable);
  1027. end;
  1028. if tvarsym(srsym).varstate=vs_declared then
  1029. begin
  1030. include(p1.flags,nf_first_use);
  1031. { set special between first loaded until checked in resulttypepass }
  1032. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  1033. end;
  1034. end;
  1035. typedconstsym :
  1036. begin
  1037. p1:=cloadnode.create(srsym,srsymtable);
  1038. end;
  1039. syssym :
  1040. begin
  1041. p1:=statement_syssym(tsyssym(srsym).number);
  1042. end;
  1043. typesym :
  1044. begin
  1045. htype.setsym(srsym);
  1046. if not assigned(htype.def) then
  1047. begin
  1048. again:=false;
  1049. end
  1050. else
  1051. begin
  1052. if token=_LKLAMMER then
  1053. begin
  1054. consume(_LKLAMMER);
  1055. p1:=comp_expr(true);
  1056. consume(_RKLAMMER);
  1057. p1:=ctypeconvnode.create_explicit(p1,htype);
  1058. end
  1059. else { not LKLAMMER }
  1060. if (token=_POINT) and
  1061. is_object(htype.def) then
  1062. begin
  1063. consume(_POINT);
  1064. if assigned(current_procdef) and
  1065. assigned(current_procdef._class) and
  1066. not(getaddr) then
  1067. begin
  1068. if current_procdef._class.is_related(tobjectdef(htype.def)) then
  1069. begin
  1070. p1:=ctypenode.create(htype);
  1071. { search also in inherited methods }
  1072. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1073. check_hints(srsym);
  1074. consume(_ID);
  1075. do_member_read(false,srsym,p1,again,[]);
  1076. end
  1077. else
  1078. begin
  1079. Message(parser_e_no_super_class);
  1080. again:=false;
  1081. end;
  1082. end
  1083. else
  1084. begin
  1085. { allows @TObject.Load }
  1086. { also allows static methods and variables }
  1087. p1:=ctypenode.create(htype);
  1088. { TP allows also @TMenu.Load if Load is only }
  1089. { defined in an anchestor class }
  1090. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1091. check_hints(srsym);
  1092. if not assigned(srsym) then
  1093. Message1(sym_e_id_no_member,pattern)
  1094. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1095. Message(sym_e_only_static_in_static)
  1096. else
  1097. begin
  1098. consume(_ID);
  1099. do_member_read(getaddr,srsym,p1,again,[]);
  1100. end;
  1101. end;
  1102. end
  1103. else
  1104. begin
  1105. { class reference ? }
  1106. if is_class(htype.def) then
  1107. begin
  1108. if getaddr and (token=_POINT) then
  1109. begin
  1110. consume(_POINT);
  1111. { allows @Object.Method }
  1112. { also allows static methods and variables }
  1113. p1:=ctypenode.create(htype);
  1114. { TP allows also @TMenu.Load if Load is only }
  1115. { defined in an anchestor class }
  1116. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1117. check_hints(srsym);
  1118. if not assigned(srsym) then
  1119. Message1(sym_e_id_no_member,pattern)
  1120. else
  1121. begin
  1122. consume(_ID);
  1123. do_member_read(getaddr,srsym,p1,again,[]);
  1124. end;
  1125. end
  1126. else
  1127. begin
  1128. p1:=ctypenode.create(htype);
  1129. { For a type block we simply return only
  1130. the type. For all other blocks we return
  1131. a loadvmt node }
  1132. if (block_type<>bt_type) then
  1133. p1:=cloadvmtaddrnode.create(p1);
  1134. end;
  1135. end
  1136. else
  1137. p1:=ctypenode.create(htype);
  1138. end;
  1139. end;
  1140. end;
  1141. enumsym :
  1142. begin
  1143. p1:=genenumnode(tenumsym(srsym));
  1144. end;
  1145. constsym :
  1146. begin
  1147. case tconstsym(srsym).consttyp of
  1148. constint :
  1149. begin
  1150. { do a very dirty trick to bootstrap this code }
  1151. if (tconstsym(srsym).value.valueord>=-(int64(2147483647)+int64(1))) and
  1152. (tconstsym(srsym).value.valueord<=2147483647) then
  1153. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,s32bittype,true)
  1154. else if (tconstsym(srsym).value.valueord > maxlongint) and
  1155. (tconstsym(srsym).value.valueord <= int64(maxlongint)+int64(maxlongint)+1) then
  1156. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,u32bittype,true)
  1157. else
  1158. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cs64bittype,true);
  1159. end;
  1160. conststring :
  1161. begin
  1162. len:=tconstsym(srsym).value.len;
  1163. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1164. len:=255;
  1165. getmem(pc,len+1);
  1166. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1167. pc[len]:=#0;
  1168. p1:=cstringconstnode.createpchar(pc,len);
  1169. end;
  1170. constchar :
  1171. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cchartype,true);
  1172. constreal :
  1173. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1174. constbool :
  1175. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,booltype,true);
  1176. constset :
  1177. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1178. constord :
  1179. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1180. constpointer :
  1181. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1182. constnil :
  1183. p1:=cnilnode.create;
  1184. constresourcestring:
  1185. begin
  1186. p1:=cloadnode.create(srsym,srsymtable);
  1187. do_resulttypepass(p1);
  1188. p1.resulttype:=cansistringtype;
  1189. end;
  1190. constguid :
  1191. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1192. end;
  1193. end;
  1194. procsym :
  1195. begin
  1196. { are we in a class method ? }
  1197. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1198. not(is_interface(tdef(srsym.owner.defowner))) and
  1199. assigned(current_procdef) and
  1200. (po_classmethod in current_procdef.procoptions);
  1201. do_proc_call(srsym,srsymtable,
  1202. (getaddr and not(token in [_CARET,_POINT])),
  1203. again,p1);
  1204. { we need to know which procedure is called }
  1205. if possible_error then
  1206. begin
  1207. do_resulttypepass(p1);
  1208. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1209. Message(parser_e_only_class_methods);
  1210. end;
  1211. end;
  1212. propertysym :
  1213. begin
  1214. { access to property in a method }
  1215. { are we in a class method ? }
  1216. if (srsym.owner.symtabletype=objectsymtable) and
  1217. assigned(current_procdef) and
  1218. (po_classmethod in current_procdef.procoptions) then
  1219. Message(parser_e_only_class_methods);
  1220. { no method pointer }
  1221. p1:=nil;
  1222. handle_propertysym(srsym,srsymtable,p1,getaddr);
  1223. end;
  1224. labelsym :
  1225. begin
  1226. consume(_COLON);
  1227. if tlabelsym(srsym).defined then
  1228. Message(sym_e_label_already_defined);
  1229. tlabelsym(srsym).defined:=true;
  1230. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1231. end;
  1232. errorsym :
  1233. begin
  1234. p1:=cerrornode.create;
  1235. if token=_LKLAMMER then
  1236. begin
  1237. consume(_LKLAMMER);
  1238. parse_paras(false,false);
  1239. consume(_RKLAMMER);
  1240. end;
  1241. end;
  1242. else
  1243. begin
  1244. p1:=cerrornode.create;
  1245. Message(cg_e_illegal_expression);
  1246. end;
  1247. end; { end case }
  1248. end;
  1249. end;
  1250. {---------------------------------------------
  1251. Factor_Read_Set
  1252. ---------------------------------------------}
  1253. { Read a set between [] }
  1254. function factor_read_set:tnode;
  1255. var
  1256. p1,p2 : tnode;
  1257. lastp,
  1258. buildp : tarrayconstructornode;
  1259. begin
  1260. buildp:=nil;
  1261. { be sure that a least one arrayconstructn is used, also for an
  1262. empty [] }
  1263. if token=_RECKKLAMMER then
  1264. buildp:=carrayconstructornode.create(nil,buildp)
  1265. else
  1266. begin
  1267. while true do
  1268. begin
  1269. p1:=comp_expr(true);
  1270. if token=_POINTPOINT then
  1271. begin
  1272. consume(_POINTPOINT);
  1273. p2:=comp_expr(true);
  1274. p1:=carrayconstructorrangenode.create(p1,p2);
  1275. end;
  1276. { insert at the end of the tree, to get the correct order }
  1277. if not assigned(buildp) then
  1278. begin
  1279. buildp:=carrayconstructornode.create(p1,nil);
  1280. lastp:=buildp;
  1281. end
  1282. else
  1283. begin
  1284. lastp.right:=carrayconstructornode.create(p1,nil);
  1285. lastp:=tarrayconstructornode(lastp.right);
  1286. end;
  1287. { there could be more elements }
  1288. if token=_COMMA then
  1289. consume(_COMMA)
  1290. else
  1291. break;
  1292. end;
  1293. end;
  1294. factor_read_set:=buildp;
  1295. end;
  1296. {---------------------------------------------
  1297. PostFixOperators
  1298. ---------------------------------------------}
  1299. procedure postfixoperators(var p1:tnode;var again:boolean);
  1300. { tries to avoid syntax errors after invalid qualifiers }
  1301. procedure recoverconsume_postfixops;
  1302. begin
  1303. while true do
  1304. begin
  1305. case token of
  1306. _CARET:
  1307. consume(_CARET);
  1308. _POINT:
  1309. begin
  1310. consume(_POINT);
  1311. if token=_ID then
  1312. consume(_ID);
  1313. end;
  1314. _LECKKLAMMER:
  1315. begin
  1316. consume(_LECKKLAMMER);
  1317. repeat
  1318. comp_expr(true);
  1319. if token=_COMMA then
  1320. consume(_COMMA)
  1321. else
  1322. break;
  1323. until false;
  1324. consume(_RECKKLAMMER);
  1325. end
  1326. else
  1327. break;
  1328. end;
  1329. end;
  1330. end;
  1331. var
  1332. store_static : boolean;
  1333. protsym : tpropertysym;
  1334. p2,p3 : tnode;
  1335. hsym : tsym;
  1336. classh : tobjectdef;
  1337. begin
  1338. again:=true;
  1339. while again do
  1340. begin
  1341. { we need the resulttype }
  1342. do_resulttypepass(p1);
  1343. if codegenerror then
  1344. begin
  1345. recoverconsume_postfixops;
  1346. exit;
  1347. end;
  1348. { handle token }
  1349. case token of
  1350. _CARET:
  1351. begin
  1352. consume(_CARET);
  1353. if (p1.resulttype.def.deftype<>pointerdef) then
  1354. begin
  1355. { ^ as binary operator is a problem!!!! (FK) }
  1356. again:=false;
  1357. Message(cg_e_invalid_qualifier);
  1358. recoverconsume_postfixops;
  1359. p1.destroy;
  1360. p1:=cerrornode.create;
  1361. end
  1362. else
  1363. begin
  1364. p1:=cderefnode.create(p1);
  1365. end;
  1366. end;
  1367. _LECKKLAMMER:
  1368. begin
  1369. if is_class_or_interface(p1.resulttype.def) then
  1370. begin
  1371. { default property }
  1372. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1373. if not(assigned(protsym)) then
  1374. begin
  1375. p1.destroy;
  1376. p1:=cerrornode.create;
  1377. again:=false;
  1378. message(parser_e_no_default_property_available);
  1379. end
  1380. else
  1381. begin
  1382. { The property symbol is referenced indirect }
  1383. inc(protsym.refs);
  1384. handle_propertysym(protsym,protsym.owner,p1,getaddr);
  1385. end;
  1386. end
  1387. else
  1388. begin
  1389. consume(_LECKKLAMMER);
  1390. repeat
  1391. case p1.resulttype.def.deftype of
  1392. pointerdef:
  1393. begin
  1394. { support delphi autoderef }
  1395. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1396. (m_autoderef in aktmodeswitches) then
  1397. begin
  1398. p1:=cderefnode.create(p1);
  1399. end;
  1400. p2:=comp_expr(true);
  1401. p1:=cvecnode.create(p1,p2);
  1402. end;
  1403. stringdef :
  1404. begin
  1405. p2:=comp_expr(true);
  1406. p1:=cvecnode.create(p1,p2);
  1407. end;
  1408. arraydef :
  1409. begin
  1410. p2:=comp_expr(true);
  1411. { support SEG:OFS for go32v2 Mem[] }
  1412. if (target_info.system=system_i386_go32v2) and
  1413. (p1.nodetype=loadn) and
  1414. assigned(tloadnode(p1).symtableentry) and
  1415. assigned(tloadnode(p1).symtableentry.owner.name) and
  1416. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1417. ((tloadnode(p1).symtableentry.name='MEM') or
  1418. (tloadnode(p1).symtableentry.name='MEMW') or
  1419. (tloadnode(p1).symtableentry.name='MEML')) then
  1420. begin
  1421. if (token=_COLON) then
  1422. begin
  1423. consume(_COLON);
  1424. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype,false),p2);
  1425. p2:=comp_expr(true);
  1426. p2:=caddnode.create(addn,p2,p3);
  1427. p1:=cvecnode.create(p1,p2);
  1428. include(tvecnode(p1).flags,nf_memseg);
  1429. include(tvecnode(p1).flags,nf_memindex);
  1430. end
  1431. else
  1432. begin
  1433. p1:=cvecnode.create(p1,p2);
  1434. include(tvecnode(p1).flags,nf_memindex);
  1435. end;
  1436. end
  1437. else
  1438. p1:=cvecnode.create(p1,p2);
  1439. end;
  1440. else
  1441. begin
  1442. Message(cg_e_invalid_qualifier);
  1443. p1.destroy;
  1444. p1:=cerrornode.create;
  1445. comp_expr(true);
  1446. again:=false;
  1447. end;
  1448. end;
  1449. do_resulttypepass(p1);
  1450. if token=_COMMA then
  1451. consume(_COMMA)
  1452. else
  1453. break;
  1454. until false;
  1455. consume(_RECKKLAMMER);
  1456. end;
  1457. end;
  1458. _POINT :
  1459. begin
  1460. consume(_POINT);
  1461. if (p1.resulttype.def.deftype=pointerdef) and
  1462. (m_autoderef in aktmodeswitches) then
  1463. begin
  1464. p1:=cderefnode.create(p1);
  1465. do_resulttypepass(p1);
  1466. end;
  1467. case p1.resulttype.def.deftype of
  1468. recorddef:
  1469. begin
  1470. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1471. check_hints(hsym);
  1472. if assigned(hsym) and
  1473. (hsym.typ=varsym) then
  1474. p1:=csubscriptnode.create(hsym,p1)
  1475. else
  1476. begin
  1477. Message1(sym_e_illegal_field,pattern);
  1478. p1.destroy;
  1479. p1:=cerrornode.create;
  1480. end;
  1481. consume(_ID);
  1482. end;
  1483. variantdef:
  1484. begin
  1485. end;
  1486. classrefdef:
  1487. begin
  1488. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1489. hsym:=searchsym_in_class(classh,pattern);
  1490. check_hints(hsym);
  1491. if hsym=nil then
  1492. begin
  1493. Message1(sym_e_id_no_member,pattern);
  1494. p1.destroy;
  1495. p1:=cerrornode.create;
  1496. { try to clean up }
  1497. consume(_ID);
  1498. end
  1499. else
  1500. begin
  1501. consume(_ID);
  1502. do_member_read(getaddr,hsym,p1,again,[]);
  1503. end;
  1504. end;
  1505. objectdef:
  1506. begin
  1507. store_static:=allow_only_static;
  1508. allow_only_static:=false;
  1509. classh:=tobjectdef(p1.resulttype.def);
  1510. hsym:=searchsym_in_class(classh,pattern);
  1511. check_hints(hsym);
  1512. allow_only_static:=store_static;
  1513. if hsym=nil then
  1514. begin
  1515. Message1(sym_e_id_no_member,pattern);
  1516. p1.destroy;
  1517. p1:=cerrornode.create;
  1518. { try to clean up }
  1519. consume(_ID);
  1520. end
  1521. else
  1522. begin
  1523. consume(_ID);
  1524. do_member_read(getaddr,hsym,p1,again,[]);
  1525. end;
  1526. end;
  1527. pointerdef:
  1528. begin
  1529. Message(cg_e_invalid_qualifier);
  1530. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1531. Message(parser_h_maybe_deref_caret_missing);
  1532. end;
  1533. else
  1534. begin
  1535. Message(cg_e_invalid_qualifier);
  1536. p1.destroy;
  1537. p1:=cerrornode.create;
  1538. consume(_ID);
  1539. end;
  1540. end;
  1541. end;
  1542. else
  1543. begin
  1544. { is this a procedure variable ? }
  1545. if assigned(p1.resulttype.def) then
  1546. begin
  1547. if (p1.resulttype.def.deftype=procvardef) then
  1548. begin
  1549. if assigned(getprocvardef) and
  1550. equal_defs(p1.resulttype.def,getprocvardef) then
  1551. again:=false
  1552. else
  1553. if (token=_LKLAMMER) or
  1554. ((tprocvardef(p1.resulttype.def).maxparacount=0) and
  1555. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1556. (not afterassignment) and
  1557. (not in_args)) then
  1558. begin
  1559. if try_to_consume(_LKLAMMER) then
  1560. begin
  1561. p2:=parse_paras(false,false);
  1562. consume(_RKLAMMER);
  1563. end
  1564. else
  1565. p2:=nil;
  1566. p1:=ccallnode.create_procvar(p2,p1);
  1567. { proc():= is never possible }
  1568. if token=_ASSIGNMENT then
  1569. begin
  1570. Message(cg_e_illegal_expression);
  1571. p1.free;
  1572. p1:=cerrornode.create;
  1573. again:=false;
  1574. end;
  1575. end
  1576. else
  1577. again:=false;
  1578. end
  1579. else
  1580. again:=false;
  1581. end
  1582. else
  1583. again:=false;
  1584. end;
  1585. end;
  1586. end; { while again }
  1587. end;
  1588. {---------------------------------------------
  1589. Factor (Main)
  1590. ---------------------------------------------}
  1591. var
  1592. l : longint;
  1593. card : cardinal;
  1594. ic : TConstExprInt;
  1595. oldp1,
  1596. p1 : tnode;
  1597. code : integer;
  1598. again : boolean;
  1599. sym : tsym;
  1600. pd : tprocdef;
  1601. classh : tobjectdef;
  1602. d : bestreal;
  1603. hs : string;
  1604. htype : ttype;
  1605. filepos : tfileposinfo;
  1606. {---------------------------------------------
  1607. Helpers
  1608. ---------------------------------------------}
  1609. procedure check_tokenpos;
  1610. begin
  1611. if (p1<>oldp1) then
  1612. begin
  1613. if assigned(p1) then
  1614. p1.set_tree_filepos(filepos);
  1615. oldp1:=p1;
  1616. filepos:=akttokenpos;
  1617. end;
  1618. end;
  1619. begin
  1620. oldp1:=nil;
  1621. p1:=nil;
  1622. filepos:=akttokenpos;
  1623. again:=false;
  1624. if token=_ID then
  1625. begin
  1626. factor_read_id(p1,again);
  1627. if again then
  1628. begin
  1629. check_tokenpos;
  1630. { handle post fix operators }
  1631. postfixoperators(p1,again);
  1632. end;
  1633. end
  1634. else
  1635. case token of
  1636. _SELF :
  1637. begin
  1638. again:=true;
  1639. consume(_SELF);
  1640. if not(assigned(current_procdef) and
  1641. assigned(current_procdef._class)) then
  1642. begin
  1643. p1:=cerrornode.create;
  1644. again:=false;
  1645. Message(parser_e_self_not_in_method);
  1646. end
  1647. else
  1648. begin
  1649. p1:=load_self;
  1650. postfixoperators(p1,again);
  1651. end;
  1652. end;
  1653. _INHERITED :
  1654. begin
  1655. again:=true;
  1656. consume(_INHERITED);
  1657. if assigned(current_procdef._class) then
  1658. begin
  1659. classh:=current_procdef._class.childof;
  1660. { if inherited; only then we need the method with
  1661. the same name }
  1662. if token in endtokens then
  1663. begin
  1664. hs:=current_procdef.procsym.name;
  1665. anon_inherited:=true;
  1666. { For message methods we need to search using the message
  1667. number or string }
  1668. pd:=tprocsym(current_procdef.procsym).first_procdef;
  1669. if (po_msgint in pd.procoptions) then
  1670. sym:=searchsym_in_class_by_msgint(classh,pd.messageinf.i)
  1671. else
  1672. if (po_msgstr in pd.procoptions) then
  1673. sym:=searchsym_in_class_by_msgstr(classh,pd.messageinf.str)
  1674. else
  1675. sym:=searchsym_in_class(classh,hs);
  1676. end
  1677. else
  1678. begin
  1679. hs:=pattern;
  1680. consume(_ID);
  1681. anon_inherited:=false;
  1682. sym:=searchsym_in_class(classh,hs);
  1683. end;
  1684. if assigned(sym) then
  1685. begin
  1686. check_hints(sym);
  1687. { load the procdef from the inherited class and
  1688. not from self }
  1689. if sym.typ=procsym then
  1690. begin
  1691. htype.setdef(classh);
  1692. p1:=ctypenode.create(htype);
  1693. end;
  1694. do_member_read(false,sym,p1,again,[nf_inherited,nf_anon_inherited]);
  1695. end
  1696. else
  1697. begin
  1698. if anon_inherited then
  1699. begin
  1700. { we didn't find a member in the parents call the
  1701. DefaultHandler }
  1702. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1703. if not assigned(sym) or
  1704. (sym.typ<>procsym) then
  1705. internalerror(200303171);
  1706. p1:=nil;
  1707. do_proc_call(sym,sym.owner,false,again,p1);
  1708. end
  1709. else
  1710. begin
  1711. Message1(sym_e_id_no_member,hs);
  1712. p1:=cerrornode.create;
  1713. end;
  1714. again:=false;
  1715. end;
  1716. { turn auto inheriting off }
  1717. anon_inherited:=false;
  1718. end
  1719. else
  1720. begin
  1721. Message(parser_e_generic_methods_only_in_methods);
  1722. again:=false;
  1723. p1:=cerrornode.create;
  1724. end;
  1725. postfixoperators(p1,again);
  1726. end;
  1727. _INTCONST :
  1728. begin
  1729. { try cardinal first }
  1730. val(pattern,card,code);
  1731. if code<>0 then
  1732. begin
  1733. { then longint }
  1734. valint(pattern,l,code);
  1735. if code <> 0 then
  1736. begin
  1737. { then int64 }
  1738. val(pattern,ic,code);
  1739. if code<>0 then
  1740. begin
  1741. {finally float }
  1742. val(pattern,d,code);
  1743. if code<>0 then
  1744. begin
  1745. Message(cg_e_invalid_integer);
  1746. consume(_INTCONST);
  1747. l:=1;
  1748. p1:=cordconstnode.create(l,s32bittype,true);
  1749. end
  1750. else
  1751. begin
  1752. consume(_INTCONST);
  1753. p1:=crealconstnode.create(d,pbestrealtype^);
  1754. end;
  1755. end
  1756. else
  1757. begin
  1758. consume(_INTCONST);
  1759. p1:=cordconstnode.create(ic,cs64bittype,true);
  1760. end
  1761. end
  1762. else
  1763. begin
  1764. consume(_INTCONST);
  1765. p1:=cordconstnode.create(l,defaultordconsttype,true)
  1766. end
  1767. end
  1768. else
  1769. begin
  1770. consume(_INTCONST);
  1771. { check whether the value isn't in the longint range as well }
  1772. { (longint is easier to perform calculations with) (JM) }
  1773. if card <= $7fffffff then
  1774. { no sign extension necessary, so not longint typecast (JM) }
  1775. p1:=cordconstnode.create(card,s32bittype,true)
  1776. else
  1777. p1:=cordconstnode.create(card,u32bittype,true)
  1778. end;
  1779. end;
  1780. _REALNUMBER :
  1781. begin
  1782. val(pattern,d,code);
  1783. if code<>0 then
  1784. begin
  1785. Message(parser_e_error_in_real);
  1786. d:=1.0;
  1787. end;
  1788. consume(_REALNUMBER);
  1789. p1:=crealconstnode.create(d,pbestrealtype^);
  1790. end;
  1791. _STRING :
  1792. begin
  1793. string_dec(htype);
  1794. { STRING can be also a type cast }
  1795. if token=_LKLAMMER then
  1796. begin
  1797. consume(_LKLAMMER);
  1798. p1:=comp_expr(true);
  1799. consume(_RKLAMMER);
  1800. p1:=ctypeconvnode.create_explicit(p1,htype);
  1801. { handle postfix operators here e.g. string(a)[10] }
  1802. again:=true;
  1803. postfixoperators(p1,again);
  1804. end
  1805. else
  1806. p1:=ctypenode.create(htype);
  1807. end;
  1808. _FILE :
  1809. begin
  1810. htype:=cfiletype;
  1811. consume(_FILE);
  1812. { FILE can be also a type cast }
  1813. if token=_LKLAMMER then
  1814. begin
  1815. consume(_LKLAMMER);
  1816. p1:=comp_expr(true);
  1817. consume(_RKLAMMER);
  1818. p1:=ctypeconvnode.create_explicit(p1,htype);
  1819. { handle postfix operators here e.g. string(a)[10] }
  1820. again:=true;
  1821. postfixoperators(p1,again);
  1822. end
  1823. else
  1824. begin
  1825. p1:=ctypenode.create(htype);
  1826. end;
  1827. end;
  1828. _CSTRING :
  1829. begin
  1830. p1:=cstringconstnode.createstr(pattern,st_default);
  1831. consume(_CSTRING);
  1832. end;
  1833. _CCHAR :
  1834. begin
  1835. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  1836. consume(_CCHAR);
  1837. end;
  1838. _CWSTRING:
  1839. begin
  1840. p1:=cstringconstnode.createwstr(patternw);
  1841. consume(_CWSTRING);
  1842. end;
  1843. _CWCHAR:
  1844. begin
  1845. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  1846. consume(_CWCHAR);
  1847. end;
  1848. _KLAMMERAFFE :
  1849. begin
  1850. consume(_KLAMMERAFFE);
  1851. got_addrn:=true;
  1852. { support both @<x> and @(<x>) }
  1853. if try_to_consume(_LKLAMMER) then
  1854. begin
  1855. p1:=factor(true);
  1856. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1857. begin
  1858. again:=true;
  1859. postfixoperators(p1,again);
  1860. end;
  1861. consume(_RKLAMMER);
  1862. end
  1863. else
  1864. p1:=factor(true);
  1865. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1866. begin
  1867. again:=true;
  1868. postfixoperators(p1,again);
  1869. end;
  1870. got_addrn:=false;
  1871. p1:=caddrnode.create(p1);
  1872. { Store the procvar that we are expecting, the
  1873. addrn will use the information to find the correct
  1874. procdef or it will return an error }
  1875. if assigned(getprocvardef) and
  1876. (taddrnode(p1).left.nodetype = loadn) then
  1877. taddrnode(p1).getprocvardef:=getprocvardef;
  1878. end;
  1879. _LKLAMMER :
  1880. begin
  1881. consume(_LKLAMMER);
  1882. p1:=comp_expr(true);
  1883. consume(_RKLAMMER);
  1884. { it's not a good solution }
  1885. { but (a+b)^ makes some problems }
  1886. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1887. begin
  1888. again:=true;
  1889. postfixoperators(p1,again);
  1890. end;
  1891. end;
  1892. _LECKKLAMMER :
  1893. begin
  1894. consume(_LECKKLAMMER);
  1895. p1:=factor_read_set;
  1896. consume(_RECKKLAMMER);
  1897. end;
  1898. _PLUS :
  1899. begin
  1900. consume(_PLUS);
  1901. p1:=factor(false);
  1902. end;
  1903. _MINUS :
  1904. begin
  1905. consume(_MINUS);
  1906. p1:=sub_expr(oppower,false);
  1907. p1:=cunaryminusnode.create(p1);
  1908. end;
  1909. _OP_NOT :
  1910. begin
  1911. consume(_OP_NOT);
  1912. p1:=factor(false);
  1913. p1:=cnotnode.create(p1);
  1914. end;
  1915. _TRUE :
  1916. begin
  1917. consume(_TRUE);
  1918. p1:=cordconstnode.create(1,booltype,false);
  1919. end;
  1920. _FALSE :
  1921. begin
  1922. consume(_FALSE);
  1923. p1:=cordconstnode.create(0,booltype,false);
  1924. end;
  1925. _NIL :
  1926. begin
  1927. consume(_NIL);
  1928. p1:=cnilnode.create;
  1929. { It's really ugly code nil^, but delphi allows it }
  1930. if token in [_CARET] then
  1931. begin
  1932. again:=true;
  1933. postfixoperators(p1,again);
  1934. end;
  1935. end;
  1936. else
  1937. begin
  1938. p1:=cerrornode.create;
  1939. consume(token);
  1940. Message(cg_e_illegal_expression);
  1941. end;
  1942. end;
  1943. { generate error node if no node is created }
  1944. if not assigned(p1) then
  1945. begin
  1946. {$ifdef EXTDEBUG}
  1947. Comment(V_Warning,'factor: p1=nil');
  1948. {$endif}
  1949. p1:=cerrornode.create;
  1950. end;
  1951. { get the resulttype for the node }
  1952. if (not assigned(p1.resulttype.def)) then
  1953. do_resulttypepass(p1);
  1954. { tp7 procvar handling, but not if the next token
  1955. will be a := }
  1956. check_tp_procvar(p1);
  1957. factor:=p1;
  1958. check_tokenpos;
  1959. end;
  1960. {$ifdef fpc}
  1961. {$maxfpuregisters default}
  1962. {$endif fpc}
  1963. {****************************************************************************
  1964. Sub_Expr
  1965. ****************************************************************************}
  1966. const
  1967. { Warning these stay be ordered !! }
  1968. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1969. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1970. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1971. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1972. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1973. [_STARSTAR] );
  1974. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1975. {Reads a subexpression while the operators are of the current precedence
  1976. level, or any higher level. Replaces the old term, simpl_expr and
  1977. simpl2_expr.}
  1978. var
  1979. p1,p2 : tnode;
  1980. oldt : Ttoken;
  1981. filepos : tfileposinfo;
  1982. begin
  1983. if pred_level=highest_precedence then
  1984. p1:=factor(false)
  1985. else
  1986. p1:=sub_expr(succ(pred_level),true);
  1987. repeat
  1988. if (token in operator_levels[pred_level]) and
  1989. ((token<>_EQUAL) or accept_equal) then
  1990. begin
  1991. oldt:=token;
  1992. filepos:=akttokenpos;
  1993. consume(token);
  1994. if pred_level=highest_precedence then
  1995. p2:=factor(false)
  1996. else
  1997. p2:=sub_expr(succ(pred_level),true);
  1998. case oldt of
  1999. _PLUS :
  2000. p1:=caddnode.create(addn,p1,p2);
  2001. _MINUS :
  2002. p1:=caddnode.create(subn,p1,p2);
  2003. _STAR :
  2004. p1:=caddnode.create(muln,p1,p2);
  2005. _SLASH :
  2006. p1:=caddnode.create(slashn,p1,p2);
  2007. _EQUAL :
  2008. p1:=caddnode.create(equaln,p1,p2);
  2009. _GT :
  2010. p1:=caddnode.create(gtn,p1,p2);
  2011. _LT :
  2012. p1:=caddnode.create(ltn,p1,p2);
  2013. _GTE :
  2014. p1:=caddnode.create(gten,p1,p2);
  2015. _LTE :
  2016. p1:=caddnode.create(lten,p1,p2);
  2017. _SYMDIF :
  2018. p1:=caddnode.create(symdifn,p1,p2);
  2019. _STARSTAR :
  2020. p1:=caddnode.create(starstarn,p1,p2);
  2021. _OP_AS :
  2022. p1:=casnode.create(p1,p2);
  2023. _OP_IN :
  2024. p1:=cinnode.create(p1,p2);
  2025. _OP_IS :
  2026. p1:=cisnode.create(p1,p2);
  2027. _OP_OR :
  2028. p1:=caddnode.create(orn,p1,p2);
  2029. _OP_AND :
  2030. p1:=caddnode.create(andn,p1,p2);
  2031. _OP_DIV :
  2032. p1:=cmoddivnode.create(divn,p1,p2);
  2033. _OP_NOT :
  2034. p1:=cnotnode.create(p1);
  2035. _OP_MOD :
  2036. p1:=cmoddivnode.create(modn,p1,p2);
  2037. _OP_SHL :
  2038. p1:=cshlshrnode.create(shln,p1,p2);
  2039. _OP_SHR :
  2040. p1:=cshlshrnode.create(shrn,p1,p2);
  2041. _OP_XOR :
  2042. p1:=caddnode.create(xorn,p1,p2);
  2043. _ASSIGNMENT :
  2044. p1:=cassignmentnode.create(p1,p2);
  2045. _CARET :
  2046. p1:=caddnode.create(caretn,p1,p2);
  2047. _UNEQUAL :
  2048. p1:=caddnode.create(unequaln,p1,p2);
  2049. end;
  2050. p1.set_tree_filepos(filepos);
  2051. end
  2052. else
  2053. break;
  2054. until false;
  2055. sub_expr:=p1;
  2056. end;
  2057. function comp_expr(accept_equal : boolean):tnode;
  2058. var
  2059. oldafterassignment : boolean;
  2060. p1 : tnode;
  2061. begin
  2062. oldafterassignment:=afterassignment;
  2063. afterassignment:=true;
  2064. p1:=sub_expr(opcompare,accept_equal);
  2065. { get the resulttype for this expression }
  2066. if not assigned(p1.resulttype.def) then
  2067. do_resulttypepass(p1);
  2068. afterassignment:=oldafterassignment;
  2069. comp_expr:=p1;
  2070. end;
  2071. function expr : tnode;
  2072. var
  2073. p1,p2 : tnode;
  2074. oldafterassignment : boolean;
  2075. oldp1 : tnode;
  2076. filepos : tfileposinfo;
  2077. begin
  2078. oldafterassignment:=afterassignment;
  2079. p1:=sub_expr(opcompare,true);
  2080. { get the resulttype for this expression }
  2081. if not assigned(p1.resulttype.def) then
  2082. do_resulttypepass(p1);
  2083. filepos:=akttokenpos;
  2084. check_tp_procvar(p1);
  2085. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2086. afterassignment:=true;
  2087. oldp1:=p1;
  2088. case token of
  2089. _POINTPOINT :
  2090. begin
  2091. consume(_POINTPOINT);
  2092. p2:=sub_expr(opcompare,true);
  2093. p1:=crangenode.create(p1,p2);
  2094. end;
  2095. _ASSIGNMENT :
  2096. begin
  2097. consume(_ASSIGNMENT);
  2098. if (p1.resulttype.def.deftype=procvardef) then
  2099. getprocvardef:=tprocvardef(p1.resulttype.def);
  2100. p2:=sub_expr(opcompare,true);
  2101. if assigned(getprocvardef) then
  2102. handle_procvar(getprocvardef,p2);
  2103. getprocvardef:=nil;
  2104. p1:=cassignmentnode.create(p1,p2);
  2105. end;
  2106. _PLUSASN :
  2107. begin
  2108. consume(_PLUSASN);
  2109. p2:=sub_expr(opcompare,true);
  2110. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2111. end;
  2112. _MINUSASN :
  2113. begin
  2114. consume(_MINUSASN);
  2115. p2:=sub_expr(opcompare,true);
  2116. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2117. end;
  2118. _STARASN :
  2119. begin
  2120. consume(_STARASN );
  2121. p2:=sub_expr(opcompare,true);
  2122. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2123. end;
  2124. _SLASHASN :
  2125. begin
  2126. consume(_SLASHASN );
  2127. p2:=sub_expr(opcompare,true);
  2128. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2129. end;
  2130. end;
  2131. { get the resulttype for this expression }
  2132. if not assigned(p1.resulttype.def) then
  2133. do_resulttypepass(p1);
  2134. afterassignment:=oldafterassignment;
  2135. if p1<>oldp1 then
  2136. p1.set_tree_filepos(filepos);
  2137. expr:=p1;
  2138. end;
  2139. {$ifdef int64funcresok}
  2140. function get_intconst:TConstExprInt;
  2141. {$else int64funcresok}
  2142. function get_intconst:longint;
  2143. {$endif int64funcresok}
  2144. {Reads an expression, tries to evalute it and check if it is an integer
  2145. constant. Then the constant is returned.}
  2146. var
  2147. p:tnode;
  2148. begin
  2149. p:=comp_expr(true);
  2150. if not codegenerror then
  2151. begin
  2152. if (p.nodetype<>ordconstn) or
  2153. not(is_integer(p.resulttype.def)) then
  2154. Message(cg_e_illegal_expression)
  2155. else
  2156. get_intconst:=tordconstnode(p).value;
  2157. end;
  2158. p.free;
  2159. end;
  2160. function get_stringconst:string;
  2161. {Reads an expression, tries to evaluate it and checks if it is a string
  2162. constant. Then the constant is returned.}
  2163. var
  2164. p:tnode;
  2165. begin
  2166. get_stringconst:='';
  2167. p:=comp_expr(true);
  2168. if p.nodetype<>stringconstn then
  2169. begin
  2170. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2171. get_stringconst:=char(tordconstnode(p).value)
  2172. else
  2173. Message(cg_e_illegal_expression);
  2174. end
  2175. else
  2176. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2177. p.free;
  2178. end;
  2179. end.
  2180. {
  2181. $Log$
  2182. Revision 1.116 2003-05-11 14:45:12 peter
  2183. * tloadnode does not support objectsymtable,withsymtable anymore
  2184. * withnode cleanup
  2185. * direct with rewritten to use temprefnode
  2186. Revision 1.115 2003/05/09 17:47:03 peter
  2187. * self moved to hidden parameter
  2188. * removed hdisposen,hnewn,selfn
  2189. Revision 1.114 2003/05/01 07:59:42 florian
  2190. * introduced defaultordconsttype to decribe the default size of ordinal constants
  2191. on 64 bit CPUs it's equal to cs64bitdef while on 32 bit CPUs it's equal to s32bitdef
  2192. + added defines CPU32 and CPU64 for 32 bit and 64 bit CPUs
  2193. * int64s/qwords are allowed as for loop counter on 64 bit CPUs
  2194. Revision 1.113 2003/04/27 11:21:33 peter
  2195. * aktprocdef renamed to current_procdef
  2196. * procinfo renamed to current_procinfo
  2197. * procinfo will now be stored in current_module so it can be
  2198. cleaned up properly
  2199. * gen_main_procsym changed to create_main_proc and release_main_proc
  2200. to also generate a tprocinfo structure
  2201. * fixed unit implicit initfinal
  2202. Revision 1.112 2003/04/27 07:29:50 peter
  2203. * current_procdef cleanup, current_procdef is now always nil when parsing
  2204. a new procdef declaration
  2205. * aktprocsym removed
  2206. * lexlevel removed, use symtable.symtablelevel instead
  2207. * implicit init/final code uses the normal genentry/genexit
  2208. * funcret state checking updated for new funcret handling
  2209. Revision 1.111 2003/04/26 00:33:07 peter
  2210. * vo_is_result flag added for the special RESULT symbol
  2211. Revision 1.110 2003/04/25 20:59:33 peter
  2212. * removed funcretn,funcretsym, function result is now in varsym
  2213. and aliases for result and function name are added using absolutesym
  2214. * vs_hidden parameter for funcret passed in parameter
  2215. * vs_hidden fixes
  2216. * writenode changed to printnode and released from extdebug
  2217. * -vp option added to generate a tree.log with the nodetree
  2218. * nicer printnode for statements, callnode
  2219. Revision 1.109 2003/04/23 10:13:55 peter
  2220. * firstaddr will check procvardef
  2221. Revision 1.108 2003/04/22 23:50:23 peter
  2222. * firstpass uses expectloc
  2223. * checks if there are differences between the expectloc and
  2224. location.loc from secondpass in EXTDEBUG
  2225. Revision 1.107 2003/04/11 15:49:01 peter
  2226. * default property also increased the reference count for the
  2227. property symbol
  2228. Revision 1.106 2003/04/11 14:50:08 peter
  2229. * fix tw2454
  2230. Revision 1.105 2003/03/27 17:44:13 peter
  2231. * fixed small mem leaks
  2232. Revision 1.104 2003/03/17 18:55:30 peter
  2233. * allow more tokens instead of only semicolon after inherited
  2234. Revision 1.103 2003/03/17 16:54:41 peter
  2235. * support DefaultHandler and anonymous inheritance fixed
  2236. for message methods
  2237. Revision 1.102 2003/01/30 21:46:57 peter
  2238. * self fixes for static methods (merged)
  2239. Revision 1.101 2003/01/16 22:12:22 peter
  2240. * Find the correct procvar to load when using @ in fpc mode
  2241. Revision 1.100 2003/01/15 01:44:32 peter
  2242. * merged methodpointer fixes from 1.0.x
  2243. Revision 1.98 2003/01/12 17:51:42 peter
  2244. * tp procvar handling fix for tb0448
  2245. Revision 1.97 2003/01/05 22:44:14 peter
  2246. * remove a lot of code to support typen in loadn-procsym
  2247. Revision 1.96 2002/12/11 22:40:36 peter
  2248. * assigned(procvar) fix for delphi mode, fixes tb0430
  2249. Revision 1.95 2002/11/30 11:12:48 carl
  2250. + checking for symbols used with hint directives is done mostly in pexpr
  2251. only now
  2252. Revision 1.94 2002/11/27 15:33:47 peter
  2253. * the never ending story of tp procvar hacks
  2254. Revision 1.93 2002/11/26 22:58:24 peter
  2255. * fix for tw2178. When a ^ or . follows a procsym then the procsym
  2256. needs to be called
  2257. Revision 1.92 2002/11/25 17:43:22 peter
  2258. * splitted defbase in defutil,symutil,defcmp
  2259. * merged isconvertable and is_equal into compare_defs(_ext)
  2260. * made operator search faster by walking the list only once
  2261. Revision 1.91 2002/11/22 22:48:10 carl
  2262. * memory optimization with tconstsym (1.5%)
  2263. Revision 1.90 2002/11/20 22:49:55 pierre
  2264. * commented check code tht was invalid in 1.1
  2265. Revision 1.89 2002/11/18 18:34:41 peter
  2266. * fix crash with EXTDEBUG code
  2267. Revision 1.88 2002/11/18 17:48:21 peter
  2268. * fix tw2209 (merged)
  2269. Revision 1.87 2002/11/18 17:31:58 peter
  2270. * pass proccalloption to ret_in_xxx and push_xxx functions
  2271. Revision 1.86 2002/10/05 00:48:57 peter
  2272. * support inherited; support for overload as it is handled by
  2273. delphi. This is only for delphi mode as it is working is
  2274. undocumented and hard to predict what is done
  2275. Revision 1.85 2002/10/04 21:13:59 peter
  2276. * ignore vecn,subscriptn when checking for a procvar loadn
  2277. Revision 1.84 2002/10/02 20:51:22 peter
  2278. * don't check interfaces for class methods
  2279. Revision 1.83 2002/10/02 18:20:52 peter
  2280. * Copy() is now internal syssym that calls compilerprocs
  2281. Revision 1.82 2002/09/30 07:00:48 florian
  2282. * fixes to common code to get the alpha compiler compiled applied
  2283. Revision 1.81 2002/09/16 19:06:14 peter
  2284. * allow ^ after nil
  2285. Revision 1.80 2002/09/07 15:25:07 peter
  2286. * old logs removed and tabs fixed
  2287. Revision 1.79 2002/09/07 12:16:03 carl
  2288. * second part bug report 1996 fix, testrange in cordconstnode
  2289. only called if option is set (also make parsing a tiny faster)
  2290. Revision 1.78 2002/09/03 16:26:27 daniel
  2291. * Make Tprocdef.defs protected
  2292. Revision 1.77 2002/08/18 20:06:24 peter
  2293. * inlining is now also allowed in interface
  2294. * renamed write/load to ppuwrite/ppuload
  2295. * tnode storing in ppu
  2296. * nld,ncon,nbas are already updated for storing in ppu
  2297. Revision 1.76 2002/08/17 09:23:39 florian
  2298. * first part of procinfo rewrite
  2299. Revision 1.75 2002/08/01 16:37:47 jonas
  2300. - removed some superfluous "in_paras := true" statements
  2301. Revision 1.74 2002/07/26 21:15:41 florian
  2302. * rewrote the system handling
  2303. Revision 1.73 2002/07/23 09:51:23 daniel
  2304. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2305. are worth comitting.
  2306. Revision 1.72 2002/07/20 11:57:55 florian
  2307. * types.pas renamed to defbase.pas because D6 contains a types
  2308. unit so this would conflicts if D6 programms are compiled
  2309. + Willamette/SSE2 instructions to assembler added
  2310. Revision 1.71 2002/07/16 15:34:20 florian
  2311. * exit is now a syssym instead of a keyword
  2312. Revision 1.70 2002/07/06 20:18:02 carl
  2313. * longstring declaration now gives parser error since its not supported!
  2314. Revision 1.69 2002/06/12 15:46:14 jonas
  2315. * fixed web bug 1995
  2316. Revision 1.68 2002/05/18 13:34:12 peter
  2317. * readded missing revisions
  2318. Revision 1.67 2002/05/16 19:46:43 carl
  2319. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2320. + try to fix temp allocation (still in ifdef)
  2321. + generic constructor calls
  2322. + start of tassembler / tmodulebase class cleanup
  2323. Revision 1.65 2002/05/12 16:53:09 peter
  2324. * moved entry and exitcode to ncgutil and cgobj
  2325. * foreach gets extra argument for passing local data to the
  2326. iterator function
  2327. * -CR checks also class typecasts at runtime by changing them
  2328. into as
  2329. * fixed compiler to cycle with the -CR option
  2330. * fixed stabs with elf writer, finally the global variables can
  2331. be watched
  2332. * removed a lot of routines from cga unit and replaced them by
  2333. calls to cgobj
  2334. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2335. u32bit then the other is typecasted also to u32bit without giving
  2336. a rangecheck warning/error.
  2337. * fixed pascal calling method with reversing also the high tree in
  2338. the parast, detected by tcalcst3 test
  2339. Revision 1.64 2002/04/23 19:16:34 peter
  2340. * add pinline unit that inserts compiler supported functions using
  2341. one or more statements
  2342. * moved finalize and setlength from ninl to pinline
  2343. Revision 1.63 2002/04/21 19:02:05 peter
  2344. * removed newn and disposen nodes, the code is now directly
  2345. inlined from pexpr
  2346. * -an option that will write the secondpass nodes to the .s file, this
  2347. requires EXTDEBUG define to actually write the info
  2348. * fixed various internal errors and crashes due recent code changes
  2349. Revision 1.62 2002/04/16 16:11:17 peter
  2350. * using inherited; without a parent having the same function
  2351. will do nothing like delphi
  2352. Revision 1.61 2002/04/07 13:31:36 carl
  2353. + change unit use
  2354. Revision 1.60 2002/04/01 20:57:13 jonas
  2355. * fixed web bug 1907
  2356. * fixed some other procvar related bugs (all related to accepting procvar
  2357. constructs with either too many or too little parameters)
  2358. (both merged, includes second typo fix of pexpr.pas)
  2359. Revision 1.59 2002/03/31 20:26:35 jonas
  2360. + a_loadfpu_* and a_loadmm_* methods in tcg
  2361. * register allocation is now handled by a class and is mostly processor
  2362. independent (+rgobj.pas and i386/rgcpu.pas)
  2363. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2364. * some small improvements and fixes to the optimizer
  2365. * some register allocation fixes
  2366. * some fpuvaroffset fixes in the unary minus node
  2367. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2368. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2369. also better optimizable)
  2370. * fixed and optimized register saving/restoring for new/dispose nodes
  2371. * LOC_FPU locations now also require their "register" field to be set to
  2372. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2373. - list field removed of the tnode class because it's not used currently
  2374. and can cause hard-to-find bugs
  2375. }