pexpr.pas 98 KB

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