pexpr.pas 103 KB

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