pexpr.pas 105 KB

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