pdecsub.pas 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecsub;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,symconst,symtype,symdef,symsym;
  23. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40; { directive can not be used object declaration }
  31. pd_notobjintf= $80; { directive can not be used interface declaration }
  32. function is_proc_directive(tok:ttoken):boolean;
  33. procedure parameter_dec(aktprocdef:tabstractprocdef);
  34. procedure parse_proc_directives(var pdflags:word);
  35. procedure handle_calling_convention(sym:tprocsym;def:tabstractprocdef);
  36. procedure calc_parasymtable_addresses(def:tprocdef);
  37. procedure parse_proc_head(options:tproctypeoption);
  38. procedure parse_proc_dec;
  39. procedure parse_var_proc_directives(var sym : tsym);
  40. procedure parse_object_proc_directives(var sym : tprocsym);
  41. function proc_add_definition(aprocsym:tprocsym;var aprocdef : tprocdef) : boolean;
  42. implementation
  43. uses
  44. {$ifdef delphi}
  45. sysutils,
  46. {$else delphi}
  47. strings,
  48. {$endif delphi}
  49. { common }
  50. cutils,cclasses,
  51. { global }
  52. globtype,globals,verbose,
  53. systems,cpubase,
  54. { aasm }
  55. aasmbase,aasmtai,aasmcpu,
  56. { symtable }
  57. symbase,symtable,defutil,defcmp,paramgr,
  58. { pass 1 }
  59. node,htypechk,
  60. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  61. { parser }
  62. fmodule,scanner,
  63. pbase,pexpr,ptype,pdecl,
  64. { linking }
  65. import,gendef,
  66. { codegen }
  67. cpuinfo,cgbase
  68. ;
  69. procedure resetvaluepara(p:tnamedindexitem;arg:pointer);
  70. begin
  71. if tsym(p).typ=varsym then
  72. with tvarsym(p) do
  73. if copy(name,1,3)='val' then
  74. aktprocdef.parast.symsearch.rename(name,copy(name,4,length(name)));
  75. end;
  76. procedure checkparatype(p:tnamedindexitem;arg:pointer);
  77. var
  78. highname : string;
  79. begin
  80. if tsym(p).typ<>varsym then
  81. exit;
  82. with tvarsym(p) do
  83. begin
  84. if assigned(vartype.def) and
  85. (vartype.def.deftype=arraydef) and
  86. not is_special_array(vartype.def) then
  87. begin
  88. if (varspez<>vs_var) then
  89. Message(parser_h_c_arrays_are_references);
  90. varspez:=vs_var;
  91. end;
  92. if assigned(vartype.def) and
  93. is_array_of_const(vartype.def) then
  94. begin
  95. if assigned(indexnext) and
  96. (tsym(indexnext).typ=varsym) and
  97. (copy(tvarsym(indexnext).name,1,4)='high') then
  98. begin
  99. { removing it is to complicated,
  100. we just hide it PM }
  101. highname:='hidden'+copy(tvarsym(indexnext).name,5,high(name));
  102. owner.rename(tvarsym(indexnext).name,highname);
  103. if assigned(indexnext.indexnext) then
  104. Message(parser_e_C_array_of_const_must_be_last);
  105. end
  106. else
  107. if assigned(indexnext) then
  108. Message(parser_e_C_array_of_const_must_be_last);
  109. end;
  110. end;
  111. end;
  112. procedure parameter_dec(aktprocdef:tabstractprocdef);
  113. {
  114. handle_procvar needs the same changes
  115. }
  116. var
  117. is_procvar : boolean;
  118. sc : tsinglelist;
  119. htype,
  120. tt : ttype;
  121. arrayelementtype : ttype;
  122. hvs,
  123. vs : tvarsym;
  124. srsym : tsym;
  125. hs1 : string;
  126. varspez : Tvarspez;
  127. inserthigh : boolean;
  128. tdefaultvalue : tconstsym;
  129. defaultrequired : boolean;
  130. old_object_option : tsymoptions;
  131. dummyst : tparasymtable;
  132. currparast : tparasymtable;
  133. begin
  134. consume(_LKLAMMER);
  135. { Delphi/Kylix supports nonsense like }
  136. { procedure p(); }
  137. if try_to_consume(_RKLAMMER) and
  138. not(m_tp7 in aktmodeswitches) then
  139. exit;
  140. { parsing a proc or procvar ? }
  141. is_procvar:=(aktprocdef.deftype=procvardef);
  142. { create dummy symtable for procvars }
  143. if is_procvar then
  144. begin
  145. dummyst:=tparasymtable.create;
  146. currparast:=dummyst;
  147. end
  148. else
  149. begin
  150. currparast:=tparasymtable(tprocdef(aktprocdef).parast);
  151. end;
  152. { reset }
  153. sc:=tsinglelist.create;
  154. defaultrequired:=false;
  155. { the variables are always public }
  156. old_object_option:=current_object_option;
  157. current_object_option:=[sp_public];
  158. inc(testcurobject);
  159. repeat
  160. if try_to_consume(_VAR) then
  161. varspez:=vs_var
  162. else
  163. if try_to_consume(_CONST) then
  164. varspez:=vs_const
  165. else
  166. if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  167. begin
  168. consume(_OUT);
  169. varspez:=vs_out
  170. end
  171. else
  172. varspez:=vs_value;
  173. inserthigh:=false;
  174. tdefaultvalue:=nil;
  175. tt.reset;
  176. { self is only allowed in procvars and class methods }
  177. if (idtoken=_SELF) and
  178. (is_procvar or
  179. (assigned(procinfo._class) and is_class(procinfo._class))) then
  180. begin
  181. if varspez <> vs_value then
  182. CGMessage(parser_e_self_call_by_value);
  183. if not is_procvar then
  184. begin
  185. htype.setdef(procinfo._class);
  186. vs:=tvarsym.create('@',htype);
  187. vs.varspez:=vs_var;
  188. { insert the sym in the parasymtable }
  189. tprocdef(aktprocdef).parast.insert(vs);
  190. inc(procinfo.selfpointer_offset,vs.address);
  191. end
  192. else
  193. vs:=nil;
  194. { must also be included for procvars to allow the proc2procvar }
  195. { type conversions (po_containsself is in po_comp) (JM) }
  196. include(aktprocdef.procoptions,po_containsself);
  197. consume(idtoken);
  198. consume(_COLON);
  199. single_type(tt,hs1,false);
  200. { this must be call-by-value, but we generate already an }
  201. { an error above if that's not the case (JM) }
  202. aktprocdef.concatpara(tt,vs,varspez,nil);
  203. { check the types for procedures only }
  204. if not is_procvar then
  205. begin
  206. if compare_defs(tt.def,procinfo._class,nothingn)=te_incompatible then
  207. CGMessage2(type_e_incompatible_types,tt.def.typename,procinfo._class.typename);
  208. end;
  209. end
  210. else
  211. begin
  212. { read identifiers and insert with error type }
  213. sc.reset;
  214. repeat
  215. vs:=tvarsym.create(orgpattern,generrortype);
  216. currparast.insert(vs);
  217. sc.insert(vs);
  218. consume(_ID);
  219. until not try_to_consume(_COMMA);
  220. { read type declaration, force reading for value and const paras }
  221. if (token=_COLON) or (varspez=vs_value) then
  222. begin
  223. consume(_COLON);
  224. { check for an open array }
  225. if token=_ARRAY then
  226. begin
  227. consume(_ARRAY);
  228. consume(_OF);
  229. { define range and type of range }
  230. tt.setdef(tarraydef.create(0,-1,s32bittype));
  231. { array of const ? }
  232. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  233. begin
  234. consume(_CONST);
  235. srsym:=searchsymonlyin(systemunit,'TVARREC');
  236. if not assigned(srsym) then
  237. InternalError(1234124);
  238. tarraydef(tt.def).setelementtype(ttypesym(srsym).restype);
  239. tarraydef(tt.def).IsArrayOfConst:=true;
  240. end
  241. else
  242. begin
  243. { define field type }
  244. single_type(arrayelementtype,hs1,false);
  245. tarraydef(tt.def).setelementtype(arrayelementtype);
  246. end;
  247. inserthigh:=true;
  248. end
  249. else
  250. begin
  251. { open string ? }
  252. if (varspez=vs_var) and
  253. (
  254. (
  255. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  256. (cs_openstring in aktmoduleswitches) and
  257. not(cs_ansistrings in aktlocalswitches)
  258. ) or
  259. (idtoken=_OPENSTRING)) then
  260. begin
  261. consume(token);
  262. tt:=openshortstringtype;
  263. hs1:='openstring';
  264. inserthigh:=true;
  265. end
  266. else
  267. begin
  268. { everything else }
  269. single_type(tt,hs1,false);
  270. end;
  271. { default parameter }
  272. if (m_default_para in aktmodeswitches) then
  273. begin
  274. if try_to_consume(_EQUAL) then
  275. begin
  276. vs:=tvarsym(sc.first);
  277. if assigned(vs.listnext) then
  278. Message(parser_e_default_value_only_one_para);
  279. { prefix 'def' to the parameter name }
  280. tdefaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  281. if assigned(tdefaultvalue) then
  282. tprocdef(aktprocdef).parast.insert(tdefaultvalue);
  283. defaultrequired:=true;
  284. end
  285. else
  286. begin
  287. if defaultrequired then
  288. Message1(parser_e_default_value_expected_for_para,vs.name);
  289. end;
  290. end;
  291. end;
  292. end
  293. else
  294. begin
  295. {$ifndef UseNiceNames}
  296. hs1:='$$$';
  297. {$else UseNiceNames}
  298. hs1:='var';
  299. {$endif UseNiceNames}
  300. tt:=cformaltype;
  301. end;
  302. { For proc vars we only need the definitions }
  303. if not is_procvar then
  304. begin
  305. vs:=tvarsym(sc.first);
  306. while assigned(vs) do
  307. begin
  308. { update varsym }
  309. vs.vartype:=tt;
  310. vs.varspez:=varspez;
  311. if (varspez in [vs_var,vs_const,vs_out]) and
  312. paramanager.push_addr_param(tt.def,aktprocdef.proccalloption) then
  313. include(vs.varoptions,vo_regable);
  314. { do we need a local copy? Then rename the varsym, do this after the
  315. insert so the dup id checking is done correctly }
  316. if (varspez=vs_value) and
  317. paramanager.push_addr_param(tt.def,aktprocdef.proccalloption) and
  318. not(is_open_array(tt.def) or is_array_of_const(tt.def)) then
  319. currparast.rename(vs.name,'val'+vs.name);
  320. { also need to push a high value? }
  321. if inserthigh then
  322. begin
  323. hvs:=tvarsym.create('$high'+vs.name,s32bittype);
  324. hvs.varspez:=vs_const;
  325. currparast.insert(hvs);
  326. end;
  327. aktprocdef.concatpara(tt,vs,varspez,tdefaultvalue);
  328. vs:=tvarsym(vs.listnext);
  329. end;
  330. end
  331. else
  332. begin
  333. vs:=tvarsym(sc.first);
  334. while assigned(vs) do
  335. begin
  336. { don't insert a parasym, the varsyms will be
  337. disposed }
  338. aktprocdef.concatpara(tt,nil,varspez,tdefaultvalue);
  339. vs:=tvarsym(vs.listnext);
  340. end;
  341. end;
  342. end;
  343. { set the new mangled name }
  344. until not try_to_consume(_SEMICOLON);
  345. { remove parasymtable from stack }
  346. if is_procvar then
  347. dummyst.free;
  348. sc.free;
  349. { reset object options }
  350. dec(testcurobject);
  351. current_object_option:=old_object_option;
  352. consume(_RKLAMMER);
  353. end;
  354. procedure parse_proc_head(options:tproctypeoption);
  355. var
  356. orgsp,sp:stringid;
  357. paramoffset:longint;
  358. sym:tsym;
  359. st : tsymtable;
  360. srsymtable : tsymtable;
  361. pdl : pprocdeflist;
  362. storepos,procstartfilepos : tfileposinfo;
  363. i: longint;
  364. begin
  365. { Save the position where this procedure really starts }
  366. procstartfilepos:=akttokenpos;
  367. aktprocdef:=nil;
  368. if (options=potype_operator) then
  369. begin
  370. sp:=overloaded_names[optoken];
  371. orgsp:=sp;
  372. end
  373. else
  374. begin
  375. sp:=pattern;
  376. orgsp:=orgpattern;
  377. consume(_ID);
  378. end;
  379. { examine interface map: function/procedure iname.functionname=locfuncname }
  380. if parse_only and
  381. assigned(procinfo._class) and
  382. assigned(procinfo._class.implementedinterfaces) and
  383. (procinfo._class.implementedinterfaces.count>0) and
  384. try_to_consume(_POINT) then
  385. begin
  386. storepos:=akttokenpos;
  387. akttokenpos:=procstartfilepos;
  388. { get interface syms}
  389. searchsym(sp,sym,srsymtable);
  390. if not assigned(sym) then
  391. begin
  392. identifier_not_found(orgsp);
  393. sym:=generrorsym;
  394. end;
  395. akttokenpos:=storepos;
  396. { load proc name }
  397. if sym.typ=typesym then
  398. i:=procinfo._class.implementedinterfaces.searchintf(ttypesym(sym).restype.def);
  399. { qualifier is interface name? }
  400. if (sym.typ<>typesym) or (ttypesym(sym).restype.def.deftype<>objectdef) or
  401. (i=-1) then
  402. begin
  403. Message(parser_e_interface_id_expected);
  404. aktprocsym:=nil;
  405. end
  406. else
  407. begin
  408. aktprocsym:=tprocsym(procinfo._class.implementedinterfaces.interfaces(i).symtable.search(sp));
  409. { the method can be declared after the mapping FK
  410. if not(assigned(aktprocsym)) then
  411. Message(parser_e_methode_id_expected);
  412. }
  413. end;
  414. consume(_ID);
  415. consume(_EQUAL);
  416. if (token=_ID) { and assigned(aktprocsym) } then
  417. procinfo._class.implementedinterfaces.addmappings(i,sp,pattern);
  418. consume(_ID);
  419. exit;
  420. end;
  421. { method ? }
  422. if not(parse_only) and
  423. (lexlevel=normal_function_level) and
  424. try_to_consume(_POINT) then
  425. begin
  426. { search for object name }
  427. storepos:=akttokenpos;
  428. akttokenpos:=procstartfilepos;
  429. searchsym(sp,sym,srsymtable);
  430. if not assigned(sym) then
  431. begin
  432. identifier_not_found(orgsp);
  433. sym:=generrorsym;
  434. end;
  435. akttokenpos:=storepos;
  436. { consume proc name }
  437. sp:=pattern;
  438. orgsp:=orgpattern;
  439. procstartfilepos:=akttokenpos;
  440. consume(_ID);
  441. { qualifier is class name ? }
  442. if (sym.typ<>typesym) or
  443. (ttypesym(sym).restype.def.deftype<>objectdef) then
  444. begin
  445. Message(parser_e_class_id_expected);
  446. aktprocsym:=nil;
  447. aktprocdef:=nil;
  448. end
  449. else
  450. begin
  451. { used to allow private syms to be seen }
  452. aktobjectdef:=tobjectdef(ttypesym(sym).restype.def);
  453. procinfo._class:=tobjectdef(ttypesym(sym).restype.def);
  454. aktprocsym:=tprocsym(procinfo._class.symtable.search(sp));
  455. {The procedure has been found. So it is
  456. a global one. Set the flags to mark this.}
  457. procinfo.flags:=procinfo.flags or pi_is_global;
  458. aktobjectdef:=nil;
  459. { we solve this below }
  460. if not(assigned(aktprocsym)) then
  461. Message(parser_e_methode_id_expected);
  462. end;
  463. end
  464. else
  465. begin
  466. { check for constructor/destructor which is not allowed here }
  467. if (not parse_only) and
  468. (options in [potype_constructor,potype_destructor]) then
  469. Message(parser_e_constructors_always_objects);
  470. akttokenpos:=procstartfilepos;
  471. aktprocsym:=tprocsym(symtablestack.search(sp));
  472. if not(parse_only) then
  473. begin
  474. {The procedure we prepare for is in the implementation
  475. part of the unit we compile. It is also possible that we
  476. are compiling a program, which is also some kind of
  477. implementaion part.
  478. We need to find out if the procedure is global. If it is
  479. global, it is in the global symtable.}
  480. if not assigned(aktprocsym) and
  481. (symtablestack.symtabletype=staticsymtable) and
  482. assigned(symtablestack.next) and
  483. (symtablestack.next.unitid=0) then
  484. begin
  485. {Search the procedure in the global symtable.}
  486. aktprocsym:=tprocsym(symtablestack.next.search(sp));
  487. if assigned(aktprocsym) then
  488. begin
  489. {Check if it is a procedure.}
  490. if aktprocsym.typ<>procsym then
  491. DuplicateSym(aktprocsym);
  492. {The procedure has been found. So it is
  493. a global one. Set the flags to mark this.}
  494. procinfo.flags:=procinfo.flags or pi_is_global;
  495. end;
  496. end;
  497. end;
  498. end;
  499. if assigned(aktprocsym) then
  500. begin
  501. { Check if overloaded is a procsym }
  502. if aktprocsym.typ<>procsym then
  503. begin
  504. { when the other symbol is a unit symbol then hide the unit
  505. symbol. Only in tp mode because it's bad programming }
  506. if (m_duplicate_names in aktmodeswitches) and
  507. (aktprocsym.typ=unitsym) then
  508. begin
  509. aktprocsym.owner.rename(aktprocsym.name,'hidden'+aktprocsym.name);
  510. end
  511. else
  512. begin
  513. { we use a different error message for tp7 so it looks more compatible }
  514. if (m_fpc in aktmodeswitches) then
  515. Message1(parser_e_overloaded_no_procedure,aktprocsym.realname)
  516. else
  517. DuplicateSym(aktprocsym);
  518. { rename the name to an unique name to avoid an
  519. error when inserting the symbol in the symtable }
  520. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  521. end;
  522. { generate a new aktprocsym }
  523. aktprocsym:=nil;
  524. end;
  525. end;
  526. { test again if assigned, it can be reset to recover }
  527. if not assigned(aktprocsym) then
  528. begin
  529. { create a new procsym and set the real filepos }
  530. akttokenpos:=procstartfilepos;
  531. { for operator we have only one procsym for each overloaded
  532. operation }
  533. if (options=potype_operator) then
  534. begin
  535. { is the current overload sym already in the current unit }
  536. if assigned(overloaded_operators[optoken]) and
  537. (overloaded_operators[optoken].owner=symtablestack) then
  538. aktprocsym:=overloaded_operators[optoken]
  539. else
  540. begin
  541. { create the procsym with saving the original case }
  542. aktprocsym:=tprocsym.create('$'+sp);
  543. if assigned(overloaded_operators[optoken]) then
  544. overloaded_operators[optoken].concat_procdefs_to(aktprocsym);
  545. end;
  546. end
  547. else
  548. aktprocsym:=tprocsym.create(orgsp);
  549. symtablestack.insert(aktprocsym);
  550. end;
  551. st:=symtablestack;
  552. aktprocdef:=tprocdef.create;
  553. aktprocdef.symtablelevel:=symtablestack.symtablelevel;
  554. if assigned(procinfo._class) then
  555. aktprocdef._class := procinfo._class;
  556. { set the options from the caller (podestructor or poconstructor) }
  557. aktprocdef.proctypeoption:=options;
  558. { add procsym to the procdef }
  559. aktprocdef.procsym:=aktprocsym;
  560. { save file position }
  561. aktprocdef.fileinfo:=procstartfilepos;
  562. { this must also be inserted in the right symtable !! PM }
  563. { otherwise we get subbtle problems with
  564. definitions of args defs in staticsymtable for
  565. implementation of a global method }
  566. if token=_LKLAMMER then
  567. parameter_dec(aktprocdef);
  568. { calculate the offset of the parameters }
  569. paramoffset:=target_info.first_parm_offset;
  570. { calculate frame pointer offset }
  571. if lexlevel>normal_function_level then
  572. begin
  573. procinfo.framepointer_offset:=paramoffset;
  574. inc(paramoffset,pointer_size);
  575. { this is needed to get correct framepointer push for local
  576. forward functions !! }
  577. aktprocdef.parast.symtablelevel:=lexlevel;
  578. end;
  579. if assigned (procinfo._Class) and
  580. is_object(procinfo._Class) and
  581. (aktprocdef.proctypeoption in [potype_constructor,potype_destructor]) then
  582. inc(paramoffset,pointer_size);
  583. { self pointer offset, must be done after parsing the parameters }
  584. { self isn't pushed in nested procedure of methods }
  585. if assigned(procinfo._class) and (lexlevel=normal_function_level) then
  586. begin
  587. procinfo.selfpointer_offset:=paramoffset;
  588. if assigned(aktprocdef) and
  589. not(po_containsself in aktprocdef.procoptions) then
  590. inc(paramoffset,pointer_size);
  591. end;
  592. { con/-destructor flag ? }
  593. if assigned (procinfo._Class) and
  594. is_class(procinfo._class) and
  595. (aktprocdef.proctypeoption in [potype_destructor,potype_constructor]) then
  596. inc(paramoffset,pointer_size);
  597. procinfo.para_offset:=paramoffset;
  598. { so we only restore the symtable now }
  599. symtablestack:=st;
  600. if (options=potype_operator) then
  601. overloaded_operators[optoken]:=aktprocsym;
  602. end;
  603. procedure parse_proc_dec;
  604. var
  605. hs : string;
  606. isclassmethod : boolean;
  607. begin
  608. inc(lexlevel);
  609. { read class method }
  610. if token=_CLASS then
  611. begin
  612. consume(_CLASS);
  613. isclassmethod:=true;
  614. end
  615. else
  616. isclassmethod:=false;
  617. case token of
  618. _FUNCTION : begin
  619. consume(_FUNCTION);
  620. parse_proc_head(potype_none);
  621. if token<>_COLON then
  622. begin
  623. if assigned(aktprocsym) and
  624. not(is_interface(aktprocdef._class)) and
  625. not(aktprocdef.forwarddef) or
  626. (m_repeat_forward in aktmodeswitches) then
  627. begin
  628. consume(_COLON);
  629. consume_all_until(_SEMICOLON);
  630. end;
  631. end
  632. else
  633. begin
  634. consume(_COLON);
  635. inc(testcurobject);
  636. single_type(aktprocdef.rettype,hs,false);
  637. aktprocdef.test_if_fpu_result;
  638. if (aktprocdef.rettype.def.deftype=stringdef) and
  639. (tstringdef(aktprocdef.rettype.def).string_typ<>st_shortstring) then
  640. procinfo.no_fast_exit:=true;
  641. dec(testcurobject);
  642. end;
  643. end;
  644. _PROCEDURE : begin
  645. consume(_PROCEDURE);
  646. parse_proc_head(potype_none);
  647. if assigned(aktprocsym) then
  648. aktprocdef.rettype:=voidtype;
  649. end;
  650. _CONSTRUCTOR : begin
  651. consume(_CONSTRUCTOR);
  652. parse_proc_head(potype_constructor);
  653. if assigned(procinfo._class) and
  654. is_class(procinfo._class) then
  655. begin
  656. { CLASS constructors return the created instance }
  657. aktprocdef.rettype.setdef(procinfo._class);
  658. end
  659. else
  660. begin
  661. { OBJECT constructors return a boolean }
  662. aktprocdef.rettype:=booltype;
  663. end;
  664. end;
  665. _DESTRUCTOR : begin
  666. consume(_DESTRUCTOR);
  667. parse_proc_head(potype_destructor);
  668. aktprocdef.rettype:=voidtype;
  669. end;
  670. _OPERATOR : begin
  671. if lexlevel>normal_function_level then
  672. Message(parser_e_no_local_operator);
  673. consume(_OPERATOR);
  674. if (token in [first_overloaded..last_overloaded]) then
  675. begin
  676. procinfo.flags:=procinfo.flags or pi_operator;
  677. optoken:=token;
  678. end
  679. else
  680. begin
  681. Message(parser_e_overload_operator_failed);
  682. { Use the dummy NOTOKEN that is also declared
  683. for the overloaded_operator[] }
  684. optoken:=NOTOKEN;
  685. end;
  686. consume(Token);
  687. parse_proc_head(potype_operator);
  688. if token<>_ID then
  689. begin
  690. otsym:=nil;
  691. if not(m_result in aktmodeswitches) then
  692. consume(_ID);
  693. end
  694. else
  695. begin
  696. otsym:=tvarsym.create(pattern,voidtype);
  697. consume(_ID);
  698. end;
  699. if not try_to_consume(_COLON) then
  700. begin
  701. consume(_COLON);
  702. aktprocdef.rettype:=generrortype;
  703. consume_all_until(_SEMICOLON);
  704. end
  705. else
  706. begin
  707. single_type(aktprocdef.rettype,hs,false);
  708. aktprocdef.test_if_fpu_result;
  709. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  710. ((aktprocdef.rettype.def.deftype<>orddef) or
  711. (torddef(aktprocdef.rettype.def).typ<>bool8bit)) then
  712. Message(parser_e_comparative_operator_return_boolean);
  713. if assigned(otsym) then
  714. otsym.vartype.def:=aktprocdef.rettype.def;
  715. if (optoken=_ASSIGNMENT) and
  716. equal_defs(aktprocdef.rettype.def,
  717. tvarsym(aktprocdef.parast.symindex.first).vartype.def) then
  718. message(parser_e_no_such_assignment)
  719. else if not isoperatoracceptable(aktprocdef,optoken) then
  720. Message(parser_e_overload_impossible);
  721. end;
  722. end;
  723. end;
  724. if isclassmethod and
  725. assigned(aktprocsym) then
  726. include(aktprocdef.procoptions,po_classmethod);
  727. { support procedure proc;stdcall export; in Delphi mode only }
  728. if not((m_delphi in aktmodeswitches) and
  729. is_proc_directive(token)) then
  730. consume(_SEMICOLON);
  731. dec(lexlevel);
  732. end;
  733. {****************************************************************************
  734. Procedure directive handlers
  735. ****************************************************************************}
  736. procedure pd_far;
  737. begin
  738. Message(parser_w_proc_far_ignored);
  739. end;
  740. procedure pd_near;
  741. begin
  742. Message(parser_w_proc_near_ignored);
  743. end;
  744. procedure pd_export;
  745. begin
  746. if assigned(procinfo._class) then
  747. Message(parser_e_methods_dont_be_export);
  748. if lexlevel<>normal_function_level then
  749. Message(parser_e_dont_nest_export);
  750. { only os/2 needs this }
  751. if target_info.system=system_i386_os2 then
  752. begin
  753. aktprocdef.aliasnames.insert(aktprocsym.realname);
  754. procinfo.exported:=true;
  755. if cs_link_deffile in aktglobalswitches then
  756. deffile.AddExport(aktprocdef.mangledname);
  757. end;
  758. end;
  759. procedure pd_forward;
  760. begin
  761. aktprocdef.forwarddef:=true;
  762. end;
  763. procedure pd_alias;
  764. begin
  765. consume(_COLON);
  766. aktprocdef.aliasnames.insert(get_stringconst);
  767. end;
  768. procedure pd_asmname;
  769. begin
  770. aktprocdef.setmangledname(target_info.Cprefix+pattern);
  771. if token=_CCHAR then
  772. consume(_CCHAR)
  773. else
  774. consume(_CSTRING);
  775. { we don't need anything else }
  776. aktprocdef.forwarddef:=false;
  777. end;
  778. procedure pd_inline;
  779. var
  780. hp : tparaitem;
  781. begin
  782. { check if there is an array of const }
  783. hp:=tparaitem(aktprocdef.para.first);
  784. while assigned(hp) do
  785. begin
  786. if assigned(hp.paratype.def) and
  787. (hp.paratype.def.deftype=arraydef) then
  788. begin
  789. with tarraydef(hp.paratype.def) do
  790. if IsVariant or IsConstructor {or IsArrayOfConst} then
  791. begin
  792. Message1(parser_w_not_supported_for_inline,'array of const');
  793. Message(parser_w_inlining_disabled);
  794. aktprocdef.proccalloption:=pocall_fpccall;
  795. end;
  796. end;
  797. hp:=tparaitem(hp.next);
  798. end;
  799. end;
  800. procedure pd_intern;
  801. begin
  802. consume(_COLON);
  803. aktprocdef.extnumber:=get_intconst;
  804. end;
  805. procedure pd_interrupt;
  806. begin
  807. if lexlevel<>normal_function_level then
  808. Message(parser_e_dont_nest_interrupt);
  809. end;
  810. procedure pd_abstract;
  811. begin
  812. if (po_virtualmethod in aktprocdef.procoptions) then
  813. include(aktprocdef.procoptions,po_abstractmethod)
  814. else
  815. Message(parser_e_only_virtual_methods_abstract);
  816. { the method is defined }
  817. aktprocdef.forwarddef:=false;
  818. end;
  819. procedure pd_virtual;
  820. {$ifdef WITHDMT}
  821. var
  822. pt : tnode;
  823. {$endif WITHDMT}
  824. begin
  825. if (aktprocdef.proctypeoption=potype_constructor) and
  826. is_object(aktprocdef._class) then
  827. Message(parser_e_constructor_cannot_be_not_virtual);
  828. {$ifdef WITHDMT}
  829. if is_object(aktprocdef._class) and
  830. (token<>_SEMICOLON) then
  831. begin
  832. { any type of parameter is allowed here! }
  833. pt:=comp_expr(true);
  834. if is_constintnode(pt) then
  835. begin
  836. include(aktprocdef.procoptions,po_msgint);
  837. aktprocdef.messageinf.i:=pt^.value;
  838. end
  839. else
  840. Message(parser_e_ill_msg_expr);
  841. disposetree(pt);
  842. end;
  843. {$endif WITHDMT}
  844. end;
  845. procedure pd_static;
  846. begin
  847. if (cs_static_keyword in aktmoduleswitches) then
  848. begin
  849. include(aktprocsym.symoptions,sp_static);
  850. include(aktprocdef.procoptions,po_staticmethod);
  851. end;
  852. end;
  853. procedure pd_override;
  854. begin
  855. if not(is_class_or_interface(aktprocdef._class)) then
  856. Message(parser_e_no_object_override);
  857. end;
  858. procedure pd_overload;
  859. begin
  860. include(aktprocsym.symoptions,sp_has_overloaded);
  861. end;
  862. procedure pd_message;
  863. var
  864. pt : tnode;
  865. begin
  866. { check parameter type }
  867. if not(po_containsself in aktprocdef.procoptions) and
  868. ((aktprocdef.minparacount<>1) or
  869. (aktprocdef.maxparacount<>1) or
  870. (TParaItem(aktprocdef.Para.first).paratyp<>vs_var)) then
  871. Message(parser_e_ill_msg_param);
  872. pt:=comp_expr(true);
  873. if pt.nodetype=stringconstn then
  874. begin
  875. include(aktprocdef.procoptions,po_msgstr);
  876. aktprocdef.messageinf.str:=strnew(tstringconstnode(pt).value_str);
  877. end
  878. else
  879. if is_constintnode(pt) then
  880. begin
  881. include(aktprocdef.procoptions,po_msgint);
  882. aktprocdef.messageinf.i:=tordconstnode(pt).value;
  883. end
  884. else
  885. Message(parser_e_ill_msg_expr);
  886. pt.free;
  887. end;
  888. procedure pd_reintroduce;
  889. begin
  890. Message1(parser_w_proc_directive_ignored,'REINTRODUCE');
  891. end;
  892. procedure pd_syscall;
  893. begin
  894. aktprocdef.forwarddef:=false;
  895. aktprocdef.extnumber:=get_intconst;
  896. end;
  897. procedure pd_external;
  898. {
  899. If import_dll=nil the procedure is assumed to be in another
  900. object file. In that object file it should have the name to
  901. which import_name is pointing to. Otherwise, the procedure is
  902. assumed to be in the DLL to which import_dll is pointing to. In
  903. that case either import_nr<>0 or import_name<>nil is true, so
  904. the procedure is either imported by number or by name. (DM)
  905. }
  906. var
  907. pd : tprocdef;
  908. import_dll,
  909. import_name : string;
  910. import_nr : word;
  911. begin
  912. aktprocdef.forwarddef:=false;
  913. { forbid local external procedures }
  914. if lexlevel>normal_function_level then
  915. Message(parser_e_no_local_external);
  916. { If the procedure should be imported from a DLL, a constant string follows.
  917. This isn't really correct, an contant string expression follows
  918. so we check if an semicolon follows, else a string constant have to
  919. follow (FK) }
  920. import_nr:=0;
  921. import_name:='';
  922. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  923. begin
  924. import_dll:=get_stringconst;
  925. if (idtoken=_NAME) then
  926. begin
  927. consume(_NAME);
  928. import_name:=get_stringconst;
  929. end;
  930. if (idtoken=_INDEX) then
  931. begin
  932. {After the word index follows the index number in the DLL.}
  933. consume(_INDEX);
  934. import_nr:=get_intconst;
  935. end;
  936. { default is to used the realname of the procedure }
  937. if (import_nr=0) and (import_name='') then
  938. import_name:=aktprocsym.realname;
  939. { create importlib if not already done }
  940. if not(current_module.uses_imports) then
  941. begin
  942. current_module.uses_imports:=true;
  943. importlib.preparelib(current_module.modulename^);
  944. end;
  945. if not(m_repeat_forward in aktmodeswitches) then
  946. begin
  947. { we can only have one overloaded here ! }
  948. if aktprocsym.procdef_count>1 then
  949. pd:=aktprocsym.procdef[2]
  950. else
  951. pd:=aktprocdef;
  952. end
  953. else
  954. pd:=aktprocdef;
  955. importlib.importproceduredef(pd,import_dll,import_nr,import_name);
  956. end
  957. else
  958. begin
  959. if (idtoken=_NAME) then
  960. begin
  961. consume(_NAME);
  962. import_name:=get_stringconst;
  963. aktprocdef.setmangledname(import_name);
  964. end;
  965. end;
  966. end;
  967. type
  968. pd_handler=procedure;
  969. proc_dir_rec=record
  970. idtok : ttoken;
  971. pd_flags : longint;
  972. handler : pd_handler;
  973. pocall : tproccalloption;
  974. pooption : tprocoptions;
  975. mutexclpocall : tproccalloptions;
  976. mutexclpotype : tproctypeoptions;
  977. mutexclpo : tprocoptions;
  978. end;
  979. const
  980. {Should contain the number of procedure directives we support.}
  981. num_proc_directives=36;
  982. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  983. (
  984. (
  985. idtok:_ABSTRACT;
  986. pd_flags : pd_interface+pd_object+pd_notobjintf;
  987. handler : {$ifdef FPCPROCVAR}@{$endif}pd_abstract;
  988. pocall : pocall_none;
  989. pooption : [po_abstractmethod];
  990. mutexclpocall : [pocall_internproc,pocall_inline];
  991. mutexclpotype : [potype_constructor,potype_destructor];
  992. mutexclpo : [po_exports,po_interrupt,po_external]
  993. ),(
  994. idtok:_ALIAS;
  995. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  996. handler : {$ifdef FPCPROCVAR}@{$endif}pd_alias;
  997. pocall : pocall_none;
  998. pooption : [];
  999. mutexclpocall : [pocall_inline];
  1000. mutexclpotype : [];
  1001. mutexclpo : [po_external]
  1002. ),(
  1003. idtok:_ASMNAME;
  1004. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1005. handler : {$ifdef FPCPROCVAR}@{$endif}pd_asmname;
  1006. pocall : pocall_cdecl;
  1007. pooption : [po_external];
  1008. mutexclpocall : [pocall_internproc,pocall_inline];
  1009. mutexclpotype : [];
  1010. mutexclpo : [po_external]
  1011. ),(
  1012. idtok:_ASSEMBLER;
  1013. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1014. handler : nil;
  1015. pocall : pocall_none;
  1016. pooption : [po_assembler];
  1017. mutexclpocall : [];
  1018. mutexclpotype : [];
  1019. mutexclpo : [po_external]
  1020. ),(
  1021. idtok:_CDECL;
  1022. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1023. handler : nil;
  1024. pocall : pocall_cdecl;
  1025. pooption : [];
  1026. mutexclpocall : [];
  1027. mutexclpotype : [];
  1028. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1029. ),(
  1030. idtok:_DYNAMIC;
  1031. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1032. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1033. pocall : pocall_none;
  1034. pooption : [po_virtualmethod];
  1035. mutexclpocall : [pocall_internproc,pocall_inline];
  1036. mutexclpotype : [];
  1037. mutexclpo : [po_exports,po_interrupt,po_external]
  1038. ),(
  1039. idtok:_EXPORT;
  1040. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??}+pd_notobjintf;
  1041. handler : {$ifdef FPCPROCVAR}@{$endif}pd_export;
  1042. pocall : pocall_none;
  1043. pooption : [po_exports];
  1044. mutexclpocall : [pocall_internproc,pocall_inline];
  1045. mutexclpotype : [];
  1046. mutexclpo : [po_external,po_interrupt]
  1047. ),(
  1048. idtok:_EXTERNAL;
  1049. pd_flags : pd_implemen+pd_interface+pd_notobjintf;
  1050. handler : {$ifdef FPCPROCVAR}@{$endif}pd_external;
  1051. pocall : pocall_none;
  1052. pooption : [po_external];
  1053. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  1054. mutexclpotype : [];
  1055. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1056. ),(
  1057. idtok:_FAR;
  1058. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar+pd_notobjintf;
  1059. handler : {$ifdef FPCPROCVAR}@{$endif}pd_far;
  1060. pocall : pocall_none;
  1061. pooption : [];
  1062. mutexclpocall : [pocall_internproc,pocall_inline];
  1063. mutexclpotype : [];
  1064. mutexclpo : []
  1065. ),(
  1066. idtok:_FAR16;
  1067. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1068. handler : nil;
  1069. pocall : pocall_far16;
  1070. pooption : [];
  1071. mutexclpocall : [];
  1072. mutexclpotype : [];
  1073. mutexclpo : [po_external,po_leftright]
  1074. ),(
  1075. idtok:_FORWARD;
  1076. pd_flags : pd_implemen+pd_notobjintf;
  1077. handler : {$ifdef FPCPROCVAR}@{$endif}pd_forward;
  1078. pocall : pocall_none;
  1079. pooption : [];
  1080. mutexclpocall : [pocall_internproc,pocall_inline];
  1081. mutexclpotype : [];
  1082. mutexclpo : [po_external]
  1083. ),(
  1084. idtok:_FPCCALL;
  1085. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1086. handler : nil;
  1087. pocall : pocall_fpccall;
  1088. pooption : [];
  1089. mutexclpocall : [];
  1090. mutexclpotype : [];
  1091. mutexclpo : [po_leftright]
  1092. ),(
  1093. idtok:_INLINE;
  1094. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1095. handler : {$ifdef FPCPROCVAR}@{$endif}pd_inline;
  1096. pocall : pocall_inline;
  1097. pooption : [];
  1098. mutexclpocall : [];
  1099. mutexclpotype : [potype_constructor,potype_destructor];
  1100. mutexclpo : [po_exports,po_external,po_interrupt]
  1101. ),(
  1102. idtok:_INTERNCONST;
  1103. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1104. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1105. pocall : pocall_none;
  1106. pooption : [po_internconst];
  1107. mutexclpocall : [];
  1108. mutexclpotype : [potype_operator];
  1109. mutexclpo : []
  1110. ),(
  1111. idtok:_INTERNPROC;
  1112. pd_flags : pd_implemen+pd_notobjintf;
  1113. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1114. pocall : pocall_internproc;
  1115. pooption : [];
  1116. mutexclpocall : [];
  1117. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1118. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_leftright]
  1119. ),(
  1120. idtok:_INTERRUPT;
  1121. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1122. handler : {$ifdef FPCPROCVAR}@{$endif}pd_interrupt;
  1123. pocall : pocall_none;
  1124. pooption : [po_interrupt];
  1125. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,
  1126. pocall_inline,pocall_pascal,pocall_system,pocall_far16,pocall_fpccall];
  1127. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1128. mutexclpo : [po_external,po_leftright,po_clearstack]
  1129. ),(
  1130. idtok:_IOCHECK;
  1131. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1132. handler : nil;
  1133. pocall : pocall_none;
  1134. pooption : [po_iocheck];
  1135. mutexclpocall : [pocall_internproc];
  1136. mutexclpotype : [];
  1137. mutexclpo : [po_external]
  1138. ),(
  1139. idtok:_MESSAGE;
  1140. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1141. handler : {$ifdef FPCPROCVAR}@{$endif}pd_message;
  1142. pocall : pocall_none;
  1143. pooption : []; { can be po_msgstr or po_msgint }
  1144. mutexclpocall : [pocall_inline,pocall_internproc];
  1145. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1146. mutexclpo : [po_interrupt,po_external]
  1147. ),(
  1148. idtok:_NEAR;
  1149. pd_flags : pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1150. handler : {$ifdef FPCPROCVAR}@{$endif}pd_near;
  1151. pocall : pocall_none;
  1152. pooption : [];
  1153. mutexclpocall : [pocall_internproc];
  1154. mutexclpotype : [];
  1155. mutexclpo : []
  1156. ),(
  1157. idtok:_OVERLOAD;
  1158. pd_flags : pd_implemen+pd_interface+pd_body;
  1159. handler : {$ifdef FPCPROCVAR}@{$endif}pd_overload;
  1160. pocall : pocall_none;
  1161. pooption : [po_overload];
  1162. mutexclpocall : [pocall_internproc];
  1163. mutexclpotype : [];
  1164. mutexclpo : []
  1165. ),(
  1166. idtok:_OVERRIDE;
  1167. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1168. handler : {$ifdef FPCPROCVAR}@{$endif}pd_override;
  1169. pocall : pocall_none;
  1170. pooption : [po_overridingmethod,po_virtualmethod];
  1171. mutexclpocall : [pocall_inline,pocall_internproc];
  1172. mutexclpotype : [];
  1173. mutexclpo : [po_exports,po_external,po_interrupt]
  1174. ),(
  1175. idtok:_PASCAL;
  1176. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1177. handler : nil;
  1178. pocall : pocall_pascal;
  1179. pooption : [];
  1180. mutexclpocall : [];
  1181. mutexclpotype : [];
  1182. mutexclpo : [po_external]
  1183. ),(
  1184. idtok:_POPSTACK;
  1185. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1186. handler : nil;
  1187. pocall : pocall_none;
  1188. pooption : [po_clearstack];
  1189. mutexclpocall : [pocall_inline,pocall_internproc,pocall_stdcall];
  1190. mutexclpotype : [];
  1191. mutexclpo : [po_assembler,po_external]
  1192. ),(
  1193. idtok:_PUBLIC;
  1194. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject+pd_notobjintf;
  1195. handler : nil;
  1196. pocall : pocall_none;
  1197. pooption : [];
  1198. mutexclpocall : [pocall_internproc,pocall_inline];
  1199. mutexclpotype : [];
  1200. mutexclpo : [po_external]
  1201. ),(
  1202. idtok:_REGISTER;
  1203. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1204. handler : nil;
  1205. pocall : pocall_register;
  1206. pooption : [];
  1207. mutexclpocall : [];
  1208. mutexclpotype : [];
  1209. mutexclpo : [po_external]
  1210. ),(
  1211. idtok:_REINTRODUCE;
  1212. pd_flags : pd_interface+pd_object;
  1213. handler : {$ifdef FPCPROCVAR}@{$endif}pd_reintroduce;
  1214. pocall : pocall_none;
  1215. pooption : [];
  1216. mutexclpocall : [];
  1217. mutexclpotype : [];
  1218. mutexclpo : []
  1219. ),(
  1220. idtok:_SAFECALL;
  1221. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1222. handler : nil;
  1223. pocall : pocall_safecall;
  1224. pooption : [];
  1225. mutexclpocall : [];
  1226. mutexclpotype : [];
  1227. mutexclpo : [po_external]
  1228. ),(
  1229. idtok:_SAVEREGISTERS;
  1230. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1231. handler : nil;
  1232. pocall : pocall_none;
  1233. pooption : [po_saveregisters];
  1234. mutexclpocall : [pocall_internproc];
  1235. mutexclpotype : [];
  1236. mutexclpo : [po_external]
  1237. ),(
  1238. idtok:_STATIC;
  1239. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1240. handler : {$ifdef FPCPROCVAR}@{$endif}pd_static;
  1241. pocall : pocall_none;
  1242. pooption : [po_staticmethod];
  1243. mutexclpocall : [pocall_inline,pocall_internproc];
  1244. mutexclpotype : [potype_constructor,potype_destructor];
  1245. mutexclpo : [po_external,po_interrupt,po_exports]
  1246. ),(
  1247. idtok:_STDCALL;
  1248. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1249. handler : nil;
  1250. pocall : pocall_stdcall;
  1251. pooption : [];
  1252. mutexclpocall : [];
  1253. mutexclpotype : [];
  1254. mutexclpo : [po_external]
  1255. ),(
  1256. idtok:_SYSCALL;
  1257. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1258. handler : {$ifdef FPCPROCVAR}@{$endif}pd_syscall;
  1259. pocall : pocall_palmossyscall;
  1260. pooption : [];
  1261. mutexclpocall : [];
  1262. mutexclpotype : [];
  1263. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1264. ),(
  1265. idtok:_SYSTEM;
  1266. pd_flags : pd_implemen+pd_notobjintf;
  1267. handler : nil;
  1268. pocall : pocall_system;
  1269. pooption : [];
  1270. mutexclpocall : [];
  1271. mutexclpotype : [];
  1272. mutexclpo : [po_external,po_assembler,po_interrupt]
  1273. ),(
  1274. idtok:_VIRTUAL;
  1275. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1276. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1277. pocall : pocall_none;
  1278. pooption : [po_virtualmethod];
  1279. mutexclpocall : [pocall_inline,pocall_internproc];
  1280. mutexclpotype : [];
  1281. mutexclpo : [po_external,po_interrupt,po_exports]
  1282. ),(
  1283. idtok:_CPPDECL;
  1284. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1285. handler : nil;
  1286. pocall : pocall_cppdecl;
  1287. pooption : [po_savestdregs];
  1288. mutexclpocall : [];
  1289. mutexclpotype : [];
  1290. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1291. ),(
  1292. idtok:_VARARGS;
  1293. pd_flags : pd_interface+pd_implemen+pd_procvar;
  1294. handler : nil;
  1295. pocall : pocall_none;
  1296. pooption : [po_varargs];
  1297. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1298. pocall_inline,pocall_far16,pocall_fpccall];
  1299. mutexclpotype : [];
  1300. mutexclpo : [po_assembler,po_interrupt,po_leftright]
  1301. ),(
  1302. idtok:_COMPILERPROC;
  1303. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1304. handler : nil;
  1305. pocall : pocall_compilerproc;
  1306. pooption : [];
  1307. mutexclpocall : [];
  1308. mutexclpotype : [];
  1309. mutexclpo : [po_interrupt]
  1310. )
  1311. );
  1312. function is_proc_directive(tok:ttoken):boolean;
  1313. var
  1314. i : longint;
  1315. begin
  1316. is_proc_directive:=false;
  1317. for i:=1 to num_proc_directives do
  1318. if proc_direcdata[i].idtok=idtoken then
  1319. begin
  1320. is_proc_directive:=true;
  1321. exit;
  1322. end;
  1323. end;
  1324. function parse_proc_direc(var pdflags:word):boolean;
  1325. {
  1326. Parse the procedure directive, returns true if a correct directive is found
  1327. }
  1328. var
  1329. p : longint;
  1330. found : boolean;
  1331. name : stringid;
  1332. begin
  1333. parse_proc_direc:=false;
  1334. name:=tokeninfo^[idtoken].str;
  1335. found:=false;
  1336. { Hint directive? Then exit immediatly }
  1337. if (m_hintdirective in aktmodeswitches) then
  1338. begin
  1339. case idtoken of
  1340. _LIBRARY,
  1341. _PLATFORM,
  1342. _UNIMPLEMENTED,
  1343. _DEPRECATED :
  1344. exit;
  1345. end;
  1346. end;
  1347. { retrieve data for directive if found }
  1348. for p:=1 to num_proc_directives do
  1349. if proc_direcdata[p].idtok=idtoken then
  1350. begin
  1351. found:=true;
  1352. break;
  1353. end;
  1354. { Check if the procedure directive is known }
  1355. if not found then
  1356. begin
  1357. { parsing a procvar type the name can be any
  1358. next variable !! }
  1359. if (pdflags and (pd_procvar or pd_object))=0 then
  1360. Message1(parser_w_unknown_proc_directive_ignored,name);
  1361. exit;
  1362. end;
  1363. { static needs a special treatment }
  1364. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1365. exit;
  1366. { Conflicts between directives ? }
  1367. if (aktprocdef.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1368. (aktprocdef.proccalloption in proc_direcdata[p].mutexclpocall) or
  1369. ((aktprocdef.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1370. begin
  1371. Message1(parser_e_proc_dir_conflict,name);
  1372. exit;
  1373. end;
  1374. { set calling convention }
  1375. if proc_direcdata[p].pocall<>pocall_none then
  1376. begin
  1377. if aktprocdef.proccalloption<>pocall_none then
  1378. begin
  1379. Message2(parser_w_proc_overriding_calling,
  1380. proccalloptionStr[aktprocdef.proccalloption],
  1381. proccalloptionStr[proc_direcdata[p].pocall]);
  1382. end;
  1383. aktprocdef.proccalloption:=proc_direcdata[p].pocall;
  1384. end;
  1385. { check if method and directive not for object, like public.
  1386. This needs to be checked also for procvars }
  1387. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1388. (aktprocdef.owner.symtabletype=objectsymtable) then
  1389. exit;
  1390. if aktprocdef.deftype=procdef then
  1391. begin
  1392. { Check if the directive is only for objects }
  1393. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1394. not assigned(aktprocdef._class) then
  1395. exit;
  1396. { check if method and directive not for interface }
  1397. if ((proc_direcdata[p].pd_flags and pd_notobjintf)<>0) and
  1398. is_interface(aktprocdef._class) then
  1399. exit;
  1400. end;
  1401. { consume directive, and turn flag on }
  1402. consume(token);
  1403. parse_proc_direc:=true;
  1404. { Check the pd_flags if the directive should be allowed }
  1405. if ((pdflags and pd_interface)<>0) and
  1406. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1407. begin
  1408. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1409. exit;
  1410. end;
  1411. if ((pdflags and pd_implemen)<>0) and
  1412. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1413. begin
  1414. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1415. exit;
  1416. end;
  1417. if ((pdflags and pd_procvar)<>0) and
  1418. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1419. begin
  1420. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1421. exit;
  1422. end;
  1423. { Return the new pd_flags }
  1424. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1425. pdflags:=pdflags and (not pd_body);
  1426. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1427. pdflags:=pdflags or pd_global;
  1428. { Add the correct flag }
  1429. aktprocdef.procoptions:=aktprocdef.procoptions+proc_direcdata[p].pooption;
  1430. { Call the handler }
  1431. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1432. proc_direcdata[p].handler{$ifdef FPCPROCVAR}(){$endif};
  1433. end;
  1434. procedure handle_calling_convention(sym:tprocsym;def:tabstractprocdef);
  1435. begin
  1436. { set the default calling convention }
  1437. if def.proccalloption=pocall_none then
  1438. def.proccalloption:=aktdefproccall;
  1439. case def.proccalloption of
  1440. pocall_cdecl :
  1441. begin
  1442. { use popstack and save std registers }
  1443. include(def.procoptions,po_clearstack);
  1444. include(def.procoptions,po_savestdregs);
  1445. { set mangledname }
  1446. if (def.deftype=procdef) then
  1447. begin
  1448. if not tprocdef(def).has_mangledname then
  1449. begin
  1450. if assigned(tprocdef(def)._class) then
  1451. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def)._class.objrealname^+'_'+sym.realname)
  1452. else
  1453. tprocdef(def).setmangledname(target_info.Cprefix+sym.realname);
  1454. end;
  1455. if not assigned(tprocdef(def).parast) then
  1456. internalerror(200110234);
  1457. { do not copy on local !! }
  1458. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1459. { check C cdecl para types }
  1460. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1461. { Adjust alignment to match cdecl or stdcall }
  1462. tprocdef(def).parast.dataalignment:=std_param_align;
  1463. end;
  1464. end;
  1465. pocall_cppdecl :
  1466. begin
  1467. if not assigned(sym) then
  1468. internalerror(200110231);
  1469. { use popstack and save std registers }
  1470. include(def.procoptions,po_clearstack);
  1471. include(def.procoptions,po_savestdregs);
  1472. { set mangledname }
  1473. if (def.deftype=procdef) then
  1474. begin
  1475. if not tprocdef(def).has_mangledname then
  1476. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def).cplusplusmangledname);
  1477. if not assigned(tprocdef(def).parast) then
  1478. internalerror(200110235);
  1479. { do not copy on local !! }
  1480. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1481. { check C cdecl para types }
  1482. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1483. { Adjust alignment to match cdecl or stdcall }
  1484. tprocdef(def).parast.dataalignment:=std_param_align;
  1485. end;
  1486. end;
  1487. pocall_stdcall :
  1488. begin
  1489. include(def.procoptions,po_savestdregs);
  1490. if (def.deftype=procdef) then
  1491. begin
  1492. if not assigned(tprocdef(def).parast) then
  1493. internalerror(200110236);
  1494. { Adjust alignment to match cdecl or stdcall }
  1495. tprocdef(def).parast.dataalignment:=std_param_align;
  1496. end;
  1497. end;
  1498. pocall_safecall :
  1499. begin
  1500. include(def.procoptions,po_savestdregs);
  1501. end;
  1502. pocall_compilerproc :
  1503. begin
  1504. if (not assigned(sym)) or
  1505. (def.deftype<>procdef) then
  1506. internalerror(200110232);
  1507. tprocdef(def).setmangledname(lower(sym.name));
  1508. end;
  1509. pocall_pascal :
  1510. begin
  1511. include(def.procoptions,po_leftright);
  1512. end;
  1513. pocall_register :
  1514. begin
  1515. Message1(parser_w_proc_directive_ignored,'REGISTER');
  1516. end;
  1517. pocall_far16 :
  1518. begin
  1519. { Temporary stub, must be rewritten to support OS/2 far16 }
  1520. Message1(parser_w_proc_directive_ignored,'FAR16');
  1521. end;
  1522. pocall_system :
  1523. begin
  1524. include(def.procoptions,po_clearstack);
  1525. if (not assigned(sym)) or
  1526. (def.deftype<>procdef) then
  1527. internalerror(200110233);
  1528. if not tprocdef(def).has_mangledname then
  1529. tprocdef(def).setmangledname(sym.realname);
  1530. end;
  1531. pocall_palmossyscall :
  1532. begin
  1533. { use popstack and save std registers }
  1534. include(def.procoptions,po_clearstack);
  1535. include(def.procoptions,po_savestdregs);
  1536. if (def.deftype=procdef) then
  1537. begin
  1538. if not assigned(tprocdef(def).parast) then
  1539. internalerror(200110236);
  1540. { do not copy on local !! }
  1541. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1542. { Adjust positions of args for cdecl or stdcall }
  1543. tprocdef(def).parast.dataalignment:=std_param_align;
  1544. end;
  1545. end;
  1546. pocall_inline :
  1547. begin
  1548. if not(cs_support_inline in aktmoduleswitches) then
  1549. begin
  1550. Message(parser_e_proc_inline_not_supported);
  1551. def.proccalloption:=pocall_fpccall;
  1552. end;
  1553. end;
  1554. end;
  1555. { add mangledname to external list }
  1556. if (def.deftype=procdef) and
  1557. (po_external in def.procoptions) and
  1558. target_info.DllScanSupported then
  1559. current_module.externals.insert(tExternalsItem.create(tprocdef(def).mangledname));
  1560. end;
  1561. procedure calc_parasymtable_addresses(def:tprocdef);
  1562. var
  1563. lastps,
  1564. highps,ps : tsym;
  1565. st : tsymtable;
  1566. begin
  1567. st:=def.parast;
  1568. if po_leftright in def.procoptions then
  1569. begin
  1570. { pushed in reversed order, left to right }
  1571. highps:=nil;
  1572. lastps:=nil;
  1573. while assigned(st.symindex.first) and (lastps<>tsym(st.symindex.first)) do
  1574. begin
  1575. ps:=tsym(st.symindex.first);
  1576. while assigned(ps.indexnext) and (tsym(ps.indexnext)<>lastps) do
  1577. ps:=tsym(ps.indexnext);
  1578. if ps.typ=varsym then
  1579. begin
  1580. { Wait with inserting the high value, it needs to be inserted
  1581. after the corresponding parameter }
  1582. if Copy(ps.name,1,4)='high' then
  1583. highps:=ps
  1584. else
  1585. begin
  1586. st.insertvardata(ps);
  1587. { add also the high tree if it was saved }
  1588. if assigned(highps) then
  1589. begin
  1590. st.insertvardata(highps);
  1591. highps:=nil;
  1592. end;
  1593. end;
  1594. end;
  1595. lastps:=ps;
  1596. end;
  1597. if assigned(highps) then
  1598. internalerror(200208257);
  1599. end
  1600. else
  1601. begin
  1602. { pushed in normal order, right to left }
  1603. ps:=tsym(st.symindex.first);
  1604. while assigned(ps) do
  1605. begin
  1606. if ps.typ=varsym then
  1607. st.insertvardata(ps);
  1608. ps:=tsym(ps.indexnext);
  1609. end;
  1610. end;
  1611. end;
  1612. procedure parse_proc_directives(var pdflags:word);
  1613. {
  1614. Parse the procedure directives. It does not matter if procedure directives
  1615. are written using ;procdir; or ['procdir'] syntax.
  1616. }
  1617. var
  1618. res : boolean;
  1619. begin
  1620. while token in [_ID,_LECKKLAMMER] do
  1621. begin
  1622. if try_to_consume(_LECKKLAMMER) then
  1623. begin
  1624. repeat
  1625. parse_proc_direc(pdflags);
  1626. until not try_to_consume(_COMMA);
  1627. consume(_RECKKLAMMER);
  1628. { we always expect at least '[];' }
  1629. res:=true;
  1630. end
  1631. else
  1632. begin
  1633. res:=parse_proc_direc(pdflags);
  1634. end;
  1635. { A procedure directive normally followed by a semicolon, but in
  1636. a const section we should stop when _EQUAL is found }
  1637. if res then
  1638. begin
  1639. if (block_type=bt_const) and
  1640. (token=_EQUAL) then
  1641. break;
  1642. { support procedure proc;stdcall export; in Delphi mode only }
  1643. if not((m_delphi in aktmodeswitches) and
  1644. is_proc_directive(token)) then
  1645. consume(_SEMICOLON);
  1646. end
  1647. else
  1648. break;
  1649. end;
  1650. handle_calling_convention(aktprocsym,aktprocdef);
  1651. { calculate addresses in parasymtable }
  1652. if aktprocdef.deftype=procdef then
  1653. calc_parasymtable_addresses(aktprocdef);
  1654. end;
  1655. procedure parse_var_proc_directives(var sym : tsym);
  1656. var
  1657. pdflags : word;
  1658. oldsym : tprocsym;
  1659. olddef : tprocdef;
  1660. pd : tabstractprocdef;
  1661. begin
  1662. oldsym:=aktprocsym;
  1663. olddef:=aktprocdef;
  1664. pdflags:=pd_procvar;
  1665. { we create a temporary aktprocsym to read the directives }
  1666. aktprocsym:=tprocsym.create(sym.name);
  1667. case sym.typ of
  1668. varsym :
  1669. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1670. typedconstsym :
  1671. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1672. typesym :
  1673. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1674. else
  1675. internalerror(994932432);
  1676. end;
  1677. if pd.deftype<>procvardef then
  1678. internalerror(994932433);
  1679. tabstractprocdef(aktprocdef):=pd;
  1680. { names should never be used anyway }
  1681. inc(lexlevel);
  1682. parse_proc_directives(pdflags);
  1683. dec(lexlevel);
  1684. aktprocsym.free;
  1685. aktprocsym:=oldsym;
  1686. aktprocdef:=olddef;
  1687. end;
  1688. procedure parse_object_proc_directives(var sym : tprocsym);
  1689. var
  1690. pdflags : word;
  1691. begin
  1692. pdflags:=pd_object;
  1693. inc(lexlevel);
  1694. parse_proc_directives(pdflags);
  1695. dec(lexlevel);
  1696. if (po_containsself in aktprocdef.procoptions) and
  1697. (([po_msgstr,po_msgint]*aktprocdef.procoptions)=[]) then
  1698. Message(parser_e_self_in_non_message_handler);
  1699. end;
  1700. function proc_add_definition(aprocsym:tprocsym;var aprocdef : tprocdef) : boolean;
  1701. {
  1702. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1703. forwarddef is found and reused it returns true
  1704. }
  1705. var
  1706. hd : tprocdef;
  1707. ad,fd : tsym;
  1708. forwardfound : boolean;
  1709. i : cardinal;
  1710. begin
  1711. forwardfound:=false;
  1712. { check overloaded functions if the same function already exists }
  1713. for i:=1 to aprocsym.procdef_count do
  1714. begin
  1715. hd:=aprocsym.procdef[i];
  1716. { Skip overloaded definitions that are declared in other
  1717. units }
  1718. if hd.procsym<>aprocsym then
  1719. continue;
  1720. { check the parameters, for delphi/tp it is possible to
  1721. leave the parameters away in the implementation (forwarddef=false).
  1722. But for an overload declared function this is not allowed }
  1723. if { check if empty implementation arguments match is allowed }
  1724. (
  1725. not(m_repeat_forward in aktmodeswitches) and
  1726. not(aprocdef.forwarddef) and
  1727. (aprocdef.maxparacount=0) and
  1728. not(po_overload in hd.procoptions)
  1729. ) or
  1730. { check arguments }
  1731. (
  1732. (compare_paras(aprocdef.para,hd.para,cp_none,false)>=te_equal) and
  1733. { for operators equal_paras is not enough !! }
  1734. ((aprocdef.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1735. equal_defs(hd.rettype.def,aprocdef.rettype.def))
  1736. ) then
  1737. begin
  1738. { Check if we've found the forwarddef, if found then
  1739. we need to update the forward def with the current
  1740. implementation settings }
  1741. if hd.forwarddef then
  1742. begin
  1743. forwardfound:=true;
  1744. { Check if the procedure type and return type are correct,
  1745. also the parameters must match also with the type }
  1746. if (hd.proctypeoption<>aprocdef.proctypeoption) or
  1747. (
  1748. (m_repeat_forward in aktmodeswitches) and
  1749. (not((aprocdef.maxparacount=0) or
  1750. (compare_paras(aprocdef.para,hd.para,cp_all,false)>=te_equal)))
  1751. ) or
  1752. (
  1753. ((m_repeat_forward in aktmodeswitches) or
  1754. not(is_void(aprocdef.rettype.def))) and
  1755. (not equal_defs(hd.rettype.def,aprocdef.rettype.def))) then
  1756. begin
  1757. MessagePos1(aprocdef.fileinfo,parser_e_header_dont_match_forward,
  1758. aprocdef.fullprocname);
  1759. break;
  1760. end;
  1761. { Check if both are declared forward }
  1762. if hd.forwarddef and aprocdef.forwarddef then
  1763. begin
  1764. MessagePos1(aprocdef.fileinfo,parser_e_function_already_declared_public_forward,
  1765. aprocdef.fullprocname);
  1766. end;
  1767. { internconst or internproc only need to be defined once }
  1768. if (hd.proccalloption=pocall_internproc) then
  1769. aprocdef.proccalloption:=hd.proccalloption
  1770. else
  1771. if (aprocdef.proccalloption=pocall_internproc) then
  1772. hd.proccalloption:=aprocdef.proccalloption;
  1773. if (po_internconst in hd.procoptions) then
  1774. include(aprocdef.procoptions,po_internconst)
  1775. else if (po_internconst in aprocdef.procoptions) then
  1776. include(hd.procoptions,po_internconst);
  1777. { Check calling convention }
  1778. if (hd.proccalloption<>aprocdef.proccalloption) then
  1779. begin
  1780. { For delphi check if the current implementation has no proccalloption, then
  1781. take the options from the interface }
  1782. if not(m_delphi in aktmodeswitches) or
  1783. (aprocdef.proccalloption<>pocall_none) then
  1784. MessagePos(aprocdef.fileinfo,parser_e_call_convention_dont_match_forward);
  1785. { restore interface settings }
  1786. aprocdef.proccalloption:=hd.proccalloption;
  1787. if hd.has_mangledname then
  1788. aprocdef.setmangledname(hd.mangledname);
  1789. end;
  1790. { Check manglednames }
  1791. if (m_repeat_forward in aktmodeswitches) or
  1792. aprocdef.haspara then
  1793. begin
  1794. { If mangled names are equal then they have the same amount of arguments }
  1795. { We can check the names of the arguments }
  1796. { both symtables are in the same order from left to right }
  1797. ad:=tsym(hd.parast.symindex.first);
  1798. fd:=tsym(aprocdef.parast.symindex.first);
  1799. repeat
  1800. { skip default parameter constsyms }
  1801. while assigned(ad) and (ad.typ<>varsym) do
  1802. ad:=tsym(ad.indexnext);
  1803. while assigned(fd) and (fd.typ<>varsym) do
  1804. fd:=tsym(fd.indexnext);
  1805. { stop when one of the two lists is at the end }
  1806. if not assigned(ad) or not assigned(fd) then
  1807. break;
  1808. if (ad.name<>fd.name) then
  1809. begin
  1810. MessagePos3(aprocdef.fileinfo,parser_e_header_different_var_names,
  1811. aprocsym.name,ad.name,fd.name);
  1812. break;
  1813. end;
  1814. ad:=tsym(ad.indexnext);
  1815. fd:=tsym(fd.indexnext);
  1816. until false;
  1817. if assigned(ad) or assigned(fd) then
  1818. internalerror(200204178);
  1819. end;
  1820. { Everything is checked, now we can update the forward declaration
  1821. with the new data from the implementation }
  1822. hd.forwarddef:=aprocdef.forwarddef;
  1823. hd.hasforward:=true;
  1824. hd.parast.address_fixup:=aprocdef.parast.address_fixup;
  1825. hd.procoptions:=hd.procoptions+aprocdef.procoptions;
  1826. if hd.extnumber=65535 then
  1827. hd.extnumber:=aprocdef.extnumber;
  1828. while not aprocdef.aliasnames.empty do
  1829. hd.aliasnames.insert(aprocdef.aliasnames.getfirst);
  1830. { update mangledname if the implementation has a fixed mangledname set }
  1831. if aprocdef.has_mangledname then
  1832. begin
  1833. { rename also asmsymbol first, because the name can already be used }
  1834. objectlibrary.renameasmsymbol(hd.mangledname,aprocdef.mangledname);
  1835. hd.setmangledname(aprocdef.mangledname);
  1836. end;
  1837. { for compilerproc defines we need to rename and update the
  1838. symbolname to lowercase }
  1839. if (aprocdef.proccalloption=pocall_compilerproc) then
  1840. begin
  1841. { rename to lowercase so users can't access it }
  1842. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  1843. { also update the realname that is stored in the ppu }
  1844. stringdispose(aprocsym._realname);
  1845. aprocsym._realname:=stringdup('$'+aprocsym.name);
  1846. { the mangeled name is already changed by the pd_compilerproc }
  1847. { handler. It must be done immediately because if we have a }
  1848. { call to a compilerproc before it's implementation is }
  1849. { encountered, it must already use the new mangled name (JM) }
  1850. end;
  1851. { return the forwarddef }
  1852. aprocdef:=hd;
  1853. end
  1854. else
  1855. begin
  1856. { abstract methods aren't forward defined, but this }
  1857. { needs another error message }
  1858. if (po_abstractmethod in hd.procoptions) then
  1859. MessagePos(aprocdef.fileinfo,parser_e_abstract_no_definition)
  1860. else
  1861. MessagePos(aprocdef.fileinfo,parser_e_overloaded_have_same_parameters);
  1862. end;
  1863. { we found one proc with the same arguments, there are no others
  1864. so we can stop }
  1865. break;
  1866. end;
  1867. { check for allowing overload directive }
  1868. if not(m_fpc in aktmodeswitches) then
  1869. begin
  1870. { overload directive turns on overloading }
  1871. if ((po_overload in aprocdef.procoptions) or
  1872. (po_overload in hd.procoptions)) then
  1873. begin
  1874. { check if all procs have overloading, but not if the proc was
  1875. already declared forward, then the check is already done }
  1876. if not(hd.hasforward or
  1877. (aprocdef.forwarddef<>hd.forwarddef) or
  1878. ((po_overload in aprocdef.procoptions) and
  1879. (po_overload in hd.procoptions))) then
  1880. begin
  1881. MessagePos1(aprocdef.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  1882. break;
  1883. end;
  1884. end
  1885. else
  1886. begin
  1887. if not(hd.forwarddef) then
  1888. begin
  1889. MessagePos(aprocdef.fileinfo,parser_e_procedure_overloading_is_off);
  1890. break;
  1891. end;
  1892. end;
  1893. end; { equal arguments }
  1894. end;
  1895. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  1896. list }
  1897. if not forwardfound then
  1898. begin
  1899. aprocsym.addprocdef(aprocdef);
  1900. { add overloadnumber for unique naming, the overloadcount is
  1901. counted per module and 0 for the first procedure }
  1902. aprocdef.overloadnumber:=aprocsym.overloadcount;
  1903. inc(aprocsym.overloadcount);
  1904. end;
  1905. { insert otsym only in the right symtable }
  1906. if ((procinfo.flags and pi_operator)<>0) and
  1907. assigned(otsym) then
  1908. begin
  1909. if not parse_only then
  1910. begin
  1911. if paramanager.ret_in_param(aprocdef.rettype.def,aprocdef.proccalloption) then
  1912. begin
  1913. aprocdef.parast.insert(otsym);
  1914. { this allows to read the funcretoffset }
  1915. otsym.address:=-4;
  1916. otsym.varspez:=vs_var;
  1917. end
  1918. else
  1919. begin
  1920. aprocdef.localst.insert(otsym);
  1921. aprocdef.localst.insertvardata(otsym);
  1922. end;
  1923. end
  1924. else
  1925. begin
  1926. { this is not required anymore }
  1927. otsym.free;
  1928. otsym:=nil;
  1929. end;
  1930. end;
  1931. paramanager.create_param_loc_info(aprocdef);
  1932. proc_add_definition:=forwardfound;
  1933. end;
  1934. end.
  1935. {
  1936. $Log$
  1937. Revision 1.84 2002-11-29 22:31:19 carl
  1938. + unimplemented hint directive added
  1939. * hint directive parsing implemented
  1940. * warning on these directives
  1941. Revision 1.83 2002/11/27 02:35:28 peter
  1942. * fixed typo in method comparing
  1943. Revision 1.82 2002/11/25 17:43:21 peter
  1944. * splitted defbase in defutil,symutil,defcmp
  1945. * merged isconvertable and is_equal into compare_defs(_ext)
  1946. * made operator search faster by walking the list only once
  1947. Revision 1.81 2002/11/18 17:31:58 peter
  1948. * pass proccalloption to ret_in_xxx and push_xxx functions
  1949. Revision 1.80 2002/11/17 16:31:56 carl
  1950. * memory optimization (3-4%) : cleanup of tai fields,
  1951. cleanup of tdef and tsym fields.
  1952. * make it work for m68k
  1953. Revision 1.79 2002/11/16 14:20:50 peter
  1954. * fix infinite loop in pd_inline
  1955. Revision 1.78 2002/11/15 01:58:53 peter
  1956. * merged changes from 1.0.7 up to 04-11
  1957. - -V option for generating bug report tracing
  1958. - more tracing for option parsing
  1959. - errors for cdecl and high()
  1960. - win32 import stabs
  1961. - win32 records<=8 are returned in eax:edx (turned off by default)
  1962. - heaptrc update
  1963. - more info for temp management in .s file with EXTDEBUG
  1964. Revision 1.77 2002/10/06 15:09:12 peter
  1965. * variant:=nil supported
  1966. Revision 1.76 2002/09/27 21:13:29 carl
  1967. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1968. Revision 1.75 2002/09/16 14:11:13 peter
  1969. * add argument to equal_paras() to support default values or not
  1970. Revision 1.74 2002/09/10 16:27:28 peter
  1971. * don't insert parast in symtablestack, because typesyms should not be
  1972. searched in the the parast
  1973. Revision 1.73 2002/09/09 19:39:07 peter
  1974. * check return type for forwarddefs also not delphi mode when
  1975. the type is not void
  1976. Revision 1.72 2002/09/09 17:34:15 peter
  1977. * tdicationary.replace added to replace and item in a dictionary. This
  1978. is only allowed for the same name
  1979. * varsyms are inserted in symtable before the types are parsed. This
  1980. fixes the long standing "var longint : longint" bug
  1981. - consume_idlist and idstringlist removed. The loops are inserted
  1982. at the callers place and uses the symtable for duplicate id checking
  1983. Revision 1.71 2002/09/07 15:25:06 peter
  1984. * old logs removed and tabs fixed
  1985. Revision 1.70 2002/09/03 16:26:27 daniel
  1986. * Make Tprocdef.defs protected
  1987. Revision 1.69 2002/09/01 12:11:33 peter
  1988. * calc param_offset after parameters are read, because the calculation
  1989. depends on po_containself
  1990. Revision 1.68 2002/08/25 19:25:20 peter
  1991. * sym.insert_in_data removed
  1992. * symtable.insertvardata/insertconstdata added
  1993. * removed insert_in_data call from symtable.insert, it needs to be
  1994. called separatly. This allows to deref the address calculation
  1995. * procedures now calculate the parast addresses after the procedure
  1996. directives are parsed. This fixes the cdecl parast problem
  1997. * push_addr_param has an extra argument that specifies if cdecl is used
  1998. or not
  1999. Revision 1.67 2002/08/25 11:33:06 peter
  2000. * also check the paratypes when a forward was found
  2001. Revision 1.66 2002/08/19 19:36:44 peter
  2002. * More fixes for cross unit inlining, all tnodes are now implemented
  2003. * Moved pocall_internconst to po_internconst because it is not a
  2004. calling type at all and it conflicted when inlining of these small
  2005. functions was requested
  2006. Revision 1.65 2002/08/18 20:06:24 peter
  2007. * inlining is now also allowed in interface
  2008. * renamed write/load to ppuwrite/ppuload
  2009. * tnode storing in ppu
  2010. * nld,ncon,nbas are already updated for storing in ppu
  2011. Revision 1.64 2002/08/17 09:23:39 florian
  2012. * first part of procinfo rewrite
  2013. Revision 1.63 2002/08/11 14:32:27 peter
  2014. * renamed current_library to objectlibrary
  2015. Revision 1.62 2002/08/11 13:24:12 peter
  2016. * saving of asmsymbols in ppu supported
  2017. * asmsymbollist global is removed and moved into a new class
  2018. tasmlibrarydata that will hold the info of a .a file which
  2019. corresponds with a single module. Added librarydata to tmodule
  2020. to keep the library info stored for the module. In the future the
  2021. objectfiles will also be stored to the tasmlibrarydata class
  2022. * all getlabel/newasmsymbol and friends are moved to the new class
  2023. Revision 1.61 2002/07/26 21:15:40 florian
  2024. * rewrote the system handling
  2025. Revision 1.60 2002/07/20 11:57:55 florian
  2026. * types.pas renamed to defbase.pas because D6 contains a types
  2027. unit so this would conflicts if D6 programms are compiled
  2028. + Willamette/SSE2 instructions to assembler added
  2029. Revision 1.59 2002/07/11 14:41:28 florian
  2030. * start of the new generic parameter handling
  2031. Revision 1.58 2002/07/01 18:46:25 peter
  2032. * internal linker
  2033. * reorganized aasm layer
  2034. Revision 1.57 2002/05/18 13:34:12 peter
  2035. * readded missing revisions
  2036. Revision 1.56 2002/05/16 19:46:42 carl
  2037. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2038. + try to fix temp allocation (still in ifdef)
  2039. + generic constructor calls
  2040. + start of tassembler / tmodulebase class cleanup
  2041. Revision 1.54 2002/05/12 16:53:08 peter
  2042. * moved entry and exitcode to ncgutil and cgobj
  2043. * foreach gets extra argument for passing local data to the
  2044. iterator function
  2045. * -CR checks also class typecasts at runtime by changing them
  2046. into as
  2047. * fixed compiler to cycle with the -CR option
  2048. * fixed stabs with elf writer, finally the global variables can
  2049. be watched
  2050. * removed a lot of routines from cga unit and replaced them by
  2051. calls to cgobj
  2052. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2053. u32bit then the other is typecasted also to u32bit without giving
  2054. a rangecheck warning/error.
  2055. * fixed pascal calling method with reversing also the high tree in
  2056. the parast, detected by tcalcst3 test
  2057. Revision 1.53 2002/04/21 19:02:04 peter
  2058. * removed newn and disposen nodes, the code is now directly
  2059. inlined from pexpr
  2060. * -an option that will write the secondpass nodes to the .s file, this
  2061. requires EXTDEBUG define to actually write the info
  2062. * fixed various internal errors and crashes due recent code changes
  2063. Revision 1.52 2002/04/20 21:32:24 carl
  2064. + generic FPC_CHECKPOINTER
  2065. + first parameter offset in stack now portable
  2066. * rename some constants
  2067. + move some cpu stuff to other units
  2068. - remove unused constents
  2069. * fix stacksize for some targets
  2070. * fix generic size problems which depend now on EXTEND_SIZE constant
  2071. Revision 1.51 2002/04/20 15:27:05 carl
  2072. - remove ifdef i386 define
  2073. Revision 1.50 2002/04/19 15:46:02 peter
  2074. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  2075. in most cases and not written to the ppu
  2076. * add mangeledname_prefix() routine to generate the prefix of
  2077. manglednames depending on the current procedure, object and module
  2078. * removed static procprefix since the mangledname is now build only
  2079. on demand from tprocdef.mangledname
  2080. }