pexpr.pas 91 KB

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