pdecsub.pas 83 KB

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