pdecsub.pas 82 KB

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