pdecsub.pas 83 KB

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