pdecsub.pas 86 KB

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