pdecsub.pas 81 KB

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