pdecsub.pas 83 KB

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