pdecsub.pas 80 KB

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