pdecsub.pas 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  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,aktprocdef.proccalloption) 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) 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. if token=_CCHAR then
  769. consume(_CCHAR)
  770. else
  771. consume(_CSTRING);
  772. { we don't need anything else }
  773. aktprocdef.forwarddef:=false;
  774. end;
  775. procedure pd_inline;
  776. var
  777. hp : tparaitem;
  778. begin
  779. { check if there is an array of const }
  780. hp:=tparaitem(aktprocdef.para.first);
  781. while assigned(hp) do
  782. begin
  783. if assigned(hp.paratype.def) and
  784. (hp.paratype.def.deftype=arraydef) then
  785. begin
  786. with tarraydef(hp.paratype.def) do
  787. if IsVariant or IsConstructor {or IsArrayOfConst} then
  788. begin
  789. Message1(parser_w_not_supported_for_inline,'array of const');
  790. Message(parser_w_inlining_disabled);
  791. aktprocdef.proccalloption:=pocall_fpccall;
  792. end;
  793. end;
  794. hp:=tparaitem(hp.next);
  795. end;
  796. end;
  797. procedure pd_intern;
  798. begin
  799. consume(_COLON);
  800. aktprocdef.extnumber:=get_intconst;
  801. end;
  802. procedure pd_interrupt;
  803. begin
  804. if lexlevel<>normal_function_level then
  805. Message(parser_e_dont_nest_interrupt);
  806. end;
  807. procedure pd_abstract;
  808. begin
  809. if (po_virtualmethod in aktprocdef.procoptions) then
  810. include(aktprocdef.procoptions,po_abstractmethod)
  811. else
  812. Message(parser_e_only_virtual_methods_abstract);
  813. { the method is defined }
  814. aktprocdef.forwarddef:=false;
  815. end;
  816. procedure pd_virtual;
  817. {$ifdef WITHDMT}
  818. var
  819. pt : tnode;
  820. {$endif WITHDMT}
  821. begin
  822. if (aktprocdef.proctypeoption=potype_constructor) and
  823. is_object(aktprocdef._class) then
  824. Message(parser_e_constructor_cannot_be_not_virtual);
  825. {$ifdef WITHDMT}
  826. if is_object(aktprocdef._class) and
  827. (token<>_SEMICOLON) then
  828. begin
  829. { any type of parameter is allowed here! }
  830. pt:=comp_expr(true);
  831. if is_constintnode(pt) then
  832. begin
  833. include(aktprocdef.procoptions,po_msgint);
  834. aktprocdef.messageinf.i:=pt^.value;
  835. end
  836. else
  837. Message(parser_e_ill_msg_expr);
  838. disposetree(pt);
  839. end;
  840. {$endif WITHDMT}
  841. end;
  842. procedure pd_static;
  843. begin
  844. if (cs_static_keyword in aktmoduleswitches) then
  845. begin
  846. include(aktprocsym.symoptions,sp_static);
  847. include(aktprocdef.procoptions,po_staticmethod);
  848. end;
  849. end;
  850. procedure pd_override;
  851. begin
  852. if not(is_class_or_interface(aktprocdef._class)) then
  853. Message(parser_e_no_object_override);
  854. end;
  855. procedure pd_overload;
  856. begin
  857. include(aktprocsym.symoptions,sp_has_overloaded);
  858. end;
  859. procedure pd_message;
  860. var
  861. pt : tnode;
  862. begin
  863. { check parameter type }
  864. if not(po_containsself in aktprocdef.procoptions) and
  865. ((aktprocdef.minparacount<>1) or
  866. (aktprocdef.maxparacount<>1) or
  867. (TParaItem(aktprocdef.Para.first).paratyp<>vs_var)) then
  868. Message(parser_e_ill_msg_param);
  869. pt:=comp_expr(true);
  870. if pt.nodetype=stringconstn then
  871. begin
  872. include(aktprocdef.procoptions,po_msgstr);
  873. aktprocdef.messageinf.str:=strnew(tstringconstnode(pt).value_str);
  874. end
  875. else
  876. if is_constintnode(pt) then
  877. begin
  878. include(aktprocdef.procoptions,po_msgint);
  879. aktprocdef.messageinf.i:=tordconstnode(pt).value;
  880. end
  881. else
  882. Message(parser_e_ill_msg_expr);
  883. pt.free;
  884. end;
  885. procedure pd_reintroduce;
  886. begin
  887. Message1(parser_w_proc_directive_ignored,'REINTRODUCE');
  888. end;
  889. procedure pd_syscall;
  890. begin
  891. aktprocdef.forwarddef:=false;
  892. aktprocdef.extnumber:=get_intconst;
  893. end;
  894. procedure pd_external;
  895. {
  896. If import_dll=nil the procedure is assumed to be in another
  897. object file. In that object file it should have the name to
  898. which import_name is pointing to. Otherwise, the procedure is
  899. assumed to be in the DLL to which import_dll is pointing to. In
  900. that case either import_nr<>0 or import_name<>nil is true, so
  901. the procedure is either imported by number or by name. (DM)
  902. }
  903. var
  904. pd : tprocdef;
  905. import_dll,
  906. import_name : string;
  907. import_nr : word;
  908. begin
  909. aktprocdef.forwarddef:=false;
  910. { forbid local external procedures }
  911. if lexlevel>normal_function_level then
  912. Message(parser_e_no_local_external);
  913. { If the procedure should be imported from a DLL, a constant string follows.
  914. This isn't really correct, an contant string expression follows
  915. so we check if an semicolon follows, else a string constant have to
  916. follow (FK) }
  917. import_nr:=0;
  918. import_name:='';
  919. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  920. begin
  921. import_dll:=get_stringconst;
  922. if (idtoken=_NAME) then
  923. begin
  924. consume(_NAME);
  925. import_name:=get_stringconst;
  926. end;
  927. if (idtoken=_INDEX) then
  928. begin
  929. {After the word index follows the index number in the DLL.}
  930. consume(_INDEX);
  931. import_nr:=get_intconst;
  932. end;
  933. { default is to used the realname of the procedure }
  934. if (import_nr=0) and (import_name='') then
  935. import_name:=aktprocsym.realname;
  936. { create importlib if not already done }
  937. if not(current_module.uses_imports) then
  938. begin
  939. current_module.uses_imports:=true;
  940. importlib.preparelib(current_module.modulename^);
  941. end;
  942. if not(m_repeat_forward in aktmodeswitches) then
  943. begin
  944. { we can only have one overloaded here ! }
  945. if aktprocsym.procdef_count>1 then
  946. pd:=aktprocsym.procdef[2]
  947. else
  948. pd:=aktprocdef;
  949. end
  950. else
  951. pd:=aktprocdef;
  952. importlib.importproceduredef(pd,import_dll,import_nr,import_name);
  953. end
  954. else
  955. begin
  956. if (idtoken=_NAME) then
  957. begin
  958. consume(_NAME);
  959. import_name:=get_stringconst;
  960. aktprocdef.setmangledname(import_name);
  961. end;
  962. end;
  963. end;
  964. type
  965. pd_handler=procedure;
  966. proc_dir_rec=record
  967. idtok : ttoken;
  968. pd_flags : longint;
  969. handler : pd_handler;
  970. pocall : tproccalloption;
  971. pooption : tprocoptions;
  972. mutexclpocall : tproccalloptions;
  973. mutexclpotype : tproctypeoptions;
  974. mutexclpo : tprocoptions;
  975. end;
  976. const
  977. {Should contain the number of procedure directives we support.}
  978. num_proc_directives=36;
  979. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  980. (
  981. (
  982. idtok:_ABSTRACT;
  983. pd_flags : pd_interface+pd_object+pd_notobjintf;
  984. handler : {$ifdef FPCPROCVAR}@{$endif}pd_abstract;
  985. pocall : pocall_none;
  986. pooption : [po_abstractmethod];
  987. mutexclpocall : [pocall_internproc,pocall_inline];
  988. mutexclpotype : [potype_constructor,potype_destructor];
  989. mutexclpo : [po_exports,po_interrupt,po_external]
  990. ),(
  991. idtok:_ALIAS;
  992. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  993. handler : {$ifdef FPCPROCVAR}@{$endif}pd_alias;
  994. pocall : pocall_none;
  995. pooption : [];
  996. mutexclpocall : [pocall_inline];
  997. mutexclpotype : [];
  998. mutexclpo : [po_external]
  999. ),(
  1000. idtok:_ASMNAME;
  1001. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1002. handler : {$ifdef FPCPROCVAR}@{$endif}pd_asmname;
  1003. pocall : pocall_cdecl;
  1004. pooption : [po_external];
  1005. mutexclpocall : [pocall_internproc,pocall_inline];
  1006. mutexclpotype : [];
  1007. mutexclpo : [po_external]
  1008. ),(
  1009. idtok:_ASSEMBLER;
  1010. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1011. handler : nil;
  1012. pocall : pocall_none;
  1013. pooption : [po_assembler];
  1014. mutexclpocall : [];
  1015. mutexclpotype : [];
  1016. mutexclpo : [po_external]
  1017. ),(
  1018. idtok:_CDECL;
  1019. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1020. handler : nil;
  1021. pocall : pocall_cdecl;
  1022. pooption : [];
  1023. mutexclpocall : [];
  1024. mutexclpotype : [];
  1025. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1026. ),(
  1027. idtok:_DYNAMIC;
  1028. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1029. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1030. pocall : pocall_none;
  1031. pooption : [po_virtualmethod];
  1032. mutexclpocall : [pocall_internproc,pocall_inline];
  1033. mutexclpotype : [];
  1034. mutexclpo : [po_exports,po_interrupt,po_external]
  1035. ),(
  1036. idtok:_EXPORT;
  1037. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??}+pd_notobjintf;
  1038. handler : {$ifdef FPCPROCVAR}@{$endif}pd_export;
  1039. pocall : pocall_none;
  1040. pooption : [po_exports];
  1041. mutexclpocall : [pocall_internproc,pocall_inline];
  1042. mutexclpotype : [];
  1043. mutexclpo : [po_external,po_interrupt]
  1044. ),(
  1045. idtok:_EXTERNAL;
  1046. pd_flags : pd_implemen+pd_interface+pd_notobjintf;
  1047. handler : {$ifdef FPCPROCVAR}@{$endif}pd_external;
  1048. pocall : pocall_none;
  1049. pooption : [po_external];
  1050. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  1051. mutexclpotype : [];
  1052. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1053. ),(
  1054. idtok:_FAR;
  1055. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar+pd_notobjintf;
  1056. handler : {$ifdef FPCPROCVAR}@{$endif}pd_far;
  1057. pocall : pocall_none;
  1058. pooption : [];
  1059. mutexclpocall : [pocall_internproc,pocall_inline];
  1060. mutexclpotype : [];
  1061. mutexclpo : []
  1062. ),(
  1063. idtok:_FAR16;
  1064. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1065. handler : nil;
  1066. pocall : pocall_far16;
  1067. pooption : [];
  1068. mutexclpocall : [];
  1069. mutexclpotype : [];
  1070. mutexclpo : [po_external,po_leftright]
  1071. ),(
  1072. idtok:_FORWARD;
  1073. pd_flags : pd_implemen+pd_notobjintf;
  1074. handler : {$ifdef FPCPROCVAR}@{$endif}pd_forward;
  1075. pocall : pocall_none;
  1076. pooption : [];
  1077. mutexclpocall : [pocall_internproc,pocall_inline];
  1078. mutexclpotype : [];
  1079. mutexclpo : [po_external]
  1080. ),(
  1081. idtok:_FPCCALL;
  1082. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1083. handler : nil;
  1084. pocall : pocall_fpccall;
  1085. pooption : [];
  1086. mutexclpocall : [];
  1087. mutexclpotype : [];
  1088. mutexclpo : [po_leftright]
  1089. ),(
  1090. idtok:_INLINE;
  1091. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1092. handler : {$ifdef FPCPROCVAR}@{$endif}pd_inline;
  1093. pocall : pocall_inline;
  1094. pooption : [];
  1095. mutexclpocall : [];
  1096. mutexclpotype : [potype_constructor,potype_destructor];
  1097. mutexclpo : [po_exports,po_external,po_interrupt]
  1098. ),(
  1099. idtok:_INTERNCONST;
  1100. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1101. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1102. pocall : pocall_none;
  1103. pooption : [po_internconst];
  1104. mutexclpocall : [];
  1105. mutexclpotype : [potype_operator];
  1106. mutexclpo : []
  1107. ),(
  1108. idtok:_INTERNPROC;
  1109. pd_flags : pd_implemen+pd_notobjintf;
  1110. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1111. pocall : pocall_internproc;
  1112. pooption : [];
  1113. mutexclpocall : [];
  1114. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1115. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_leftright]
  1116. ),(
  1117. idtok:_INTERRUPT;
  1118. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1119. handler : {$ifdef FPCPROCVAR}@{$endif}pd_interrupt;
  1120. pocall : pocall_none;
  1121. pooption : [po_interrupt];
  1122. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,
  1123. pocall_inline,pocall_pascal,pocall_system,pocall_far16,pocall_fpccall];
  1124. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1125. mutexclpo : [po_external,po_leftright,po_clearstack]
  1126. ),(
  1127. idtok:_IOCHECK;
  1128. pd_flags : pd_implemen+pd_body+pd_notobjintf;
  1129. handler : nil;
  1130. pocall : pocall_none;
  1131. pooption : [po_iocheck];
  1132. mutexclpocall : [pocall_internproc];
  1133. mutexclpotype : [];
  1134. mutexclpo : [po_external]
  1135. ),(
  1136. idtok:_MESSAGE;
  1137. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1138. handler : {$ifdef FPCPROCVAR}@{$endif}pd_message;
  1139. pocall : pocall_none;
  1140. pooption : []; { can be po_msgstr or po_msgint }
  1141. mutexclpocall : [pocall_inline,pocall_internproc];
  1142. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1143. mutexclpo : [po_interrupt,po_external]
  1144. ),(
  1145. idtok:_NEAR;
  1146. pd_flags : pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1147. handler : {$ifdef FPCPROCVAR}@{$endif}pd_near;
  1148. pocall : pocall_none;
  1149. pooption : [];
  1150. mutexclpocall : [pocall_internproc];
  1151. mutexclpotype : [];
  1152. mutexclpo : []
  1153. ),(
  1154. idtok:_OVERLOAD;
  1155. pd_flags : pd_implemen+pd_interface+pd_body;
  1156. handler : {$ifdef FPCPROCVAR}@{$endif}pd_overload;
  1157. pocall : pocall_none;
  1158. pooption : [po_overload];
  1159. mutexclpocall : [pocall_internproc];
  1160. mutexclpotype : [];
  1161. mutexclpo : []
  1162. ),(
  1163. idtok:_OVERRIDE;
  1164. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1165. handler : {$ifdef FPCPROCVAR}@{$endif}pd_override;
  1166. pocall : pocall_none;
  1167. pooption : [po_overridingmethod,po_virtualmethod];
  1168. mutexclpocall : [pocall_inline,pocall_internproc];
  1169. mutexclpotype : [];
  1170. mutexclpo : [po_exports,po_external,po_interrupt]
  1171. ),(
  1172. idtok:_PASCAL;
  1173. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1174. handler : nil;
  1175. pocall : pocall_pascal;
  1176. pooption : [];
  1177. mutexclpocall : [];
  1178. mutexclpotype : [];
  1179. mutexclpo : [po_external]
  1180. ),(
  1181. idtok:_POPSTACK;
  1182. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1183. handler : nil;
  1184. pocall : pocall_none;
  1185. pooption : [po_clearstack];
  1186. mutexclpocall : [pocall_inline,pocall_internproc,pocall_stdcall];
  1187. mutexclpotype : [];
  1188. mutexclpo : [po_assembler,po_external]
  1189. ),(
  1190. idtok:_PUBLIC;
  1191. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject+pd_notobjintf;
  1192. handler : nil;
  1193. pocall : pocall_none;
  1194. pooption : [];
  1195. mutexclpocall : [pocall_internproc,pocall_inline];
  1196. mutexclpotype : [];
  1197. mutexclpo : [po_external]
  1198. ),(
  1199. idtok:_REGISTER;
  1200. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1201. handler : nil;
  1202. pocall : pocall_register;
  1203. pooption : [];
  1204. mutexclpocall : [];
  1205. mutexclpotype : [];
  1206. mutexclpo : [po_external]
  1207. ),(
  1208. idtok:_REINTRODUCE;
  1209. pd_flags : pd_interface+pd_object;
  1210. handler : {$ifdef FPCPROCVAR}@{$endif}pd_reintroduce;
  1211. pocall : pocall_none;
  1212. pooption : [];
  1213. mutexclpocall : [];
  1214. mutexclpotype : [];
  1215. mutexclpo : []
  1216. ),(
  1217. idtok:_SAFECALL;
  1218. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1219. handler : nil;
  1220. pocall : pocall_safecall;
  1221. pooption : [];
  1222. mutexclpocall : [];
  1223. mutexclpotype : [];
  1224. mutexclpo : [po_external]
  1225. ),(
  1226. idtok:_SAVEREGISTERS;
  1227. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar+pd_notobjintf;
  1228. handler : nil;
  1229. pocall : pocall_none;
  1230. pooption : [po_saveregisters];
  1231. mutexclpocall : [pocall_internproc];
  1232. mutexclpotype : [];
  1233. mutexclpo : [po_external]
  1234. ),(
  1235. idtok:_STATIC;
  1236. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1237. handler : {$ifdef FPCPROCVAR}@{$endif}pd_static;
  1238. pocall : pocall_none;
  1239. pooption : [po_staticmethod];
  1240. mutexclpocall : [pocall_inline,pocall_internproc];
  1241. mutexclpotype : [potype_constructor,potype_destructor];
  1242. mutexclpo : [po_external,po_interrupt,po_exports]
  1243. ),(
  1244. idtok:_STDCALL;
  1245. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1246. handler : nil;
  1247. pocall : pocall_stdcall;
  1248. pooption : [];
  1249. mutexclpocall : [];
  1250. mutexclpotype : [];
  1251. mutexclpo : [po_external]
  1252. ),(
  1253. idtok:_SYSCALL;
  1254. pd_flags : pd_interface+pd_implemen+pd_notobjintf;
  1255. handler : {$ifdef FPCPROCVAR}@{$endif}pd_syscall;
  1256. pocall : pocall_palmossyscall;
  1257. pooption : [];
  1258. mutexclpocall : [];
  1259. mutexclpotype : [];
  1260. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1261. ),(
  1262. idtok:_SYSTEM;
  1263. pd_flags : pd_implemen+pd_notobjintf;
  1264. handler : nil;
  1265. pocall : pocall_system;
  1266. pooption : [];
  1267. mutexclpocall : [];
  1268. mutexclpotype : [];
  1269. mutexclpo : [po_external,po_assembler,po_interrupt]
  1270. ),(
  1271. idtok:_VIRTUAL;
  1272. pd_flags : pd_interface+pd_object+pd_notobjintf;
  1273. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1274. pocall : pocall_none;
  1275. pooption : [po_virtualmethod];
  1276. mutexclpocall : [pocall_inline,pocall_internproc];
  1277. mutexclpotype : [];
  1278. mutexclpo : [po_external,po_interrupt,po_exports]
  1279. ),(
  1280. idtok:_CPPDECL;
  1281. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1282. handler : nil;
  1283. pocall : pocall_cppdecl;
  1284. pooption : [po_savestdregs];
  1285. mutexclpocall : [];
  1286. mutexclpotype : [];
  1287. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1288. ),(
  1289. idtok:_VARARGS;
  1290. pd_flags : pd_interface+pd_implemen+pd_procvar;
  1291. handler : nil;
  1292. pocall : pocall_none;
  1293. pooption : [po_varargs];
  1294. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1295. pocall_inline,pocall_far16,pocall_fpccall];
  1296. mutexclpotype : [];
  1297. mutexclpo : [po_assembler,po_interrupt,po_leftright]
  1298. ),(
  1299. idtok:_COMPILERPROC;
  1300. pd_flags : pd_interface+pd_implemen+pd_body+pd_notobjintf;
  1301. handler : nil;
  1302. pocall : pocall_compilerproc;
  1303. pooption : [];
  1304. mutexclpocall : [];
  1305. mutexclpotype : [];
  1306. mutexclpo : [po_interrupt]
  1307. )
  1308. );
  1309. function is_proc_directive(tok:ttoken):boolean;
  1310. var
  1311. i : longint;
  1312. begin
  1313. is_proc_directive:=false;
  1314. for i:=1 to num_proc_directives do
  1315. if proc_direcdata[i].idtok=idtoken then
  1316. begin
  1317. is_proc_directive:=true;
  1318. exit;
  1319. end;
  1320. end;
  1321. function parse_proc_direc(var pdflags:word):boolean;
  1322. {
  1323. Parse the procedure directive, returns true if a correct directive is found
  1324. }
  1325. var
  1326. p : longint;
  1327. found : boolean;
  1328. name : stringid;
  1329. begin
  1330. parse_proc_direc:=false;
  1331. name:=tokeninfo^[idtoken].str;
  1332. found:=false;
  1333. { Hint directive? Then exit immediatly }
  1334. if (m_hintdirective in aktmodeswitches) then
  1335. begin
  1336. case idtoken of
  1337. _LIBRARY,
  1338. _PLATFORM,
  1339. _DEPRECATED :
  1340. exit;
  1341. end;
  1342. end;
  1343. { retrieve data for directive if found }
  1344. for p:=1 to num_proc_directives do
  1345. if proc_direcdata[p].idtok=idtoken then
  1346. begin
  1347. found:=true;
  1348. break;
  1349. end;
  1350. { Check if the procedure directive is known }
  1351. if not found then
  1352. begin
  1353. { parsing a procvar type the name can be any
  1354. next variable !! }
  1355. if (pdflags and (pd_procvar or pd_object))=0 then
  1356. Message1(parser_w_unknown_proc_directive_ignored,name);
  1357. exit;
  1358. end;
  1359. { static needs a special treatment }
  1360. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1361. exit;
  1362. { Conflicts between directives ? }
  1363. if (aktprocdef.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1364. (aktprocdef.proccalloption in proc_direcdata[p].mutexclpocall) or
  1365. ((aktprocdef.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1366. begin
  1367. Message1(parser_e_proc_dir_conflict,name);
  1368. exit;
  1369. end;
  1370. { set calling convention }
  1371. if proc_direcdata[p].pocall<>pocall_none then
  1372. begin
  1373. if aktprocdef.proccalloption<>pocall_none then
  1374. begin
  1375. Message2(parser_w_proc_overriding_calling,
  1376. proccalloptionStr[aktprocdef.proccalloption],
  1377. proccalloptionStr[proc_direcdata[p].pocall]);
  1378. end;
  1379. aktprocdef.proccalloption:=proc_direcdata[p].pocall;
  1380. end;
  1381. { check if method and directive not for object, like public.
  1382. This needs to be checked also for procvars }
  1383. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1384. (aktprocdef.owner.symtabletype=objectsymtable) then
  1385. exit;
  1386. if aktprocdef.deftype=procdef then
  1387. begin
  1388. { Check if the directive is only for objects }
  1389. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1390. not assigned(aktprocdef._class) then
  1391. exit;
  1392. { check if method and directive not for interface }
  1393. if ((proc_direcdata[p].pd_flags and pd_notobjintf)<>0) and
  1394. is_interface(aktprocdef._class) then
  1395. exit;
  1396. end;
  1397. { consume directive, and turn flag on }
  1398. consume(token);
  1399. parse_proc_direc:=true;
  1400. { Check the pd_flags if the directive should be allowed }
  1401. if ((pdflags and pd_interface)<>0) and
  1402. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1403. begin
  1404. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1405. exit;
  1406. end;
  1407. if ((pdflags and pd_implemen)<>0) and
  1408. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1409. begin
  1410. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1411. exit;
  1412. end;
  1413. if ((pdflags and pd_procvar)<>0) and
  1414. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1415. begin
  1416. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1417. exit;
  1418. end;
  1419. { Return the new pd_flags }
  1420. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1421. pdflags:=pdflags and (not pd_body);
  1422. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1423. pdflags:=pdflags or pd_global;
  1424. { Add the correct flag }
  1425. aktprocdef.procoptions:=aktprocdef.procoptions+proc_direcdata[p].pooption;
  1426. { Call the handler }
  1427. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1428. proc_direcdata[p].handler{$ifdef FPCPROCVAR}(){$endif};
  1429. end;
  1430. procedure handle_calling_convention(sym:tprocsym;def:tabstractprocdef);
  1431. begin
  1432. { set the default calling convention }
  1433. if def.proccalloption=pocall_none then
  1434. def.proccalloption:=aktdefproccall;
  1435. case def.proccalloption of
  1436. pocall_cdecl :
  1437. begin
  1438. { use popstack and save std registers }
  1439. include(def.procoptions,po_clearstack);
  1440. include(def.procoptions,po_savestdregs);
  1441. { set mangledname }
  1442. if (def.deftype=procdef) then
  1443. begin
  1444. if not tprocdef(def).has_mangledname then
  1445. begin
  1446. if assigned(tprocdef(def)._class) then
  1447. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def)._class.objrealname^+'_'+sym.realname)
  1448. else
  1449. tprocdef(def).setmangledname(target_info.Cprefix+sym.realname);
  1450. end;
  1451. if not assigned(tprocdef(def).parast) then
  1452. internalerror(200110234);
  1453. { do not copy on local !! }
  1454. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1455. { check C cdecl para types }
  1456. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1457. { Adjust alignment to match cdecl or stdcall }
  1458. tprocdef(def).parast.dataalignment:=std_param_align;
  1459. end;
  1460. end;
  1461. pocall_cppdecl :
  1462. begin
  1463. if not assigned(sym) then
  1464. internalerror(200110231);
  1465. { use popstack and save std registers }
  1466. include(def.procoptions,po_clearstack);
  1467. include(def.procoptions,po_savestdregs);
  1468. { set mangledname }
  1469. if (def.deftype=procdef) then
  1470. begin
  1471. if not tprocdef(def).has_mangledname then
  1472. tprocdef(def).setmangledname(target_info.Cprefix+tprocdef(def).cplusplusmangledname);
  1473. if not assigned(tprocdef(def).parast) then
  1474. internalerror(200110235);
  1475. { do not copy on local !! }
  1476. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1477. { check C cdecl para types }
  1478. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkparatype,nil);
  1479. { Adjust alignment to match cdecl or stdcall }
  1480. tprocdef(def).parast.dataalignment:=std_param_align;
  1481. end;
  1482. end;
  1483. pocall_stdcall :
  1484. begin
  1485. include(def.procoptions,po_savestdregs);
  1486. if (def.deftype=procdef) then
  1487. begin
  1488. if not assigned(tprocdef(def).parast) then
  1489. internalerror(200110236);
  1490. { Adjust alignment to match cdecl or stdcall }
  1491. tprocdef(def).parast.dataalignment:=std_param_align;
  1492. end;
  1493. end;
  1494. pocall_safecall :
  1495. begin
  1496. include(def.procoptions,po_savestdregs);
  1497. end;
  1498. pocall_compilerproc :
  1499. begin
  1500. if (not assigned(sym)) or
  1501. (def.deftype<>procdef) then
  1502. internalerror(200110232);
  1503. tprocdef(def).setmangledname(lower(sym.name));
  1504. end;
  1505. pocall_pascal :
  1506. begin
  1507. include(def.procoptions,po_leftright);
  1508. end;
  1509. pocall_register :
  1510. begin
  1511. Message1(parser_w_proc_directive_ignored,'REGISTER');
  1512. end;
  1513. pocall_far16 :
  1514. begin
  1515. { Temporary stub, must be rewritten to support OS/2 far16 }
  1516. Message1(parser_w_proc_directive_ignored,'FAR16');
  1517. end;
  1518. pocall_system :
  1519. begin
  1520. include(def.procoptions,po_clearstack);
  1521. if (not assigned(sym)) or
  1522. (def.deftype<>procdef) then
  1523. internalerror(200110233);
  1524. if not tprocdef(def).has_mangledname then
  1525. tprocdef(def).setmangledname(sym.realname);
  1526. end;
  1527. pocall_palmossyscall :
  1528. begin
  1529. { use popstack and save std registers }
  1530. include(def.procoptions,po_clearstack);
  1531. include(def.procoptions,po_savestdregs);
  1532. if (def.deftype=procdef) then
  1533. begin
  1534. if not assigned(tprocdef(def).parast) then
  1535. internalerror(200110236);
  1536. { do not copy on local !! }
  1537. tprocdef(def).parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}resetvaluepara,nil);
  1538. { Adjust positions of args for cdecl or stdcall }
  1539. tprocdef(def).parast.dataalignment:=std_param_align;
  1540. end;
  1541. end;
  1542. pocall_inline :
  1543. begin
  1544. if not(cs_support_inline in aktmoduleswitches) then
  1545. begin
  1546. Message(parser_e_proc_inline_not_supported);
  1547. def.proccalloption:=pocall_fpccall;
  1548. end;
  1549. end;
  1550. end;
  1551. { add mangledname to external list }
  1552. if (def.deftype=procdef) and
  1553. (po_external in def.procoptions) and
  1554. target_info.DllScanSupported then
  1555. current_module.externals.insert(tExternalsItem.create(tprocdef(def).mangledname));
  1556. end;
  1557. procedure calc_parasymtable_addresses(def:tprocdef);
  1558. var
  1559. lastps,
  1560. highps,ps : tsym;
  1561. st : tsymtable;
  1562. begin
  1563. st:=def.parast;
  1564. if po_leftright in def.procoptions then
  1565. begin
  1566. { pushed in reversed order, left to right }
  1567. highps:=nil;
  1568. lastps:=nil;
  1569. while assigned(st.symindex.first) and (lastps<>tsym(st.symindex.first)) do
  1570. begin
  1571. ps:=tsym(st.symindex.first);
  1572. while assigned(ps.indexnext) and (tsym(ps.indexnext)<>lastps) do
  1573. ps:=tsym(ps.indexnext);
  1574. if ps.typ=varsym then
  1575. begin
  1576. { Wait with inserting the high value, it needs to be inserted
  1577. after the corresponding parameter }
  1578. if Copy(ps.name,1,4)='high' then
  1579. highps:=ps
  1580. else
  1581. begin
  1582. st.insertvardata(ps);
  1583. { add also the high tree if it was saved }
  1584. if assigned(highps) then
  1585. begin
  1586. st.insertvardata(highps);
  1587. highps:=nil;
  1588. end;
  1589. end;
  1590. end;
  1591. lastps:=ps;
  1592. end;
  1593. if assigned(highps) then
  1594. internalerror(200208257);
  1595. end
  1596. else
  1597. begin
  1598. { pushed in normal order, right to left }
  1599. ps:=tsym(st.symindex.first);
  1600. while assigned(ps) do
  1601. begin
  1602. if ps.typ=varsym then
  1603. st.insertvardata(ps);
  1604. ps:=tsym(ps.indexnext);
  1605. end;
  1606. end;
  1607. end;
  1608. procedure parse_proc_directives(var pdflags:word);
  1609. {
  1610. Parse the procedure directives. It does not matter if procedure directives
  1611. are written using ;procdir; or ['procdir'] syntax.
  1612. }
  1613. var
  1614. res : boolean;
  1615. begin
  1616. while token in [_ID,_LECKKLAMMER] do
  1617. begin
  1618. if try_to_consume(_LECKKLAMMER) then
  1619. begin
  1620. repeat
  1621. parse_proc_direc(pdflags);
  1622. until not try_to_consume(_COMMA);
  1623. consume(_RECKKLAMMER);
  1624. { we always expect at least '[];' }
  1625. res:=true;
  1626. end
  1627. else
  1628. begin
  1629. res:=parse_proc_direc(pdflags);
  1630. end;
  1631. { A procedure directive normally followed by a semicolon, but in
  1632. a const section we should stop when _EQUAL is found }
  1633. if res then
  1634. begin
  1635. if (block_type=bt_const) and
  1636. (token=_EQUAL) then
  1637. break;
  1638. { support procedure proc;stdcall export; in Delphi mode only }
  1639. if not((m_delphi in aktmodeswitches) and
  1640. is_proc_directive(token)) then
  1641. consume(_SEMICOLON);
  1642. end
  1643. else
  1644. break;
  1645. end;
  1646. handle_calling_convention(aktprocsym,aktprocdef);
  1647. { calculate addresses in parasymtable }
  1648. if aktprocdef.deftype=procdef then
  1649. calc_parasymtable_addresses(aktprocdef);
  1650. end;
  1651. procedure parse_var_proc_directives(var sym : tsym);
  1652. var
  1653. pdflags : word;
  1654. oldsym : tprocsym;
  1655. olddef : tprocdef;
  1656. pd : tabstractprocdef;
  1657. begin
  1658. oldsym:=aktprocsym;
  1659. olddef:=aktprocdef;
  1660. pdflags:=pd_procvar;
  1661. { we create a temporary aktprocsym to read the directives }
  1662. aktprocsym:=tprocsym.create(sym.name);
  1663. case sym.typ of
  1664. varsym :
  1665. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1666. typedconstsym :
  1667. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1668. typesym :
  1669. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1670. else
  1671. internalerror(994932432);
  1672. end;
  1673. if pd.deftype<>procvardef then
  1674. internalerror(994932433);
  1675. tabstractprocdef(aktprocdef):=pd;
  1676. { names should never be used anyway }
  1677. inc(lexlevel);
  1678. parse_proc_directives(pdflags);
  1679. dec(lexlevel);
  1680. aktprocsym.free;
  1681. aktprocsym:=oldsym;
  1682. aktprocdef:=olddef;
  1683. end;
  1684. procedure parse_object_proc_directives(var sym : tprocsym);
  1685. var
  1686. pdflags : word;
  1687. begin
  1688. pdflags:=pd_object;
  1689. inc(lexlevel);
  1690. parse_proc_directives(pdflags);
  1691. dec(lexlevel);
  1692. if (po_containsself in aktprocdef.procoptions) and
  1693. (([po_msgstr,po_msgint]*aktprocdef.procoptions)=[]) then
  1694. Message(parser_e_self_in_non_message_handler);
  1695. end;
  1696. function proc_add_definition(aprocsym:tprocsym;var aprocdef : tprocdef) : boolean;
  1697. {
  1698. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1699. forwarddef is found and reused it returns true
  1700. }
  1701. var
  1702. hd : tprocdef;
  1703. ad,fd : tsym;
  1704. forwardfound : boolean;
  1705. i : cardinal;
  1706. begin
  1707. forwardfound:=false;
  1708. { check overloaded functions if the same function already exists }
  1709. for i:=1 to aprocsym.procdef_count do
  1710. begin
  1711. hd:=aprocsym.procdef[i];
  1712. { Skip overloaded definitions that are declared in other
  1713. units }
  1714. if hd.procsym<>aprocsym then
  1715. continue;
  1716. { check the parameters, for delphi/tp it is possible to
  1717. leave the parameters away in the implementation (forwarddef=false).
  1718. But for an overload declared function this is not allowed }
  1719. if { check if empty implementation arguments match is allowed }
  1720. (
  1721. not(m_repeat_forward in aktmodeswitches) and
  1722. not(aprocdef.forwarddef) and
  1723. (aprocdef.maxparacount=0) and
  1724. not(po_overload in hd.procoptions)
  1725. ) or
  1726. { check arguments }
  1727. (
  1728. equal_paras(aprocdef.para,hd.para,cp_none,false) and
  1729. { for operators equal_paras is not enough !! }
  1730. ((aprocdef.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1731. is_equal(hd.rettype.def,aprocdef.rettype.def))
  1732. ) then
  1733. begin
  1734. { Check if we've found the forwarddef, if found then
  1735. we need to update the forward def with the current
  1736. implementation settings }
  1737. if hd.forwarddef then
  1738. begin
  1739. forwardfound:=true;
  1740. { Check if the procedure type and return type are correct,
  1741. also the parameters must match also with the type }
  1742. if (hd.proctypeoption<>aprocdef.proctypeoption) or
  1743. (
  1744. (m_repeat_forward in aktmodeswitches) and
  1745. (not((aprocdef.maxparacount=0) or
  1746. equal_paras(aprocdef.para,hd.para,cp_all,false)))
  1747. ) or
  1748. (
  1749. ((m_repeat_forward in aktmodeswitches) or
  1750. not(is_void(aprocdef.rettype.def))) and
  1751. (not is_equal(hd.rettype.def,aprocdef.rettype.def))) then
  1752. begin
  1753. MessagePos1(aprocdef.fileinfo,parser_e_header_dont_match_forward,
  1754. aprocdef.fullprocname);
  1755. break;
  1756. end;
  1757. { Check if both are declared forward }
  1758. if hd.forwarddef and aprocdef.forwarddef then
  1759. begin
  1760. MessagePos1(aprocdef.fileinfo,parser_e_function_already_declared_public_forward,
  1761. aprocdef.fullprocname);
  1762. end;
  1763. { internconst or internproc only need to be defined once }
  1764. if (hd.proccalloption=pocall_internproc) then
  1765. aprocdef.proccalloption:=hd.proccalloption
  1766. else
  1767. if (aprocdef.proccalloption=pocall_internproc) then
  1768. hd.proccalloption:=aprocdef.proccalloption;
  1769. if (po_internconst in hd.procoptions) then
  1770. include(aprocdef.procoptions,po_internconst)
  1771. else if (po_internconst in aprocdef.procoptions) then
  1772. include(hd.procoptions,po_internconst);
  1773. { Check calling convention }
  1774. if (hd.proccalloption<>aprocdef.proccalloption) then
  1775. begin
  1776. { For delphi check if the current implementation has no proccalloption, then
  1777. take the options from the interface }
  1778. if not(m_delphi in aktmodeswitches) or
  1779. (aprocdef.proccalloption<>pocall_none) then
  1780. MessagePos(aprocdef.fileinfo,parser_e_call_convention_dont_match_forward);
  1781. { restore interface settings }
  1782. aprocdef.proccalloption:=hd.proccalloption;
  1783. if hd.has_mangledname then
  1784. aprocdef.setmangledname(hd.mangledname);
  1785. end;
  1786. { Check manglednames }
  1787. if (m_repeat_forward in aktmodeswitches) or
  1788. aprocdef.haspara then
  1789. begin
  1790. { If mangled names are equal then they have the same amount of arguments }
  1791. { We can check the names of the arguments }
  1792. { both symtables are in the same order from left to right }
  1793. ad:=tsym(hd.parast.symindex.first);
  1794. fd:=tsym(aprocdef.parast.symindex.first);
  1795. repeat
  1796. { skip default parameter constsyms }
  1797. while assigned(ad) and (ad.typ<>varsym) do
  1798. ad:=tsym(ad.indexnext);
  1799. while assigned(fd) and (fd.typ<>varsym) do
  1800. fd:=tsym(fd.indexnext);
  1801. { stop when one of the two lists is at the end }
  1802. if not assigned(ad) or not assigned(fd) then
  1803. break;
  1804. if (ad.name<>fd.name) then
  1805. begin
  1806. MessagePos3(aprocdef.fileinfo,parser_e_header_different_var_names,
  1807. aprocsym.name,ad.name,fd.name);
  1808. break;
  1809. end;
  1810. ad:=tsym(ad.indexnext);
  1811. fd:=tsym(fd.indexnext);
  1812. until false;
  1813. if assigned(ad) or assigned(fd) then
  1814. internalerror(200204178);
  1815. end;
  1816. { Everything is checked, now we can update the forward declaration
  1817. with the new data from the implementation }
  1818. hd.forwarddef:=aprocdef.forwarddef;
  1819. hd.hasforward:=true;
  1820. hd.parast.address_fixup:=aprocdef.parast.address_fixup;
  1821. hd.procoptions:=hd.procoptions+aprocdef.procoptions;
  1822. if hd.extnumber=65535 then
  1823. hd.extnumber:=aprocdef.extnumber;
  1824. while not aprocdef.aliasnames.empty do
  1825. hd.aliasnames.insert(aprocdef.aliasnames.getfirst);
  1826. { update mangledname if the implementation has a fixed mangledname set }
  1827. if aprocdef.has_mangledname then
  1828. begin
  1829. { rename also asmsymbol first, because the name can already be used }
  1830. objectlibrary.renameasmsymbol(hd.mangledname,aprocdef.mangledname);
  1831. hd.setmangledname(aprocdef.mangledname);
  1832. end;
  1833. { for compilerproc defines we need to rename and update the
  1834. symbolname to lowercase }
  1835. if (aprocdef.proccalloption=pocall_compilerproc) then
  1836. begin
  1837. { rename to lowercase so users can't access it }
  1838. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  1839. { also update the realname that is stored in the ppu }
  1840. stringdispose(aprocsym._realname);
  1841. aprocsym._realname:=stringdup('$'+aprocsym.name);
  1842. { the mangeled name is already changed by the pd_compilerproc }
  1843. { handler. It must be done immediately because if we have a }
  1844. { call to a compilerproc before it's implementation is }
  1845. { encountered, it must already use the new mangled name (JM) }
  1846. end;
  1847. { return the forwarddef }
  1848. aprocdef:=hd;
  1849. end
  1850. else
  1851. begin
  1852. { abstract methods aren't forward defined, but this }
  1853. { needs another error message }
  1854. if (po_abstractmethod in hd.procoptions) then
  1855. MessagePos(aprocdef.fileinfo,parser_e_abstract_no_definition)
  1856. else
  1857. MessagePos(aprocdef.fileinfo,parser_e_overloaded_have_same_parameters);
  1858. end;
  1859. { we found one proc with the same arguments, there are no others
  1860. so we can stop }
  1861. break;
  1862. end;
  1863. { check for allowing overload directive }
  1864. if not(m_fpc in aktmodeswitches) then
  1865. begin
  1866. { overload directive turns on overloading }
  1867. if ((po_overload in aprocdef.procoptions) or
  1868. (po_overload in hd.procoptions)) then
  1869. begin
  1870. { check if all procs have overloading, but not if the proc was
  1871. already declared forward, then the check is already done }
  1872. if not(hd.hasforward or
  1873. (aprocdef.forwarddef<>hd.forwarddef) or
  1874. ((po_overload in aprocdef.procoptions) and
  1875. (po_overload in hd.procoptions))) then
  1876. begin
  1877. MessagePos1(aprocdef.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  1878. break;
  1879. end;
  1880. end
  1881. else
  1882. begin
  1883. if not(hd.forwarddef) then
  1884. begin
  1885. MessagePos(aprocdef.fileinfo,parser_e_procedure_overloading_is_off);
  1886. break;
  1887. end;
  1888. end;
  1889. end; { equal arguments }
  1890. end;
  1891. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  1892. list }
  1893. if not forwardfound then
  1894. begin
  1895. aprocsym.addprocdef(aprocdef);
  1896. { add overloadnumber for unique naming, the overloadcount is
  1897. counted per module and 0 for the first procedure }
  1898. aprocdef.overloadnumber:=aprocsym.overloadcount;
  1899. inc(aprocsym.overloadcount);
  1900. end;
  1901. { insert otsym only in the right symtable }
  1902. if ((procinfo.flags and pi_operator)<>0) and
  1903. assigned(otsym) then
  1904. begin
  1905. if not parse_only then
  1906. begin
  1907. if paramanager.ret_in_param(aprocdef.rettype.def,aprocdef.proccalloption) then
  1908. begin
  1909. aprocdef.parast.insert(otsym);
  1910. { this allows to read the funcretoffset }
  1911. otsym.address:=-4;
  1912. otsym.varspez:=vs_var;
  1913. end
  1914. else
  1915. begin
  1916. aprocdef.localst.insert(otsym);
  1917. aprocdef.localst.insertvardata(otsym);
  1918. end;
  1919. end
  1920. else
  1921. begin
  1922. { this is not required anymore }
  1923. otsym.free;
  1924. otsym:=nil;
  1925. end;
  1926. end;
  1927. paramanager.create_param_loc_info(aprocdef);
  1928. proc_add_definition:=forwardfound;
  1929. end;
  1930. end.
  1931. {
  1932. $Log$
  1933. Revision 1.81 2002-11-18 17:31:58 peter
  1934. * pass proccalloption to ret_in_xxx and push_xxx functions
  1935. Revision 1.80 2002/11/17 16:31:56 carl
  1936. * memory optimization (3-4%) : cleanup of tai fields,
  1937. cleanup of tdef and tsym fields.
  1938. * make it work for m68k
  1939. Revision 1.79 2002/11/16 14:20:50 peter
  1940. * fix infinite loop in pd_inline
  1941. Revision 1.78 2002/11/15 01:58:53 peter
  1942. * merged changes from 1.0.7 up to 04-11
  1943. - -V option for generating bug report tracing
  1944. - more tracing for option parsing
  1945. - errors for cdecl and high()
  1946. - win32 import stabs
  1947. - win32 records<=8 are returned in eax:edx (turned off by default)
  1948. - heaptrc update
  1949. - more info for temp management in .s file with EXTDEBUG
  1950. Revision 1.77 2002/10/06 15:09:12 peter
  1951. * variant:=nil supported
  1952. Revision 1.76 2002/09/27 21:13:29 carl
  1953. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  1954. Revision 1.75 2002/09/16 14:11:13 peter
  1955. * add argument to equal_paras() to support default values or not
  1956. Revision 1.74 2002/09/10 16:27:28 peter
  1957. * don't insert parast in symtablestack, because typesyms should not be
  1958. searched in the the parast
  1959. Revision 1.73 2002/09/09 19:39:07 peter
  1960. * check return type for forwarddefs also not delphi mode when
  1961. the type is not void
  1962. Revision 1.72 2002/09/09 17:34:15 peter
  1963. * tdicationary.replace added to replace and item in a dictionary. This
  1964. is only allowed for the same name
  1965. * varsyms are inserted in symtable before the types are parsed. This
  1966. fixes the long standing "var longint : longint" bug
  1967. - consume_idlist and idstringlist removed. The loops are inserted
  1968. at the callers place and uses the symtable for duplicate id checking
  1969. Revision 1.71 2002/09/07 15:25:06 peter
  1970. * old logs removed and tabs fixed
  1971. Revision 1.70 2002/09/03 16:26:27 daniel
  1972. * Make Tprocdef.defs protected
  1973. Revision 1.69 2002/09/01 12:11:33 peter
  1974. * calc param_offset after parameters are read, because the calculation
  1975. depends on po_containself
  1976. Revision 1.68 2002/08/25 19:25:20 peter
  1977. * sym.insert_in_data removed
  1978. * symtable.insertvardata/insertconstdata added
  1979. * removed insert_in_data call from symtable.insert, it needs to be
  1980. called separatly. This allows to deref the address calculation
  1981. * procedures now calculate the parast addresses after the procedure
  1982. directives are parsed. This fixes the cdecl parast problem
  1983. * push_addr_param has an extra argument that specifies if cdecl is used
  1984. or not
  1985. Revision 1.67 2002/08/25 11:33:06 peter
  1986. * also check the paratypes when a forward was found
  1987. Revision 1.66 2002/08/19 19:36:44 peter
  1988. * More fixes for cross unit inlining, all tnodes are now implemented
  1989. * Moved pocall_internconst to po_internconst because it is not a
  1990. calling type at all and it conflicted when inlining of these small
  1991. functions was requested
  1992. Revision 1.65 2002/08/18 20:06:24 peter
  1993. * inlining is now also allowed in interface
  1994. * renamed write/load to ppuwrite/ppuload
  1995. * tnode storing in ppu
  1996. * nld,ncon,nbas are already updated for storing in ppu
  1997. Revision 1.64 2002/08/17 09:23:39 florian
  1998. * first part of procinfo rewrite
  1999. Revision 1.63 2002/08/11 14:32:27 peter
  2000. * renamed current_library to objectlibrary
  2001. Revision 1.62 2002/08/11 13:24:12 peter
  2002. * saving of asmsymbols in ppu supported
  2003. * asmsymbollist global is removed and moved into a new class
  2004. tasmlibrarydata that will hold the info of a .a file which
  2005. corresponds with a single module. Added librarydata to tmodule
  2006. to keep the library info stored for the module. In the future the
  2007. objectfiles will also be stored to the tasmlibrarydata class
  2008. * all getlabel/newasmsymbol and friends are moved to the new class
  2009. Revision 1.61 2002/07/26 21:15:40 florian
  2010. * rewrote the system handling
  2011. Revision 1.60 2002/07/20 11:57:55 florian
  2012. * types.pas renamed to defbase.pas because D6 contains a types
  2013. unit so this would conflicts if D6 programms are compiled
  2014. + Willamette/SSE2 instructions to assembler added
  2015. Revision 1.59 2002/07/11 14:41:28 florian
  2016. * start of the new generic parameter handling
  2017. Revision 1.58 2002/07/01 18:46:25 peter
  2018. * internal linker
  2019. * reorganized aasm layer
  2020. Revision 1.57 2002/05/18 13:34:12 peter
  2021. * readded missing revisions
  2022. Revision 1.56 2002/05/16 19:46:42 carl
  2023. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2024. + try to fix temp allocation (still in ifdef)
  2025. + generic constructor calls
  2026. + start of tassembler / tmodulebase class cleanup
  2027. Revision 1.54 2002/05/12 16:53:08 peter
  2028. * moved entry and exitcode to ncgutil and cgobj
  2029. * foreach gets extra argument for passing local data to the
  2030. iterator function
  2031. * -CR checks also class typecasts at runtime by changing them
  2032. into as
  2033. * fixed compiler to cycle with the -CR option
  2034. * fixed stabs with elf writer, finally the global variables can
  2035. be watched
  2036. * removed a lot of routines from cga unit and replaced them by
  2037. calls to cgobj
  2038. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2039. u32bit then the other is typecasted also to u32bit without giving
  2040. a rangecheck warning/error.
  2041. * fixed pascal calling method with reversing also the high tree in
  2042. the parast, detected by tcalcst3 test
  2043. Revision 1.53 2002/04/21 19:02:04 peter
  2044. * removed newn and disposen nodes, the code is now directly
  2045. inlined from pexpr
  2046. * -an option that will write the secondpass nodes to the .s file, this
  2047. requires EXTDEBUG define to actually write the info
  2048. * fixed various internal errors and crashes due recent code changes
  2049. Revision 1.52 2002/04/20 21:32:24 carl
  2050. + generic FPC_CHECKPOINTER
  2051. + first parameter offset in stack now portable
  2052. * rename some constants
  2053. + move some cpu stuff to other units
  2054. - remove unused constents
  2055. * fix stacksize for some targets
  2056. * fix generic size problems which depend now on EXTEND_SIZE constant
  2057. Revision 1.51 2002/04/20 15:27:05 carl
  2058. - remove ifdef i386 define
  2059. Revision 1.50 2002/04/19 15:46:02 peter
  2060. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  2061. in most cases and not written to the ppu
  2062. * add mangeledname_prefix() routine to generate the prefix of
  2063. manglednames depending on the current procedure, object and module
  2064. * removed static procprefix since the mangledname is now build only
  2065. on demand from tprocdef.mangledname
  2066. }