pdecsub.pas 83 KB

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