pdecsub.pas 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332
  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. type
  24. tpdflag=(
  25. pd_body, { directive needs a body }
  26. pd_implemen, { directive can be used implementation section }
  27. pd_interface, { directive can be used interface section }
  28. pd_object, { directive can be used object declaration }
  29. pd_procvar, { directive can be used procvar declaration }
  30. pd_notobject, { directive can not be used object declaration }
  31. pd_notobjintf, { directive can not be used interface declaration }
  32. pd_notprocvar { directive can not be used procvar declaration }
  33. );
  34. tpdflags=set of tpdflag;
  35. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  36. procedure calc_parast(pd:tabstractprocdef);
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var pd:tprocdef):boolean;
  39. procedure handle_calling_convention(pd:tabstractprocdef);
  40. procedure parse_parameter_dec(pd:tabstractprocdef);
  41. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  42. procedure parse_var_proc_directives(sym:tsym);
  43. procedure parse_object_proc_directives(pd:tabstractprocdef);
  44. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  45. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  46. implementation
  47. uses
  48. strings,
  49. { common }
  50. cutils,cclasses,
  51. { global }
  52. globtype,globals,verbose,
  53. systems,
  54. cpuinfo,
  55. { aasm }
  56. aasmbase,
  57. { symtable }
  58. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  59. { pass 1 }
  60. node,htypechk,
  61. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  62. { parser }
  63. fmodule,scanner,
  64. pbase,pexpr,ptype,pdecl,
  65. { linking }
  66. import,gendef
  67. ;
  68. const
  69. { Please leave this here, this module should NOT use
  70. these variables.
  71. Declaring it as string here results in an error when compiling (PFV) }
  72. current_procinfo = 'error';
  73. procedure insert_funcret_para(pd:tabstractprocdef);
  74. var
  75. storepos : tfileposinfo;
  76. vs : tvarsym;
  77. begin
  78. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  79. not is_void(pd.rettype.def) and
  80. paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  81. begin
  82. storepos:=akttokenpos;
  83. if pd.deftype=procdef then
  84. akttokenpos:=tprocdef(pd).fileinfo;
  85. { Generate result variable accessing function result }
  86. vs:=tvarsym.create('$result',vs_var,pd.rettype);
  87. include(vs.varoptions,vo_is_funcret);
  88. pd.parast.insert(vs);
  89. { For left to right add it at the end to be delphi compatible }
  90. if pd.proccalloption in pushleftright_pocalls then
  91. pd.concatpara(nil,vs.vartype,vs,nil,true)
  92. else
  93. pd.insertpara(vs.vartype,vs,nil,true);
  94. { Store the this symbol as funcretsym for procedures }
  95. if pd.deftype=procdef then
  96. tprocdef(pd).funcretsym:=vs;
  97. akttokenpos:=storepos;
  98. end;
  99. end;
  100. procedure insert_parentfp_para(pd:tabstractprocdef);
  101. var
  102. storepos : tfileposinfo;
  103. vs : tvarsym;
  104. begin
  105. if pd.parast.symtablelevel>normal_function_level then
  106. begin
  107. storepos:=akttokenpos;
  108. if pd.deftype=procdef then
  109. akttokenpos:=tprocdef(pd).fileinfo;
  110. { Generate result variable accessing function result, it
  111. can't be put in a register since it must be accessable
  112. from the framepointer }
  113. vs:=tvarsym.create('$parentfp',vs_var,voidpointertype);
  114. include(vs.varoptions,vo_is_parentfp);
  115. vs.varregable:=vr_none;
  116. pd.parast.insert(vs);
  117. pd.insertpara(vs.vartype,vs,nil,true);
  118. akttokenpos:=storepos;
  119. end;
  120. end;
  121. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  122. var
  123. storepos : tfileposinfo;
  124. vs : tvarsym;
  125. tt : ttype;
  126. vsp : tvarspez;
  127. begin
  128. if (pd.deftype=procvardef) and
  129. pd.is_methodpointer then
  130. begin
  131. { Generate self variable }
  132. tt:=voidpointertype;
  133. vs:=tvarsym.create('$self',vs_value,tt);
  134. include(vs.varoptions,vo_is_self);
  135. { Insert as hidden parameter }
  136. pd.parast.insert(vs);
  137. pd.insertpara(vs.vartype,vs,nil,true);
  138. end
  139. else
  140. begin
  141. if (pd.deftype=procdef) and
  142. assigned(tprocdef(pd)._class) and
  143. (pd.parast.symtablelevel=normal_function_level) then
  144. begin
  145. storepos:=akttokenpos;
  146. akttokenpos:=tprocdef(pd).fileinfo;
  147. { Generate VMT variable for constructor/destructor }
  148. if pd.proctypeoption in [potype_constructor,potype_destructor] then
  149. begin
  150. { can't use classrefdef as type because inheriting
  151. will then always file because of a type mismatch }
  152. tt:=voidpointertype;
  153. vs:=tvarsym.create('$vmt',vs_value,tt);
  154. include(vs.varoptions,vo_is_vmt);
  155. { Insert as hidden parameter }
  156. pd.parast.insert(vs);
  157. pd.insertpara(vs.vartype,vs,nil,true);
  158. end;
  159. { Generate self variable, for classes we need
  160. to use the generic voidpointer to be compatible with
  161. methodpointers }
  162. vsp:=vs_value;
  163. if (po_staticmethod in pd.procoptions) or
  164. (po_classmethod in pd.procoptions) then
  165. begin
  166. tt.setdef(tprocdef(pd)._class);
  167. tt.setdef(tclassrefdef.create(tt));
  168. end
  169. else
  170. begin
  171. if is_object(tprocdef(pd)._class) then
  172. vsp:=vs_var;
  173. tt.setdef(tprocdef(pd)._class);
  174. end;
  175. vs:=tvarsym.create('$self',vsp,tt);
  176. include(vs.varoptions,vo_is_self);
  177. { Insert as hidden parameter }
  178. pd.parast.insert(vs);
  179. pd.insertpara(vs.vartype,vs,nil,true);
  180. akttokenpos:=storepos;
  181. end;
  182. end;
  183. end;
  184. procedure insert_funcret_local(pd:tprocdef);
  185. var
  186. storepos : tfileposinfo;
  187. vs : tvarsym;
  188. sl : tsymlist;
  189. begin
  190. { The result from constructors and destructors can't be accessed directly }
  191. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  192. not is_void(pd.rettype.def) then
  193. begin
  194. storepos:=akttokenpos;
  195. akttokenpos:=pd.fileinfo;
  196. { We always need a localsymtable }
  197. if not assigned(pd.localst) then
  198. pd.insert_localst;
  199. { We need to insert a varsym for the result in the localst
  200. when it is returning in a register }
  201. if not paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  202. begin
  203. vs:=tvarsym.create('$result',vs_value,pd.rettype);
  204. include(vs.varoptions,vo_is_funcret);
  205. pd.localst.insert(vs);
  206. pd.funcretsym:=vs;
  207. end;
  208. { insert the name of the procedure as alias for the function result,
  209. we can't use realname because that will not work for compilerprocs
  210. as the name is lowercase and unreachable from the code }
  211. if pd.resultname='' then
  212. pd.resultname:=pd.procsym.name;
  213. sl:=tsymlist.create;
  214. sl.addsym(sl_load,pd.funcretsym);
  215. vs:=tabsolutesym.create_ref(pd.resultname,pd.rettype,sl);
  216. include(vs.varoptions,vo_is_funcret);
  217. pd.localst.insert(vs);
  218. { insert result also if support is on }
  219. if (m_result in aktmodeswitches) then
  220. begin
  221. sl:=tsymlist.create;
  222. sl.addsym(sl_load,pd.funcretsym);
  223. vs:=tabsolutesym.create_ref('RESULT',pd.rettype,sl);
  224. include(vs.varoptions,vo_is_funcret);
  225. include(vs.varoptions,vo_is_result);
  226. pd.localst.insert(vs);
  227. end;
  228. akttokenpos:=storepos;
  229. end;
  230. end;
  231. procedure insert_hidden_para(pd:tabstractprocdef);
  232. var
  233. currpara : tparaitem;
  234. hvs : tvarsym;
  235. begin
  236. { walk from right to left, so we can insert the
  237. high parameters after the current parameter }
  238. currpara:=tparaitem(pd.para.last);
  239. while assigned(currpara) do
  240. begin
  241. { needs high parameter ? }
  242. if paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) then
  243. begin
  244. if assigned(currpara.parasym) then
  245. begin
  246. hvs:=tvarsym.create('$high'+tvarsym(currpara.parasym).name,vs_const,sinttype);
  247. include(hvs.varoptions,vo_is_high_value);
  248. tvarsym(currpara.parasym).owner.insert(hvs);
  249. end
  250. else
  251. hvs:=nil;
  252. pd.concatpara(currpara,sinttype,hvs,nil,true);
  253. end
  254. else
  255. begin
  256. { Give a warning that cdecl routines does not include high()
  257. support }
  258. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  259. paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pocall_default) then
  260. begin
  261. if is_open_string(currpara.paratype.def) then
  262. Message(parser_w_cdecl_no_openstring);
  263. if not (po_external in pd.procoptions) then
  264. Message(parser_w_cdecl_has_no_high);
  265. end;
  266. end;
  267. currpara:=tparaitem(currpara.previous);
  268. end;
  269. end;
  270. procedure check_c_para(p:tnamedindexitem;arg:pointer);
  271. begin
  272. if (tsym(p).typ<>varsym) then
  273. exit;
  274. with tvarsym(p) do
  275. begin
  276. case vartype.def.deftype of
  277. arraydef :
  278. begin
  279. if not is_variant_array(vartype.def) and
  280. not is_array_of_const(vartype.def) then
  281. begin
  282. if (varspez<>vs_var) then
  283. Message(parser_h_c_arrays_are_references);
  284. end;
  285. if is_array_of_const(vartype.def) and
  286. assigned(indexnext) and
  287. (tsym(indexnext).typ=varsym) and
  288. not(vo_is_high_value in tvarsym(indexnext).varoptions) then
  289. Message(parser_e_C_array_of_const_must_be_last);
  290. end;
  291. end;
  292. end;
  293. end;
  294. procedure set_addr_param_regable(p:tnamedindexitem;arg:pointer);
  295. begin
  296. if (tsym(p).typ<>varsym) then
  297. exit;
  298. with tvarsym(p) do
  299. begin
  300. if not vartype.def.needs_inittable and
  301. paramanager.push_addr_param(varspez,vartype.def,tprocdef(arg).proccalloption) then
  302. varregable:=vr_intreg;
  303. end;
  304. end;
  305. procedure parse_parameter_dec(pd:tabstractprocdef);
  306. {
  307. handle_procvar needs the same changes
  308. }
  309. var
  310. sc : tsinglelist;
  311. tt : ttype;
  312. arrayelementtype : ttype;
  313. vs : tvarsym;
  314. srsym : tsym;
  315. hs1 : string;
  316. varspez : Tvarspez;
  317. defaultvalue : tconstsym;
  318. defaultrequired : boolean;
  319. old_object_option : tsymoptions;
  320. currparast : tparasymtable;
  321. explicit_paraloc : boolean;
  322. locationstr : string;
  323. begin
  324. explicit_paraloc:=false;
  325. consume(_LKLAMMER);
  326. { Delphi/Kylix supports nonsense like }
  327. { procedure p(); }
  328. if try_to_consume(_RKLAMMER) and
  329. not(m_tp7 in aktmodeswitches) then
  330. exit;
  331. { parsing a proc or procvar ? }
  332. currparast:=tparasymtable(pd.parast);
  333. { reset }
  334. sc:=tsinglelist.create;
  335. defaultrequired:=false;
  336. { the variables are always public }
  337. old_object_option:=current_object_option;
  338. current_object_option:=[sp_public];
  339. inc(testcurobject);
  340. repeat
  341. if try_to_consume(_VAR) then
  342. varspez:=vs_var
  343. else
  344. if try_to_consume(_CONST) then
  345. varspez:=vs_const
  346. else
  347. if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  348. begin
  349. consume(_OUT);
  350. varspez:=vs_out
  351. end
  352. else
  353. if (token=_POINTPOINTPOINT) and (m_mac in aktmodeswitches) then
  354. begin
  355. consume(_POINTPOINTPOINT);
  356. include(pd.procoptions,po_varargs);
  357. break;
  358. end
  359. else
  360. varspez:=vs_value;
  361. defaultvalue:=nil;
  362. tt.reset;
  363. { read identifiers and insert with error type }
  364. sc.reset;
  365. repeat
  366. vs:=tvarsym.create(orgpattern,varspez,generrortype);
  367. currparast.insert(vs);
  368. if assigned(vs.owner) then
  369. sc.insert(vs)
  370. else
  371. vs.free;
  372. consume(_ID);
  373. until not try_to_consume(_COMMA);
  374. locationstr:='';
  375. { read type declaration, force reading for value and const paras }
  376. if (token=_COLON) or (varspez=vs_value) then
  377. begin
  378. consume(_COLON);
  379. { check for an open array }
  380. if token=_ARRAY then
  381. begin
  382. consume(_ARRAY);
  383. consume(_OF);
  384. { define range and type of range }
  385. tt.setdef(tarraydef.create(0,-1,s32inttype));
  386. { array of const ? }
  387. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  388. begin
  389. consume(_CONST);
  390. srsym:=searchsymonlyin(systemunit,'TVARREC');
  391. if not assigned(srsym) then
  392. InternalError(200404181);
  393. tarraydef(tt.def).setelementtype(ttypesym(srsym).restype);
  394. tarraydef(tt.def).IsArrayOfConst:=true;
  395. end
  396. else
  397. begin
  398. { define field type }
  399. single_type(arrayelementtype,hs1,false);
  400. tarraydef(tt.def).setelementtype(arrayelementtype);
  401. end;
  402. end
  403. else
  404. begin
  405. { open string ? }
  406. if (varspez=vs_var) and
  407. (
  408. (
  409. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  410. (cs_openstring in aktmoduleswitches) and
  411. not(cs_ansistrings in aktlocalswitches)
  412. ) or
  413. (idtoken=_OPENSTRING)) then
  414. begin
  415. consume(token);
  416. tt:=openshortstringtype;
  417. hs1:='openstring';
  418. end
  419. else
  420. begin
  421. { everything else }
  422. if (m_mac in aktmodeswitches) then
  423. try_to_consume(_UNIV); {currently does nothing}
  424. single_type(tt,hs1,false);
  425. end;
  426. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  427. begin
  428. if (idtoken=_LOCATION) then
  429. begin
  430. consume(_LOCATION);
  431. locationstr:=pattern;
  432. consume(_CSTRING);
  433. end
  434. else
  435. begin
  436. if explicit_paraloc then
  437. Message(parser_e_paraloc_all_paras);
  438. locationstr:='';
  439. end;
  440. end
  441. else
  442. locationstr:='';
  443. { default parameter }
  444. if (m_default_para in aktmodeswitches) then
  445. begin
  446. if try_to_consume(_EQUAL) then
  447. begin
  448. vs:=tvarsym(sc.first);
  449. if assigned(vs.listnext) then
  450. Message(parser_e_default_value_only_one_para);
  451. { prefix 'def' to the parameter name }
  452. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  453. if assigned(defaultvalue) then
  454. begin
  455. include(defaultvalue.symoptions,sp_internal);
  456. tprocdef(pd).parast.insert(defaultvalue);
  457. end;
  458. defaultrequired:=true;
  459. end
  460. else
  461. begin
  462. if defaultrequired then
  463. Message1(parser_e_default_value_expected_for_para,vs.name);
  464. end;
  465. end;
  466. end;
  467. end
  468. else
  469. begin
  470. {$ifndef UseNiceNames}
  471. hs1:='$$$';
  472. {$else UseNiceNames}
  473. hs1:='var';
  474. {$endif UseNiceNames}
  475. tt:=cformaltype;
  476. end;
  477. { File types are only allowed for var parameters }
  478. if (tt.def.deftype=filedef) and
  479. (varspez<>vs_var) then
  480. CGMessage(cg_e_file_must_call_by_reference);
  481. vs:=tvarsym(sc.first);
  482. while assigned(vs) do
  483. begin
  484. { update varsym }
  485. vs.vartype:=tt;
  486. pd.concatpara(nil,tt,vs,defaultvalue,false);
  487. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  488. begin
  489. if locationstr<>'' then
  490. begin
  491. if assigned(sc.first.listnext) then
  492. Message(parser_e_paraloc_only_one_para);
  493. if (pd.para.first<>pd.para.last) and not(explicit_paraloc) then
  494. Message(parser_e_paraloc_all_paras);
  495. explicit_paraloc:=true;
  496. if not(paramanager.parseparaloc(tparaitem(pd.para.last),upper(locationstr))) then
  497. message(parser_e_illegal_explicit_paraloc);
  498. end
  499. else
  500. if explicit_paraloc then
  501. Message(parser_e_paraloc_all_paras);
  502. end;
  503. vs:=tvarsym(vs.listnext);
  504. end;
  505. until not try_to_consume(_SEMICOLON);
  506. if explicit_paraloc then
  507. begin
  508. pd.has_paraloc_info:=true;
  509. include(pd.procoptions,po_explicitparaloc);
  510. end;
  511. { remove parasymtable from stack }
  512. sc.free;
  513. { reset object options }
  514. dec(testcurobject);
  515. current_object_option:=old_object_option;
  516. consume(_RKLAMMER);
  517. end;
  518. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  519. var
  520. orgsp,sp : stringid;
  521. sym : tsym;
  522. srsymtable : tsymtable;
  523. storepos,
  524. procstartfilepos : tfileposinfo;
  525. searchagain : boolean;
  526. i : longint;
  527. st : tsymtable;
  528. aprocsym : tprocsym;
  529. begin
  530. { Save the position where this procedure really starts }
  531. procstartfilepos:=akttokenpos;
  532. result:=false;
  533. pd:=nil;
  534. aprocsym:=nil;
  535. if (potype=potype_operator) then
  536. begin
  537. sp:=overloaded_names[optoken];
  538. orgsp:=sp;
  539. end
  540. else
  541. begin
  542. sp:=pattern;
  543. orgsp:=orgpattern;
  544. consume(_ID);
  545. end;
  546. { examine interface map: function/procedure iname.functionname=locfuncname }
  547. if assigned(aclass) and
  548. assigned(aclass.implementedinterfaces) and
  549. (aclass.implementedinterfaces.count>0) and
  550. try_to_consume(_POINT) then
  551. begin
  552. storepos:=akttokenpos;
  553. akttokenpos:=procstartfilepos;
  554. { get interface syms}
  555. searchsym(sp,sym,srsymtable);
  556. if not assigned(sym) then
  557. begin
  558. identifier_not_found(orgsp);
  559. sym:=generrorsym;
  560. end;
  561. akttokenpos:=storepos;
  562. { qualifier is interface? }
  563. if (sym.typ=typesym) and
  564. (ttypesym(sym).restype.def.deftype=objectdef) then
  565. i:=aclass.implementedinterfaces.searchintf(ttypesym(sym).restype.def)
  566. else
  567. i:=-1;
  568. if (i=-1) then
  569. Message(parser_e_interface_id_expected);
  570. consume(_ID);
  571. consume(_EQUAL);
  572. if (token=_ID) then
  573. aclass.implementedinterfaces.addmappings(i,sp,pattern);
  574. consume(_ID);
  575. result:=true;
  576. exit;
  577. end;
  578. { method ? }
  579. if not assigned(aclass) and
  580. (potype<>potype_operator) and
  581. (symtablestack.symtablelevel=main_program_level) and
  582. try_to_consume(_POINT) then
  583. begin
  584. { search for object name }
  585. storepos:=akttokenpos;
  586. akttokenpos:=procstartfilepos;
  587. searchsym(sp,sym,srsymtable);
  588. if not assigned(sym) then
  589. begin
  590. identifier_not_found(orgsp);
  591. sym:=generrorsym;
  592. end;
  593. akttokenpos:=storepos;
  594. { consume proc name }
  595. sp:=pattern;
  596. orgsp:=orgpattern;
  597. procstartfilepos:=akttokenpos;
  598. consume(_ID);
  599. { qualifier is class name ? }
  600. if (sym.typ=typesym) and
  601. (ttypesym(sym).restype.def.deftype=objectdef) then
  602. begin
  603. aclass:=tobjectdef(ttypesym(sym).restype.def);
  604. aprocsym:=tprocsym(aclass.symtable.search(sp));
  605. { we solve this below }
  606. if assigned(aprocsym) then
  607. begin
  608. if aprocsym.typ<>procsym then
  609. begin
  610. { we use a different error message for tp7 so it looks more compatible }
  611. if (m_fpc in aktmodeswitches) then
  612. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  613. else
  614. Message(parser_e_methode_id_expected);
  615. { rename the name to an unique name to avoid an
  616. error when inserting the symbol in the symtable }
  617. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  618. aprocsym:=nil;
  619. end;
  620. end
  621. else
  622. begin
  623. Message(parser_e_methode_id_expected);
  624. { recover by making it a normal procedure instead of method }
  625. aclass:=nil;
  626. end;
  627. end
  628. else
  629. Message(parser_e_class_id_expected);
  630. end
  631. else
  632. begin
  633. { check for constructor/destructor which is not allowed here }
  634. if (not parse_only) and
  635. (potype in [potype_constructor,potype_destructor]) then
  636. Message(parser_e_constructors_always_objects);
  637. repeat
  638. searchagain:=false;
  639. akttokenpos:=procstartfilepos;
  640. aprocsym:=tprocsym(symtablestack.search(sp));
  641. if not(parse_only) and
  642. not assigned(aprocsym) and
  643. (symtablestack.symtabletype=staticsymtable) and
  644. assigned(symtablestack.next) and
  645. (symtablestack.next.unitid=0) then
  646. begin
  647. { The procedure we prepare for is in the implementation
  648. part of the unit we compile. It is also possible that we
  649. are compiling a program, which is also some kind of
  650. implementaion part.
  651. We need to find out if the procedure is global. If it is
  652. global, it is in the global symtable.}
  653. aprocsym:=tprocsym(symtablestack.next.search(sp));
  654. end;
  655. { Check if overloaded is a procsym }
  656. if assigned(aprocsym) and
  657. (aprocsym.typ<>procsym) then
  658. begin
  659. { when the other symbol is a unit symbol then hide the unit
  660. symbol }
  661. if (aprocsym.typ=unitsym) then
  662. begin
  663. aprocsym.owner.rename(aprocsym.name,'hidden'+aprocsym.name);
  664. searchagain:=true;
  665. end
  666. else
  667. begin
  668. { we use a different error message for tp7 so it looks more compatible }
  669. if (m_fpc in aktmodeswitches) then
  670. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  671. else
  672. DuplicateSym(aprocsym);
  673. { rename the name to an unique name to avoid an
  674. error when inserting the symbol in the symtable }
  675. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  676. { generate a new aktprocsym }
  677. aprocsym:=nil;
  678. end;
  679. end;
  680. until not searchagain;
  681. end;
  682. { test again if assigned, it can be reset to recover }
  683. if not assigned(aprocsym) then
  684. begin
  685. { create a new procsym and set the real filepos }
  686. akttokenpos:=procstartfilepos;
  687. { for operator we have only one procsym for each overloaded
  688. operation }
  689. if (potype=potype_operator) then
  690. begin
  691. Aprocsym:=Tprocsym(symtablestack.search(sp));
  692. if Aprocsym=nil then
  693. Aprocsym:=tprocsym.create('$'+sp);
  694. end
  695. else
  696. aprocsym:=tprocsym.create(orgsp);
  697. symtablestack.insert(aprocsym);
  698. end;
  699. { to get the correct symtablelevel we must ignore objectsymtables }
  700. st:=symtablestack;
  701. while not(st.symtabletype in [staticsymtable,globalsymtable,localsymtable]) do
  702. st:=st.next;
  703. pd:=tprocdef.create(st.symtablelevel+1);
  704. pd._class:=aclass;
  705. pd.procsym:=aprocsym;
  706. pd.proctypeoption:=potype;
  707. { methods need to be exported }
  708. if assigned(aclass) and
  709. (
  710. (symtablestack.symtabletype=objectsymtable) or
  711. (symtablestack.symtablelevel=main_program_level)
  712. ) then
  713. include(pd.procoptions,po_public);
  714. { symbol options that need to be kept per procdef }
  715. pd.fileinfo:=procstartfilepos;
  716. pd.symoptions:=current_object_option;
  717. { parse parameters }
  718. if token=_LKLAMMER then
  719. parse_parameter_dec(pd);
  720. result:=true;
  721. end;
  722. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  723. var
  724. pd : tprocdef;
  725. hs : string;
  726. isclassmethod : boolean;
  727. begin
  728. pd:=nil;
  729. { read class method }
  730. if try_to_consume(_CLASS) then
  731. begin
  732. { class method only allowed for procedures and functions }
  733. if not(token in [_FUNCTION,_PROCEDURE]) then
  734. Message(parser_e_procedure_or_function_expected);
  735. isclassmethod:=true;
  736. end
  737. else
  738. isclassmethod:=false;
  739. case token of
  740. _FUNCTION :
  741. begin
  742. consume(_FUNCTION);
  743. if parse_proc_head(aclass,potype_none,pd) then
  744. begin
  745. { pd=nil when it is a interface mapping }
  746. if assigned(pd) then
  747. begin
  748. if try_to_consume(_COLON) then
  749. begin
  750. inc(testcurobject);
  751. single_type(pd.rettype,hs,false);
  752. pd.test_if_fpu_result;
  753. dec(testcurobject);
  754. end
  755. else
  756. begin
  757. if (
  758. parse_only and
  759. not(is_interface(pd._class))
  760. ) or
  761. (m_repeat_forward in aktmodeswitches) then
  762. begin
  763. consume(_COLON);
  764. consume_all_until(_SEMICOLON);
  765. end;
  766. end;
  767. if isclassmethod then
  768. include(pd.procoptions,po_classmethod);
  769. end;
  770. end
  771. else
  772. begin
  773. { recover }
  774. consume(_COLON);
  775. consume_all_until(_SEMICOLON);
  776. end;
  777. end;
  778. _PROCEDURE :
  779. begin
  780. consume(_PROCEDURE);
  781. if parse_proc_head(aclass,potype_none,pd) then
  782. begin
  783. { pd=nil when it is a interface mapping }
  784. if assigned(pd) then
  785. begin
  786. pd.rettype:=voidtype;
  787. if isclassmethod then
  788. include(pd.procoptions,po_classmethod);
  789. end;
  790. end;
  791. end;
  792. _CONSTRUCTOR :
  793. begin
  794. consume(_CONSTRUCTOR);
  795. parse_proc_head(aclass,potype_constructor,pd);
  796. if assigned(pd) and
  797. assigned(pd._class) then
  798. begin
  799. { Set return type, class constructors return the
  800. created instance, object constructors return boolean }
  801. if is_class(pd._class) then
  802. pd.rettype.setdef(pd._class)
  803. else
  804. pd.rettype:=booltype;
  805. end;
  806. end;
  807. _DESTRUCTOR :
  808. begin
  809. consume(_DESTRUCTOR);
  810. parse_proc_head(aclass,potype_destructor,pd);
  811. if assigned(pd) then
  812. pd.rettype:=voidtype;
  813. end;
  814. _OPERATOR :
  815. begin
  816. consume(_OPERATOR);
  817. if (token in [first_overloaded..last_overloaded]) then
  818. begin
  819. optoken:=token;
  820. end
  821. else
  822. begin
  823. Message(parser_e_overload_operator_failed);
  824. { Use the dummy NOTOKEN that is also declared
  825. for the overloaded_operator[] }
  826. optoken:=NOTOKEN;
  827. end;
  828. consume(token);
  829. parse_proc_head(aclass,potype_operator,pd);
  830. if assigned(pd) then
  831. begin
  832. if pd.parast.symtablelevel>normal_function_level then
  833. Message(parser_e_no_local_operator);
  834. if token<>_ID then
  835. begin
  836. if not(m_result in aktmodeswitches) then
  837. consume(_ID);
  838. end
  839. else
  840. begin
  841. pd.resultname:=orgpattern;
  842. consume(_ID);
  843. end;
  844. if not try_to_consume(_COLON) then
  845. begin
  846. consume(_COLON);
  847. pd.rettype:=generrortype;
  848. consume_all_until(_SEMICOLON);
  849. end
  850. else
  851. begin
  852. single_type(pd.rettype,hs,false);
  853. pd.test_if_fpu_result;
  854. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  855. ((pd.rettype.def.deftype<>orddef) or
  856. (torddef(pd.rettype.def).typ<>bool8bit)) then
  857. Message(parser_e_comparative_operator_return_boolean);
  858. if (optoken=_ASSIGNMENT) and
  859. equal_defs(pd.rettype.def,
  860. tvarsym(pd.parast.symindex.first).vartype.def) then
  861. message(parser_e_no_such_assignment)
  862. else if not isoperatoracceptable(pd,optoken) then
  863. Message(parser_e_overload_impossible);
  864. end;
  865. end
  866. else
  867. begin
  868. { recover }
  869. try_to_consume(_ID);
  870. consume(_COLON);
  871. consume_all_until(_SEMICOLON);
  872. end;
  873. end;
  874. end;
  875. { support procedure proc stdcall export; }
  876. if not(is_proc_directive(token,false)) then
  877. consume(_SEMICOLON);
  878. result:=pd;
  879. end;
  880. {****************************************************************************
  881. Procedure directive handlers
  882. ****************************************************************************}
  883. procedure pd_far(pd:tabstractprocdef);
  884. begin
  885. Message1(parser_w_proc_directive_ignored,'FAR');
  886. end;
  887. procedure pd_near(pd:tabstractprocdef);
  888. begin
  889. Message1(parser_w_proc_directive_ignored,'NEAR');
  890. end;
  891. procedure pd_export(pd:tabstractprocdef);
  892. begin
  893. if pd.deftype<>procdef then
  894. internalerror(200304264);
  895. if assigned(tprocdef(pd)._class) then
  896. Message(parser_e_methods_dont_be_export);
  897. if pd.parast.symtablelevel>normal_function_level then
  898. Message(parser_e_dont_nest_export);
  899. { only os/2 and emx need this }
  900. if target_info.system in [system_i386_os2,system_i386_emx] then
  901. begin
  902. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  903. if cs_link_deffile in aktglobalswitches then
  904. deffile.AddExport(tprocdef(pd).mangledname);
  905. end;
  906. end;
  907. procedure pd_forward(pd:tabstractprocdef);
  908. begin
  909. if pd.deftype<>procdef then
  910. internalerror(200304265);
  911. tprocdef(pd).forwarddef:=true;
  912. end;
  913. procedure pd_alias(pd:tabstractprocdef);
  914. begin
  915. if pd.deftype<>procdef then
  916. internalerror(200304266);
  917. consume(_COLON);
  918. tprocdef(pd).aliasnames.insert(get_stringconst);
  919. end;
  920. procedure pd_asmname(pd:tabstractprocdef);
  921. begin
  922. if pd.deftype<>procdef then
  923. internalerror(200304267);
  924. tprocdef(pd).setmangledname(target_info.Cprefix+pattern);
  925. if token=_CCHAR then
  926. consume(_CCHAR)
  927. else
  928. consume(_CSTRING);
  929. { we don't need anything else }
  930. tprocdef(pd).forwarddef:=false;
  931. end;
  932. procedure pd_inline(pd:tabstractprocdef);
  933. var
  934. hp : tparaitem;
  935. begin
  936. { check if there is an array of const }
  937. hp:=tparaitem(pd.para.first);
  938. while assigned(hp) do
  939. begin
  940. if assigned(hp.paratype.def) and
  941. (hp.paratype.def.deftype=arraydef) then
  942. begin
  943. with tarraydef(hp.paratype.def) do
  944. if IsVariant or IsConstructor {or IsArrayOfConst} then
  945. begin
  946. Message1(parser_w_not_supported_for_inline,'array of const');
  947. Message(parser_w_inlining_disabled);
  948. pd.proccalloption:=pocall_default;
  949. end;
  950. end;
  951. hp:=tparaitem(hp.next);
  952. end;
  953. end;
  954. procedure pd_intern(pd:tabstractprocdef);
  955. begin
  956. if pd.deftype<>procdef then
  957. internalerror(200304268);
  958. consume(_COLON);
  959. tprocdef(pd).extnumber:=get_intconst;
  960. end;
  961. procedure pd_interrupt(pd:tabstractprocdef);
  962. begin
  963. if pd.parast.symtablelevel>normal_function_level then
  964. Message(parser_e_dont_nest_interrupt);
  965. end;
  966. procedure pd_abstract(pd:tabstractprocdef);
  967. begin
  968. if pd.deftype<>procdef then
  969. internalerror(200304269);
  970. if (po_virtualmethod in pd.procoptions) then
  971. include(pd.procoptions,po_abstractmethod)
  972. else
  973. Message(parser_e_only_virtual_methods_abstract);
  974. { the method is defined }
  975. tprocdef(pd).forwarddef:=false;
  976. end;
  977. procedure pd_virtual(pd:tabstractprocdef);
  978. {$ifdef WITHDMT}
  979. var
  980. pt : tnode;
  981. {$endif WITHDMT}
  982. begin
  983. if pd.deftype<>procdef then
  984. internalerror(2003042610);
  985. if (pd.proctypeoption=potype_constructor) and
  986. is_object(tprocdef(pd)._class) then
  987. Message(parser_e_constructor_cannot_be_not_virtual);
  988. {$ifdef WITHDMT}
  989. if is_object(tprocdef(pd)._class) and
  990. (token<>_SEMICOLON) then
  991. begin
  992. { any type of parameter is allowed here! }
  993. pt:=comp_expr(true);
  994. if is_constintnode(pt) then
  995. begin
  996. include(pd.procoptions,po_msgint);
  997. pd.messageinf.i:=pt^.value;
  998. end
  999. else
  1000. Message(parser_e_ill_msg_expr);
  1001. disposetree(pt);
  1002. end;
  1003. {$endif WITHDMT}
  1004. end;
  1005. procedure pd_static(pd:tabstractprocdef);
  1006. begin
  1007. if (cs_static_keyword in aktmoduleswitches) then
  1008. begin
  1009. if pd.deftype=procdef then
  1010. include(tprocdef(pd).procsym.symoptions,sp_static);
  1011. include(pd.procoptions,po_staticmethod);
  1012. end;
  1013. end;
  1014. procedure pd_override(pd:tabstractprocdef);
  1015. begin
  1016. if pd.deftype<>procdef then
  1017. internalerror(2003042611);
  1018. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1019. Message(parser_e_no_object_override);
  1020. end;
  1021. procedure pd_overload(pd:tabstractprocdef);
  1022. begin
  1023. if pd.deftype<>procdef then
  1024. internalerror(2003042612);
  1025. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1026. end;
  1027. procedure pd_message(pd:tabstractprocdef);
  1028. var
  1029. pt : tnode;
  1030. begin
  1031. if pd.deftype<>procdef then
  1032. internalerror(2003042613);
  1033. if not is_class(tprocdef(pd)._class) then
  1034. Message(parser_e_msg_only_for_classes);
  1035. { check parameter type }
  1036. if ((pd.minparacount<>1) or
  1037. (pd.maxparacount<>1) or
  1038. (TParaItem(pd.Para.first).paratyp<>vs_var)) then
  1039. Message(parser_e_ill_msg_param);
  1040. pt:=comp_expr(true);
  1041. if pt.nodetype=stringconstn then
  1042. begin
  1043. include(pd.procoptions,po_msgstr);
  1044. tprocdef(pd).messageinf.str:=strnew(tstringconstnode(pt).value_str);
  1045. end
  1046. else
  1047. if is_constintnode(pt) then
  1048. begin
  1049. include(pd.procoptions,po_msgint);
  1050. tprocdef(pd).messageinf.i:=tordconstnode(pt).value;
  1051. end
  1052. else
  1053. Message(parser_e_ill_msg_expr);
  1054. pt.free;
  1055. end;
  1056. procedure pd_reintroduce(pd:tabstractprocdef);
  1057. begin
  1058. if pd.deftype<>procdef then
  1059. internalerror(200401211);
  1060. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1061. Message(parser_e_no_object_reintroduce);
  1062. end;
  1063. procedure pd_syscall(pd:tabstractprocdef);
  1064. {$ifdef powerpc}
  1065. var
  1066. sym : tsym;
  1067. symtable : tsymtable;
  1068. {$endif powerpc}
  1069. begin
  1070. if pd.deftype<>procdef then
  1071. internalerror(2003042614);
  1072. tprocdef(pd).forwarddef:=false;
  1073. {$ifdef powerpc}
  1074. if target_info.system in [system_powerpc_morphos,system_m68k_amiga] then
  1075. begin
  1076. pd.has_paraloc_info:=true;
  1077. include(pd.procoptions,po_explicitparaloc);
  1078. if consume_sym(sym,symtable) then
  1079. begin
  1080. if (sym.typ=varsym) and
  1081. ((tvarsym(sym).vartype.def.deftype=pointerdef) or
  1082. is_32bitint(tvarsym(sym).vartype.def)
  1083. ) then
  1084. begin
  1085. tprocdef(pd).libsym:=sym;
  1086. pd.concatpara(nil,tvarsym(sym).vartype,tvarsym(sym),nil,true);
  1087. paramanager.parseparaloc(tparaitem(pd.para.last),'A6');
  1088. end
  1089. else
  1090. Message(parser_e_32bitint_or_pointer_variable_expected);
  1091. end;
  1092. (paramanager as tppcparamanager).create_funcret_paraloc_info(pd,calleeside);
  1093. (paramanager as tppcparamanager).create_funcret_paraloc_info(pd,callerside);
  1094. end;
  1095. {$endif powerpc}
  1096. tprocdef(pd).extnumber:=get_intconst;
  1097. end;
  1098. procedure pd_external(pd:tabstractprocdef);
  1099. {
  1100. If import_dll=nil the procedure is assumed to be in another
  1101. object file. In that object file it should have the name to
  1102. which import_name is pointing to. Otherwise, the procedure is
  1103. assumed to be in the DLL to which import_dll is pointing to. In
  1104. that case either import_nr<>0 or import_name<>nil is true, so
  1105. the procedure is either imported by number or by name. (DM)
  1106. }
  1107. var
  1108. import_dll,
  1109. import_name : string;
  1110. import_nr : word;
  1111. begin
  1112. if pd.deftype<>procdef then
  1113. internalerror(2003042615);
  1114. tprocdef(pd).forwarddef:=false;
  1115. { forbid local external procedures }
  1116. if pd.parast.symtablelevel>normal_function_level then
  1117. Message(parser_e_no_local_external);
  1118. { If the procedure should be imported from a DLL, a constant string follows.
  1119. This isn't really correct, an contant string expression follows
  1120. so we check if an semicolon follows, else a string constant have to
  1121. follow (FK) }
  1122. import_nr:=0;
  1123. import_name:='';
  1124. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1125. begin
  1126. import_dll:=get_stringconst;
  1127. if (idtoken=_NAME) then
  1128. begin
  1129. consume(_NAME);
  1130. import_name:=get_stringconst;
  1131. end;
  1132. if (idtoken=_INDEX) then
  1133. begin
  1134. {After the word index follows the index number in the DLL.}
  1135. consume(_INDEX);
  1136. import_nr:=get_intconst;
  1137. end;
  1138. { default is to used the realname of the procedure }
  1139. if (import_nr=0) and (import_name='') then
  1140. import_name:=tprocdef(pd).procsym.realname;
  1141. { create importlib if not already done }
  1142. if not(current_module.uses_imports) then
  1143. begin
  1144. current_module.uses_imports:=true;
  1145. importlib.preparelib(current_module.modulename^);
  1146. end;
  1147. importlib.importprocedure(tprocdef(pd),import_dll,import_nr,import_name);
  1148. end
  1149. else
  1150. begin
  1151. if (idtoken=_NAME) then
  1152. begin
  1153. consume(_NAME);
  1154. import_name:=get_stringconst;
  1155. tprocdef(pd).setmangledname(import_name);
  1156. end
  1157. else if (m_mac in aktmodeswitches) and (token=_SEMICOLON) then
  1158. begin
  1159. {In MacPas a single "external" has the same effect as "external name 'xxx'"}
  1160. { don't do this if the procedure is also cdecl; in that case the c-prefix }
  1161. { must also be used (handled later) }
  1162. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1163. tprocdef(pd).setmangledname(tprocdef(pd).procsym.realname);
  1164. end;
  1165. end;
  1166. end;
  1167. type
  1168. pd_handler=procedure(pd:tabstractprocdef);
  1169. proc_dir_rec=record
  1170. idtok : ttoken;
  1171. pd_flags : tpdflags;
  1172. handler : pd_handler;
  1173. pocall : tproccalloption;
  1174. pooption : tprocoptions;
  1175. mutexclpocall : tproccalloptions;
  1176. mutexclpotype : tproctypeoptions;
  1177. mutexclpo : tprocoptions;
  1178. end;
  1179. const
  1180. {Should contain the number of procedure directives we support.}
  1181. num_proc_directives=36;
  1182. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1183. (
  1184. (
  1185. idtok:_ABSTRACT;
  1186. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1187. handler : @pd_abstract;
  1188. pocall : pocall_none;
  1189. pooption : [po_abstractmethod];
  1190. mutexclpocall : [pocall_internproc,pocall_inline];
  1191. mutexclpotype : [];
  1192. mutexclpo : [po_exports,po_interrupt,po_external]
  1193. ),(
  1194. idtok:_ALIAS;
  1195. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1196. handler : @pd_alias;
  1197. pocall : pocall_none;
  1198. pooption : [];
  1199. mutexclpocall : [pocall_inline];
  1200. mutexclpotype : [];
  1201. mutexclpo : [po_external]
  1202. ),(
  1203. idtok:_ASMNAME;
  1204. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1205. handler : @pd_asmname;
  1206. pocall : pocall_cdecl;
  1207. pooption : [po_external];
  1208. mutexclpocall : [pocall_internproc,pocall_inline];
  1209. mutexclpotype : [];
  1210. mutexclpo : [po_external]
  1211. ),(
  1212. idtok:_ASSEMBLER;
  1213. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1214. handler : nil;
  1215. pocall : pocall_none;
  1216. pooption : [po_assembler];
  1217. mutexclpocall : [];
  1218. mutexclpotype : [];
  1219. mutexclpo : [po_external]
  1220. ),(
  1221. idtok:_C; {same as cdecl for mode mac}
  1222. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1223. handler : nil;
  1224. pocall : pocall_cdecl;
  1225. pooption : [];
  1226. mutexclpocall : [];
  1227. mutexclpotype : [potype_constructor,potype_destructor];
  1228. mutexclpo : [po_assembler,po_external]
  1229. ),(
  1230. idtok:_CDECL;
  1231. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1232. handler : nil;
  1233. pocall : pocall_cdecl;
  1234. pooption : [];
  1235. mutexclpocall : [];
  1236. mutexclpotype : [potype_constructor,potype_destructor];
  1237. mutexclpo : [po_assembler,po_external]
  1238. ),(
  1239. idtok:_DYNAMIC;
  1240. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1241. handler : @pd_virtual;
  1242. pocall : pocall_none;
  1243. pooption : [po_virtualmethod];
  1244. mutexclpocall : [pocall_internproc,pocall_inline];
  1245. mutexclpotype : [];
  1246. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod]
  1247. ),(
  1248. idtok:_EXPORT;
  1249. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1250. handler : @pd_export;
  1251. pocall : pocall_none;
  1252. pooption : [po_exports,po_public];
  1253. mutexclpocall : [pocall_internproc,pocall_inline];
  1254. mutexclpotype : [potype_constructor,potype_destructor];
  1255. mutexclpo : [po_external,po_interrupt]
  1256. ),(
  1257. idtok:_EXTERNAL;
  1258. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf];
  1259. handler : @pd_external;
  1260. pocall : pocall_none;
  1261. pooption : [po_external];
  1262. mutexclpocall : [pocall_internproc,pocall_inline,pocall_syscall];
  1263. mutexclpotype : [potype_constructor,potype_destructor];
  1264. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1265. ),(
  1266. idtok:_FAR;
  1267. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1268. handler : @pd_far;
  1269. pocall : pocall_none;
  1270. pooption : [];
  1271. mutexclpocall : [pocall_internproc,pocall_inline];
  1272. mutexclpotype : [];
  1273. mutexclpo : []
  1274. ),(
  1275. idtok:_FAR16;
  1276. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1277. handler : nil;
  1278. pocall : pocall_far16;
  1279. pooption : [];
  1280. mutexclpocall : [];
  1281. mutexclpotype : [];
  1282. mutexclpo : [po_external]
  1283. ),(
  1284. idtok:_FORWARD;
  1285. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1286. handler : @pd_forward;
  1287. pocall : pocall_none;
  1288. pooption : [];
  1289. mutexclpocall : [pocall_internproc,pocall_inline];
  1290. mutexclpotype : [];
  1291. mutexclpo : [po_external]
  1292. ),(
  1293. idtok:_OLDFPCCALL;
  1294. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1295. handler : nil;
  1296. pocall : pocall_oldfpccall;
  1297. pooption : [];
  1298. mutexclpocall : [];
  1299. mutexclpotype : [];
  1300. mutexclpo : []
  1301. ),(
  1302. idtok:_INLINE;
  1303. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1304. handler : @pd_inline;
  1305. pocall : pocall_inline;
  1306. pooption : [];
  1307. mutexclpocall : [];
  1308. mutexclpotype : [potype_constructor,potype_destructor];
  1309. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1310. ),(
  1311. idtok:_INTERNCONST;
  1312. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1313. handler : @pd_intern;
  1314. pocall : pocall_none;
  1315. pooption : [po_internconst];
  1316. mutexclpocall : [];
  1317. mutexclpotype : [potype_operator];
  1318. mutexclpo : []
  1319. ),(
  1320. idtok:_INTERNPROC;
  1321. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1322. handler : @pd_intern;
  1323. pocall : pocall_internproc;
  1324. pooption : [];
  1325. mutexclpocall : [];
  1326. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1327. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1328. ),(
  1329. idtok:_INTERRUPT;
  1330. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1331. handler : @pd_interrupt;
  1332. pocall : pocall_none;
  1333. pooption : [po_interrupt];
  1334. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1335. pocall_inline,pocall_pascal,pocall_far16,pocall_oldfpccall];
  1336. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1337. mutexclpo : [po_external]
  1338. ),(
  1339. idtok:_IOCHECK;
  1340. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1341. handler : nil;
  1342. pocall : pocall_none;
  1343. pooption : [po_iocheck];
  1344. mutexclpocall : [pocall_internproc];
  1345. mutexclpotype : [];
  1346. mutexclpo : [po_external]
  1347. ),(
  1348. idtok:_MESSAGE;
  1349. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1350. handler : @pd_message;
  1351. pocall : pocall_none;
  1352. pooption : []; { can be po_msgstr or po_msgint }
  1353. mutexclpocall : [pocall_inline,pocall_internproc];
  1354. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1355. mutexclpo : [po_interrupt,po_external]
  1356. ),(
  1357. idtok:_NEAR;
  1358. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1359. handler : @pd_near;
  1360. pocall : pocall_none;
  1361. pooption : [];
  1362. mutexclpocall : [pocall_internproc];
  1363. mutexclpotype : [];
  1364. mutexclpo : []
  1365. ),(
  1366. idtok:_NOSTACKFRAME;
  1367. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1368. handler : nil;
  1369. pocall : pocall_none;
  1370. pooption : [po_nostackframe];
  1371. mutexclpocall : [pocall_internproc];
  1372. mutexclpotype : [];
  1373. mutexclpo : []
  1374. ),(
  1375. idtok:_OVERLOAD;
  1376. pd_flags : [pd_implemen,pd_interface,pd_body];
  1377. handler : @pd_overload;
  1378. pocall : pocall_none;
  1379. pooption : [po_overload];
  1380. mutexclpocall : [pocall_internproc];
  1381. mutexclpotype : [];
  1382. mutexclpo : []
  1383. ),(
  1384. idtok:_OVERRIDE;
  1385. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1386. handler : @pd_override;
  1387. pocall : pocall_none;
  1388. pooption : [po_overridingmethod,po_virtualmethod];
  1389. mutexclpocall : [pocall_inline,pocall_internproc];
  1390. mutexclpotype : [];
  1391. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1392. ),(
  1393. idtok:_PASCAL;
  1394. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1395. handler : nil;
  1396. pocall : pocall_pascal;
  1397. pooption : [];
  1398. mutexclpocall : [];
  1399. mutexclpotype : [potype_constructor,potype_destructor];
  1400. mutexclpo : [po_external]
  1401. ),(
  1402. idtok:_PUBLIC;
  1403. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1404. handler : nil;
  1405. pocall : pocall_none;
  1406. pooption : [po_public];
  1407. mutexclpocall : [pocall_internproc,pocall_inline];
  1408. mutexclpotype : [];
  1409. mutexclpo : [po_external]
  1410. ),(
  1411. idtok:_REGISTER;
  1412. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1413. handler : nil;
  1414. pocall : pocall_register;
  1415. pooption : [];
  1416. mutexclpocall : [];
  1417. mutexclpotype : [potype_constructor,potype_destructor];
  1418. mutexclpo : [po_external]
  1419. ),(
  1420. idtok:_REINTRODUCE;
  1421. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1422. handler : @pd_reintroduce;
  1423. pocall : pocall_none;
  1424. pooption : [po_reintroduce];
  1425. mutexclpocall : [pocall_inline,pocall_internproc];
  1426. mutexclpotype : [];
  1427. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1428. ),(
  1429. idtok:_SAFECALL;
  1430. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1431. handler : nil;
  1432. pocall : pocall_safecall;
  1433. pooption : [];
  1434. mutexclpocall : [];
  1435. mutexclpotype : [potype_constructor,potype_destructor];
  1436. mutexclpo : [po_external]
  1437. ),(
  1438. idtok:_SOFTFLOAT;
  1439. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1440. handler : nil;
  1441. pocall : pocall_softfloat;
  1442. pooption : [];
  1443. mutexclpocall : [];
  1444. mutexclpotype : [potype_constructor,potype_destructor];
  1445. { it's available with po_external because the libgcc floating point routines on the arm
  1446. uses this calling convention }
  1447. mutexclpo : []
  1448. ),(
  1449. idtok:_STATIC;
  1450. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1451. handler : @pd_static;
  1452. pocall : pocall_none;
  1453. pooption : [po_staticmethod];
  1454. mutexclpocall : [pocall_inline,pocall_internproc];
  1455. mutexclpotype : [potype_constructor,potype_destructor];
  1456. mutexclpo : [po_external,po_interrupt,po_exports]
  1457. ),(
  1458. idtok:_STDCALL;
  1459. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1460. handler : nil;
  1461. pocall : pocall_stdcall;
  1462. pooption : [];
  1463. mutexclpocall : [];
  1464. mutexclpotype : [potype_constructor,potype_destructor];
  1465. mutexclpo : [po_external]
  1466. ),(
  1467. idtok:_SYSCALL;
  1468. pd_flags : [pd_interface,pd_implemen,pd_notobject,pd_notobjintf];
  1469. handler : @pd_syscall;
  1470. pocall : pocall_syscall;
  1471. pooption : [];
  1472. mutexclpocall : [];
  1473. mutexclpotype : [potype_constructor,potype_destructor];
  1474. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1475. ),(
  1476. idtok:_VIRTUAL;
  1477. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1478. handler : @pd_virtual;
  1479. pocall : pocall_none;
  1480. pooption : [po_virtualmethod];
  1481. mutexclpocall : [pocall_inline,pocall_internproc];
  1482. mutexclpotype : [];
  1483. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1484. ),(
  1485. idtok:_CPPDECL;
  1486. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1487. handler : nil;
  1488. pocall : pocall_cppdecl;
  1489. pooption : [];
  1490. mutexclpocall : [];
  1491. mutexclpotype : [potype_constructor,potype_destructor];
  1492. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1493. ),(
  1494. idtok:_VARARGS;
  1495. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1496. handler : nil;
  1497. pocall : pocall_none;
  1498. pooption : [po_varargs];
  1499. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1500. pocall_inline,pocall_far16,pocall_oldfpccall];
  1501. mutexclpotype : [];
  1502. mutexclpo : [po_assembler,po_interrupt]
  1503. ),(
  1504. idtok:_COMPILERPROC;
  1505. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1506. handler : nil;
  1507. pocall : pocall_compilerproc;
  1508. pooption : [];
  1509. mutexclpocall : [];
  1510. mutexclpotype : [potype_constructor,potype_destructor];
  1511. mutexclpo : [po_interrupt]
  1512. )
  1513. );
  1514. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  1515. var
  1516. i : longint;
  1517. begin
  1518. is_proc_directive:=false;
  1519. for i:=1 to num_proc_directives do
  1520. if proc_direcdata[i].idtok=idtoken then
  1521. begin
  1522. if ((not isprocvar) or
  1523. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1524. { don't eat a public directive in classes }
  1525. not((proc_direcdata[i].idtok=_PUBLIC) and (symtablestack.symtabletype=objectsymtable)) then
  1526. is_proc_directive:=true;
  1527. exit;
  1528. end;
  1529. end;
  1530. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1531. {
  1532. Parse the procedure directive, returns true if a correct directive is found
  1533. }
  1534. var
  1535. p : longint;
  1536. found : boolean;
  1537. name : stringid;
  1538. begin
  1539. parse_proc_direc:=false;
  1540. name:=tokeninfo^[idtoken].str;
  1541. found:=false;
  1542. { Hint directive? Then exit immediatly }
  1543. if (m_hintdirective in aktmodeswitches) then
  1544. begin
  1545. case idtoken of
  1546. _LIBRARY,
  1547. _PLATFORM,
  1548. _UNIMPLEMENTED,
  1549. _DEPRECATED :
  1550. exit;
  1551. end;
  1552. end;
  1553. { C directive is MAC only, because it breaks too much existing code
  1554. on other platforms (PFV) }
  1555. if (idtoken=_C) and
  1556. not(m_mac in aktmodeswitches) then
  1557. exit;
  1558. { retrieve data for directive if found }
  1559. for p:=1 to num_proc_directives do
  1560. if proc_direcdata[p].idtok=idtoken then
  1561. begin
  1562. found:=true;
  1563. break;
  1564. end;
  1565. { Check if the procedure directive is known }
  1566. if not found then
  1567. begin
  1568. { parsing a procvar type the name can be any
  1569. next variable !! }
  1570. if ((pdflags * [pd_procvar,pd_object])=[]) and
  1571. not(idtoken=_PROPERTY) then
  1572. Message1(parser_w_unknown_proc_directive_ignored,name);
  1573. exit;
  1574. end;
  1575. { static needs a special treatment }
  1576. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1577. exit;
  1578. { Conflicts between directives ? }
  1579. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1580. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1581. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1582. begin
  1583. Message1(parser_e_proc_dir_conflict,name);
  1584. exit;
  1585. end;
  1586. { set calling convention }
  1587. if proc_direcdata[p].pocall<>pocall_none then
  1588. begin
  1589. if (po_hascallingconvention in pd.procoptions) then
  1590. begin
  1591. Message2(parser_w_proc_overriding_calling,
  1592. proccalloptionStr[pd.proccalloption],
  1593. proccalloptionStr[proc_direcdata[p].pocall]);
  1594. end;
  1595. { check if the target processor supports this calling convention }
  1596. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1597. begin
  1598. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1599. { recover }
  1600. proc_direcdata[p].pocall:=pocall_stdcall;
  1601. end;
  1602. pd.proccalloption:=proc_direcdata[p].pocall;
  1603. include(pd.procoptions,po_hascallingconvention);
  1604. end;
  1605. { check if method and directive not for object, like public.
  1606. This needs to be checked also for procvars }
  1607. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1608. (pd.owner.symtabletype=objectsymtable) then
  1609. exit;
  1610. if pd.deftype=procdef then
  1611. begin
  1612. { Check if the directive is only for objects }
  1613. if (pd_object in proc_direcdata[p].pd_flags) and
  1614. not assigned(tprocdef(pd)._class) then
  1615. exit;
  1616. { check if method and directive not for interface }
  1617. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1618. is_interface(tprocdef(pd)._class) then
  1619. exit;
  1620. end
  1621. { don't eat public in object/class declarions }
  1622. else if (pd.deftype=procvardef) and (proc_direcdata[p].idtok=_PUBLIC) and
  1623. (symtablestack.symtabletype=objectsymtable) then
  1624. exit;
  1625. { consume directive, and turn flag on }
  1626. consume(token);
  1627. parse_proc_direc:=true;
  1628. { Check the pd_flags if the directive should be allowed }
  1629. if (pd_interface in pdflags) and
  1630. not(pd_interface in proc_direcdata[p].pd_flags) then
  1631. begin
  1632. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1633. exit;
  1634. end;
  1635. if (pd_implemen in pdflags) and
  1636. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1637. begin
  1638. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1639. exit;
  1640. end;
  1641. if (pd_procvar in pdflags) and
  1642. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1643. begin
  1644. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1645. exit;
  1646. end;
  1647. { Return the new pd_flags }
  1648. if not(pd_body in proc_direcdata[p].pd_flags) then
  1649. exclude(pdflags,pd_body);
  1650. { Add the correct flag }
  1651. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1652. { Call the handler }
  1653. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1654. proc_direcdata[p].handler(pd);
  1655. end;
  1656. procedure handle_calling_convention(pd:tabstractprocdef);
  1657. begin
  1658. { set the default calling convention if none provided }
  1659. if not(po_hascallingconvention in pd.procoptions) then
  1660. pd.proccalloption:=aktdefproccall
  1661. else
  1662. begin
  1663. if pd.proccalloption=pocall_none then
  1664. internalerror(200309081);
  1665. end;
  1666. { handle proccall specific settings }
  1667. case pd.proccalloption of
  1668. pocall_cdecl :
  1669. begin
  1670. { set mangledname }
  1671. if (pd.deftype=procdef) then
  1672. begin
  1673. if not tprocdef(pd).has_mangledname then
  1674. begin
  1675. if assigned(tprocdef(pd)._class) then
  1676. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd)._class.objrealname^+'_'+tprocdef(pd).procsym.realname)
  1677. else
  1678. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).procsym.realname);
  1679. end;
  1680. { check C cdecl para types }
  1681. pd.parast.foreach_static(@check_c_para,nil);
  1682. end;
  1683. end;
  1684. pocall_cppdecl :
  1685. begin
  1686. { set mangledname }
  1687. if (pd.deftype=procdef) then
  1688. begin
  1689. if not tprocdef(pd).has_mangledname then
  1690. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).cplusplusmangledname);
  1691. { check C cdecl para types }
  1692. pd.parast.foreach_static(@check_c_para,nil);
  1693. end;
  1694. end;
  1695. pocall_compilerproc :
  1696. begin
  1697. if (pd.deftype<>procdef) then
  1698. internalerror(200110232);
  1699. if (target_info.system<>system_i386_watcom) then
  1700. tprocdef(pd).setmangledname(lower(tprocdef(pd).procsym.name));
  1701. end;
  1702. pocall_far16 :
  1703. begin
  1704. { Temporary stub, must be rewritten to support OS/2 far16 }
  1705. Message1(parser_w_proc_directive_ignored,'FAR16');
  1706. end;
  1707. pocall_inline :
  1708. begin
  1709. if not(cs_support_inline in aktmoduleswitches) then
  1710. begin
  1711. Message(parser_e_proc_inline_not_supported);
  1712. pd.proccalloption:=pocall_default;
  1713. end;
  1714. end;
  1715. end;
  1716. { For varargs directive also cdecl and external must be defined }
  1717. if (po_varargs in pd.procoptions) then
  1718. begin
  1719. { check first for external in the interface, if available there
  1720. then the cdecl must also be there since there is no implementation
  1721. available to contain it }
  1722. if parse_only then
  1723. begin
  1724. { if external is available, then cdecl must also be available,
  1725. procvars don't need external }
  1726. if not((po_external in pd.procoptions) or
  1727. (pd.deftype=procvardef)) and
  1728. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1729. Message(parser_e_varargs_need_cdecl_and_external);
  1730. end
  1731. else
  1732. begin
  1733. { both must be defined now }
  1734. if not((po_external in pd.procoptions) or
  1735. (pd.deftype=procvardef)) or
  1736. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1737. Message(parser_e_varargs_need_cdecl_and_external);
  1738. end;
  1739. end;
  1740. { Make var parameters regable, this must be done after the calling
  1741. convention is set. }
  1742. pd.parast.foreach_static(@set_addr_param_regable,pd);
  1743. { add mangledname to external list }
  1744. if (pd.deftype=procdef) and
  1745. (po_external in pd.procoptions) and
  1746. target_info.DllScanSupported then
  1747. current_module.externals.insert(tExternalsItem.create(tprocdef(pd).mangledname));
  1748. end;
  1749. procedure calc_parast(pd:tabstractprocdef);
  1750. var
  1751. currpara : tparaitem;
  1752. begin
  1753. { insert hidden high parameters }
  1754. insert_hidden_para(pd);
  1755. { insert hidden self parameter }
  1756. insert_self_and_vmt_para(pd);
  1757. { insert funcret parameter if required }
  1758. insert_funcret_para(pd);
  1759. { insert parentfp parameter if required }
  1760. insert_parentfp_para(pd);
  1761. currpara:=tparaitem(pd.para.first);
  1762. while assigned(currpara) do
  1763. begin
  1764. if not(assigned(currpara.parasym) and (currpara.parasym.typ=varsym)) then
  1765. internalerror(200304232);
  1766. { connect parasym to paraitem }
  1767. tvarsym(currpara.parasym).paraitem:=currpara;
  1768. { We need a local copy for a value parameter when only the
  1769. address is pushed. Open arrays and Array of Const are
  1770. an exception because they are allocated at runtime and the
  1771. address that is pushed is patched }
  1772. if (currpara.paratyp=vs_value) and
  1773. paramanager.push_addr_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) and
  1774. not(is_open_array(currpara.paratype.def) or
  1775. is_array_of_const(currpara.paratype.def)) then
  1776. include(tvarsym(currpara.parasym).varoptions,vo_has_local_copy);
  1777. currpara:=tparaitem(currpara.next);
  1778. end;
  1779. end;
  1780. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  1781. {
  1782. Parse the procedure directives. It does not matter if procedure directives
  1783. are written using ;procdir; or ['procdir'] syntax.
  1784. }
  1785. var
  1786. res : boolean;
  1787. begin
  1788. if (m_mac in aktmodeswitches) and (cs_externally_visible in aktlocalswitches) then
  1789. begin
  1790. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  1791. include(pd.procoptions,po_public);
  1792. end;
  1793. while token in [_ID,_LECKKLAMMER] do
  1794. begin
  1795. if try_to_consume(_LECKKLAMMER) then
  1796. begin
  1797. repeat
  1798. parse_proc_direc(pd,pdflags);
  1799. until not try_to_consume(_COMMA);
  1800. consume(_RECKKLAMMER);
  1801. { we always expect at least '[];' }
  1802. res:=true;
  1803. end
  1804. else
  1805. begin
  1806. res:=parse_proc_direc(pd,pdflags);
  1807. end;
  1808. { A procedure directive normally followed by a semicolon, but in
  1809. a const section or reading a type we should stop when _EQUAL is found,
  1810. because a constant/default value follows }
  1811. if res then
  1812. begin
  1813. if (block_type in [bt_const,bt_type]) and
  1814. (token=_EQUAL) then
  1815. break;
  1816. { support procedure proc;stdcall export; }
  1817. if not(is_proc_directive(token,(pd.deftype=procvardef))) then
  1818. consume(_SEMICOLON);
  1819. end
  1820. else
  1821. break;
  1822. end;
  1823. end;
  1824. procedure parse_var_proc_directives(sym:tsym);
  1825. var
  1826. pdflags : tpdflags;
  1827. pd : tabstractprocdef;
  1828. begin
  1829. pdflags:=[pd_procvar];
  1830. pd:=nil;
  1831. case sym.typ of
  1832. varsym :
  1833. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1834. typedconstsym :
  1835. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1836. typesym :
  1837. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1838. else
  1839. internalerror(2003042617);
  1840. end;
  1841. if pd.deftype<>procvardef then
  1842. internalerror(2003042618);
  1843. { names should never be used anyway }
  1844. parse_proc_directives(pd,pdflags);
  1845. end;
  1846. procedure parse_object_proc_directives(pd:tabstractprocdef);
  1847. var
  1848. pdflags : tpdflags;
  1849. begin
  1850. pdflags:=[pd_object];
  1851. parse_proc_directives(pd,pdflags);
  1852. end;
  1853. function proc_add_definition(var pd:tprocdef):boolean;
  1854. {
  1855. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1856. forwarddef is found and reused it returns true
  1857. }
  1858. var
  1859. hd : tprocdef;
  1860. ad,fd : tsym;
  1861. s1,s2 : stringid;
  1862. i : cardinal;
  1863. forwardfound : boolean;
  1864. po_comp : tprocoptions;
  1865. aprocsym : tprocsym;
  1866. begin
  1867. forwardfound:=false;
  1868. aprocsym:=tprocsym(pd.procsym);
  1869. { check overloaded functions if the same function already exists }
  1870. for i:=1 to aprocsym.procdef_count do
  1871. begin
  1872. hd:=aprocsym.procdef[i];
  1873. { Skip overloaded definitions that are declared in other
  1874. units }
  1875. if hd.procsym<>aprocsym then
  1876. continue;
  1877. { check the parameters, for delphi/tp it is possible to
  1878. leave the parameters away in the implementation (forwarddef=false).
  1879. But for an overload declared function this is not allowed }
  1880. if { check if empty implementation arguments match is allowed }
  1881. (
  1882. not(m_repeat_forward in aktmodeswitches) and
  1883. not(pd.forwarddef) and
  1884. (pd.maxparacount=0) and
  1885. not(po_overload in hd.procoptions)
  1886. ) or
  1887. { check arguments }
  1888. (
  1889. (compare_paras(pd.para,hd.para,cp_none,[cpo_comparedefaultvalue])>=te_equal) and
  1890. { for operators equal_paras is not enough !! }
  1891. ((pd.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1892. equal_defs(hd.rettype.def,pd.rettype.def))
  1893. ) then
  1894. begin
  1895. { Check if we've found the forwarddef, if found then
  1896. we need to update the forward def with the current
  1897. implementation settings }
  1898. if hd.forwarddef then
  1899. begin
  1900. forwardfound:=true;
  1901. { Check if the procedure type and return type are correct,
  1902. also the parameters must match also with the type }
  1903. if (hd.proctypeoption<>pd.proctypeoption) or
  1904. (
  1905. (m_repeat_forward in aktmodeswitches) and
  1906. (not((pd.maxparacount=0) or
  1907. (compare_paras(pd.para,hd.para,cp_all,[cpo_comparedefaultvalue])>=te_equal)))
  1908. ) or
  1909. (
  1910. ((m_repeat_forward in aktmodeswitches) or
  1911. not(is_void(pd.rettype.def))) and
  1912. (not equal_defs(hd.rettype.def,pd.rettype.def))) then
  1913. begin
  1914. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1915. pd.fullprocname(false));
  1916. aprocsym.write_parameter_lists(pd);
  1917. break;
  1918. end;
  1919. { Check if both are declared forward }
  1920. if hd.forwarddef and pd.forwarddef then
  1921. begin
  1922. MessagePos1(pd.fileinfo,parser_e_function_already_declared_public_forward,
  1923. pd.fullprocname(false));
  1924. end;
  1925. { internconst or internproc only need to be defined once }
  1926. if (hd.proccalloption=pocall_internproc) then
  1927. pd.proccalloption:=hd.proccalloption
  1928. else
  1929. if (pd.proccalloption=pocall_internproc) then
  1930. hd.proccalloption:=pd.proccalloption;
  1931. if (po_internconst in hd.procoptions) then
  1932. include(pd.procoptions,po_internconst)
  1933. else if (po_internconst in pd.procoptions) then
  1934. include(hd.procoptions,po_internconst);
  1935. { Check calling convention }
  1936. if (hd.proccalloption<>pd.proccalloption) then
  1937. begin
  1938. { In delphi it is possible to specify the calling
  1939. convention in the interface or implementation if
  1940. there was no convention specified in the other
  1941. part }
  1942. if (m_delphi in aktmodeswitches) then
  1943. begin
  1944. if not(po_hascallingconvention in pd.procoptions) then
  1945. pd.proccalloption:=hd.proccalloption
  1946. else
  1947. if not(po_hascallingconvention in hd.procoptions) then
  1948. hd.proccalloption:=pd.proccalloption
  1949. else
  1950. begin
  1951. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1952. aprocsym.write_parameter_lists(pd);
  1953. { restore interface settings }
  1954. pd.proccalloption:=hd.proccalloption;
  1955. end;
  1956. end
  1957. else
  1958. begin
  1959. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1960. aprocsym.write_parameter_lists(pd);
  1961. { restore interface settings }
  1962. pd.proccalloption:=hd.proccalloption;
  1963. end;
  1964. end;
  1965. { Check procedure options, Delphi requires that class is
  1966. repeated in the implementation for class methods }
  1967. if (m_fpc in aktmodeswitches) then
  1968. po_comp:=[po_varargs,po_methodpointer,po_interrupt]
  1969. else
  1970. po_comp:=[po_classmethod,po_methodpointer];
  1971. if ((po_comp * hd.procoptions)<>(po_comp * pd.procoptions)) then
  1972. begin
  1973. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1974. pd.fullprocname(false));
  1975. aprocsym.write_parameter_lists(pd);
  1976. { This error is non-fatal, we can recover }
  1977. end;
  1978. { Check manglednames }
  1979. if (m_repeat_forward in aktmodeswitches) or
  1980. (pd.minparacount>0) then
  1981. begin
  1982. { If mangled names are equal then they have the same amount of arguments }
  1983. { We can check the names of the arguments }
  1984. { both symtables are in the same order from left to right }
  1985. ad:=tsym(hd.parast.symindex.first);
  1986. fd:=tsym(pd.parast.symindex.first);
  1987. repeat
  1988. { skip default parameter constsyms }
  1989. while assigned(ad) and (ad.typ<>varsym) do
  1990. ad:=tsym(ad.indexnext);
  1991. while assigned(fd) and (fd.typ<>varsym) do
  1992. fd:=tsym(fd.indexnext);
  1993. { stop when one of the two lists is at the end }
  1994. if not assigned(ad) or not assigned(fd) then
  1995. break;
  1996. { retrieve names, remove reg for register parameters }
  1997. s1:=ad.name;
  1998. s2:=fd.name;
  1999. { compare names }
  2000. if (s1<>s2) then
  2001. begin
  2002. MessagePos3(pd.fileinfo,parser_e_header_different_var_names,
  2003. aprocsym.name,s1,s2);
  2004. break;
  2005. end;
  2006. ad:=tsym(ad.indexnext);
  2007. fd:=tsym(fd.indexnext);
  2008. until false;
  2009. if assigned(ad) xor assigned(fd) then
  2010. internalerror(200204178);
  2011. end;
  2012. { Everything is checked, now we can update the forward declaration
  2013. with the new data from the implementation }
  2014. hd.forwarddef:=pd.forwarddef;
  2015. hd.hasforward:=true;
  2016. hd.procoptions:=hd.procoptions+pd.procoptions;
  2017. if hd.extnumber=65535 then
  2018. hd.extnumber:=pd.extnumber;
  2019. while not pd.aliasnames.empty do
  2020. hd.aliasnames.insert(pd.aliasnames.getfirst);
  2021. { update fileinfo so position references the implementation,
  2022. also update funcretsym if it is already generated }
  2023. hd.fileinfo:=pd.fileinfo;
  2024. if assigned(hd.funcretsym) then
  2025. hd.funcretsym.fileinfo:=pd.fileinfo;
  2026. { update mangledname if the implementation has a fixed mangledname set }
  2027. if pd.has_mangledname then
  2028. begin
  2029. { rename also asmsymbol first, because the name can already be used }
  2030. objectlibrary.renameasmsymbol(hd.mangledname,pd.mangledname);
  2031. hd.setmangledname(pd.mangledname);
  2032. end;
  2033. { for compilerproc defines we need to rename and update the
  2034. symbolname to lowercase }
  2035. if (pd.proccalloption=pocall_compilerproc) then
  2036. begin
  2037. { rename to lowercase so users can't access it }
  2038. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  2039. { also update the realname that is stored in the ppu }
  2040. stringdispose(aprocsym._realname);
  2041. aprocsym._realname:=stringdup('$'+aprocsym.name);
  2042. { the mangeled name is already changed by the pd_compilerproc }
  2043. { handler. It must be done immediately because if we have a }
  2044. { call to a compilerproc before it's implementation is }
  2045. { encountered, it must already use the new mangled name (JM) }
  2046. end;
  2047. { the procdef will be released by the symtable, we release
  2048. at least the parast }
  2049. pd.releasemem;
  2050. pd:=hd;
  2051. end
  2052. else
  2053. begin
  2054. { abstract methods aren't forward defined, but this }
  2055. { needs another error message }
  2056. if (po_abstractmethod in hd.procoptions) then
  2057. MessagePos(pd.fileinfo,parser_e_abstract_no_definition)
  2058. else
  2059. MessagePos(pd.fileinfo,parser_e_overloaded_have_same_parameters);
  2060. end;
  2061. { we found one proc with the same arguments, there are no others
  2062. so we can stop }
  2063. break;
  2064. end;
  2065. { check for allowing overload directive }
  2066. if not(m_fpc in aktmodeswitches) then
  2067. begin
  2068. { overload directive turns on overloading }
  2069. if ((po_overload in pd.procoptions) or
  2070. (po_overload in hd.procoptions)) then
  2071. begin
  2072. { check if all procs have overloading, but not if the proc is a method or
  2073. already declared forward, then the check is already done }
  2074. if not(hd.hasforward or
  2075. assigned(pd._class) or
  2076. (pd.forwarddef<>hd.forwarddef) or
  2077. ((po_overload in pd.procoptions) and
  2078. (po_overload in hd.procoptions))) then
  2079. begin
  2080. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  2081. break;
  2082. end;
  2083. end
  2084. else
  2085. begin
  2086. if not(hd.forwarddef) then
  2087. begin
  2088. MessagePos(pd.fileinfo,parser_e_procedure_overloading_is_off);
  2089. break;
  2090. end;
  2091. end;
  2092. end; { equal arguments }
  2093. end;
  2094. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2095. list }
  2096. if not forwardfound then
  2097. begin
  2098. aprocsym.addprocdef(pd);
  2099. { add overloadnumber for unique naming, the overloadcount is
  2100. counted per module and 0 for the first procedure }
  2101. pd.overloadnumber:=aprocsym.overloadcount;
  2102. inc(aprocsym.overloadcount);
  2103. end;
  2104. proc_add_definition:=forwardfound;
  2105. end;
  2106. end.
  2107. {
  2108. $Log$
  2109. Revision 1.197 2004-10-24 20:01:08 peter
  2110. * remove saveregister calling convention
  2111. Revision 1.196 2004/10/24 13:48:50 peter
  2112. * don't give warning for property as unknwon proc directive
  2113. Revision 1.195 2004/10/24 11:44:28 peter
  2114. * small regvar fixes
  2115. * loadref parameter removed from concatcopy,incrrefcount,etc
  2116. Revision 1.194 2004/10/15 09:14:17 mazen
  2117. - remove $IFDEF DELPHI and related code
  2118. - remove $IFDEF FPCPROCVAR and related code
  2119. Revision 1.193 2004/10/11 15:45:35 peter
  2120. * mark non-regable after calling convention is set
  2121. Revision 1.192 2004/10/10 21:08:55 peter
  2122. * parameter regvar fixes
  2123. Revision 1.191 2004/10/08 17:09:43 peter
  2124. * tvarsym.varregable added, split vo_regable from varoptions
  2125. Revision 1.190 2004/08/29 11:28:41 peter
  2126. fixed crash with error in default value
  2127. allow assembler directive in interface
  2128. Revision 1.189 2004/08/25 15:57:19 peter
  2129. * fix for tw3261
  2130. Revision 1.188 2004/08/22 20:11:38 florian
  2131. * morphos now takes any pointer var. as libbase
  2132. * alignment for sparc fixed
  2133. * int -> double conversion on sparc fixed
  2134. Revision 1.187 2004/08/22 11:24:27 peter
  2135. * don't insert result variables for constructor/destructors
  2136. Revision 1.186 2004/08/13 17:53:37 jonas
  2137. * only set the mangled name immediately for external procedures in macpas
  2138. mode if the procedure isn't cdecl (so that the c-prefix is taken into
  2139. account, necessary for Mac OS X)
  2140. Revision 1.185 2004/08/08 12:35:09 florian
  2141. * proc. var declarations in a class doesn't eat a public anymore
  2142. Revision 1.184 2004/07/17 13:51:57 florian
  2143. * function result location for syscalls on MOS hopefully correctly set now
  2144. Revision 1.183 2004/07/14 23:19:21 olle
  2145. + added external facilities for macpas
  2146. Revision 1.182 2004/06/20 08:55:30 florian
  2147. * logs truncated
  2148. Revision 1.181 2004/06/16 20:07:09 florian
  2149. * dwarf branch merged
  2150. Revision 1.180 2004/05/23 20:54:39 peter
  2151. * fixed 3114
  2152. Revision 1.179 2004/05/23 19:06:26 peter
  2153. * expect : after function when it is a forwarddef
  2154. Revision 1.178 2004/05/12 13:21:09 karoly
  2155. * few small changes to add syscall support to M68k/Amiga target
  2156. Revision 1.177 2004/05/11 22:52:48 olle
  2157. * Moved import_implicit_external to symsym
  2158. }