pdecsub.pas 82 KB

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