psub.pas 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 psub;
  19. interface
  20. uses
  21. cobjects,
  22. symconst,tokens,symtable;
  23. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40;{ directive can not be used object declaration }
  31. procedure compile_proc_body(const proc_names:Tstringcontainer;
  32. make_global,parent_has_class:boolean);
  33. procedure parse_proc_head(options:tproctypeoption);
  34. procedure parse_proc_dec;
  35. function is_proc_directive(tok:ttoken):boolean;
  36. procedure parse_var_proc_directives(var sym : psym);
  37. procedure parse_object_proc_directives(var sym : pprocsym);
  38. procedure read_proc;
  39. implementation
  40. uses
  41. globtype,systems,
  42. strings,globals,verbose,files,
  43. scanner,aasm,tree,types,
  44. import,gendef,
  45. {$ifdef newcg}
  46. cgbase,
  47. {$else newcg}
  48. hcodegen,temp_gen,
  49. {$endif newcg}
  50. pass_1,cpubase,cpuasm
  51. {$ifndef NOPASS2}
  52. ,pass_2
  53. {$endif}
  54. {$ifdef GDB}
  55. ,gdb
  56. {$endif GDB}
  57. {$ifdef newcg}
  58. {$ifndef NOOPT}
  59. ,aopt
  60. {$endif}
  61. {$else}
  62. {$ifdef i386}
  63. ,tgeni386
  64. ,cgai386
  65. {$ifndef NOOPT}
  66. ,aopt386
  67. {$endif}
  68. {$endif}
  69. {$ifdef m68k}
  70. ,tgen68k,cga68k
  71. {$endif}
  72. {$endif newcg}
  73. { parser specific stuff }
  74. ,pbase,ptype,pdecl,pexpr,pstatmnt
  75. {$ifdef newcg}
  76. ,tgcpu,convtree,cgobj,tgeni386 { for the new code generator tgeni386 is only a dummy }
  77. {$endif newcg}
  78. ;
  79. var
  80. realname:string; { contains the real name of a procedure as it's typed }
  81. procedure parse_proc_head(options:tproctypeoption);
  82. var sp:stringid;
  83. pd:Pprocdef;
  84. paramoffset:longint;
  85. sym:Psym;
  86. hs:string;
  87. st : psymtable;
  88. overloaded_level:word;
  89. storepos,procstartfilepos : tfileposinfo;
  90. begin
  91. { Save the position where this procedure really starts and set col to 1 which
  92. looks nicer }
  93. procstartfilepos:=tokenpos;
  94. { procstartfilepos.column:=1; I do not agree here !!
  95. lets keep excat position PM }
  96. if (options=potype_operator) then
  97. begin
  98. sp:=overloaded_names[optoken];
  99. realname:=sp;
  100. end
  101. else
  102. begin
  103. sp:=pattern;
  104. realname:=orgpattern;
  105. consume(_ID);
  106. end;
  107. { method ? }
  108. if not(parse_only) and
  109. (lexlevel=normal_function_level) and
  110. try_to_consume(_POINT) then
  111. begin
  112. storepos:=tokenpos;
  113. tokenpos:=procstartfilepos;
  114. getsym(sp,true);
  115. sym:=srsym;
  116. tokenpos:=storepos;
  117. { load proc name }
  118. sp:=pattern;
  119. realname:=orgpattern;
  120. procstartfilepos:=tokenpos;
  121. { qualifier is class name ? }
  122. if (sym^.typ<>typesym) or
  123. (ptypesym(sym)^.restype.def^.deftype<>objectdef) then
  124. begin
  125. Message(parser_e_class_id_expected);
  126. aktprocsym:=nil;
  127. consume(_ID);
  128. end
  129. else
  130. begin
  131. { used to allow private syms to be seen }
  132. aktobjectdef:=pobjectdef(ptypesym(sym)^.restype.def);
  133. procinfo^._class:=pobjectdef(ptypesym(sym)^.restype.def);
  134. aktprocsym:=pprocsym(procinfo^._class^.symtable^.search(sp));
  135. consume(_ID);
  136. {The procedure has been found. So it is
  137. a global one. Set the flags to mark this.}
  138. procinfo^.flags:=procinfo^.flags or pi_is_global;
  139. aktobjectdef:=nil;
  140. { we solve this below }
  141. if not(assigned(aktprocsym)) then
  142. Message(parser_e_methode_id_expected);
  143. end;
  144. end
  145. else
  146. begin
  147. { check for constructor/destructor which is not allowed here }
  148. if (not parse_only) and
  149. (options in [potype_constructor,potype_destructor]) then
  150. Message(parser_e_constructors_always_objects);
  151. tokenpos:=procstartfilepos;
  152. aktprocsym:=pprocsym(symtablestack^.search(sp));
  153. if not(parse_only) then
  154. begin
  155. {The procedure we prepare for is in the implementation
  156. part of the unit we compile. It is also possible that we
  157. are compiling a program, which is also some kind of
  158. implementaion part.
  159. We need to find out if the procedure is global. If it is
  160. global, it is in the global symtable.}
  161. if not assigned(aktprocsym) and
  162. (symtablestack^.symtabletype=staticsymtable) then
  163. begin
  164. {Search the procedure in the global symtable.}
  165. aktprocsym:=Pprocsym(search_a_symtable(sp,globalsymtable));
  166. if assigned(aktprocsym) then
  167. begin
  168. {Check if it is a procedure.}
  169. if aktprocsym^.typ<>procsym then
  170. DuplicateSym(aktprocsym);
  171. {The procedure has been found. So it is
  172. a global one. Set the flags to mark this.}
  173. procinfo^.flags:=procinfo^.flags or pi_is_global;
  174. end;
  175. end;
  176. end;
  177. end;
  178. { Create the mangledname }
  179. {$ifndef UseNiceNames}
  180. if assigned(procinfo^._class) then
  181. begin
  182. if (pos('_$$_',procprefix)=0) then
  183. hs:=procprefix+'_$$_'+procinfo^._class^.objname^+'_$$_'+sp
  184. else
  185. hs:=procprefix+'_$'+sp;
  186. end
  187. else
  188. begin
  189. if lexlevel=normal_function_level then
  190. hs:=procprefix+'_'+sp
  191. else
  192. hs:=procprefix+'_$'+sp;
  193. end;
  194. {$else UseNiceNames}
  195. if assigned(procinfo^._class) then
  196. begin
  197. if (pos('_5Class_',procprefix)=0) then
  198. hs:=procprefix+'_5Class_'+procinfo^._class^.name^+'_'+tostr(length(sp))+sp
  199. else
  200. hs:=procprefix+'_'+tostr(length(sp))+sp;
  201. end
  202. else
  203. begin
  204. if lexlevel=normal_function_level then
  205. hs:=procprefix+'_'+tostr(length(sp))+sp
  206. else
  207. hs:=lowercase(procprefix)+'_'+tostr(length(sp))+sp;
  208. end;
  209. {$endif UseNiceNames}
  210. if assigned(aktprocsym) then
  211. begin
  212. { Check if overloading is enabled }
  213. if not(m_fpc in aktmodeswitches) then
  214. begin
  215. if aktprocsym^.typ<>procsym then
  216. begin
  217. DuplicateSym(aktprocsym);
  218. { try to recover by creating a new aktprocsym }
  219. tokenpos:=procstartfilepos;
  220. aktprocsym:=new(pprocsym,init(sp));
  221. end
  222. else
  223. begin
  224. if not(aktprocsym^.definition^.forwarddef) then
  225. Message(parser_e_procedure_overloading_is_off);
  226. end;
  227. end
  228. else
  229. begin
  230. { Check if the overloaded sym is realy a procsym }
  231. if aktprocsym^.typ<>procsym then
  232. begin
  233. Message1(parser_e_overloaded_no_procedure,aktprocsym^.name);
  234. { try to recover by creating a new aktprocsym }
  235. tokenpos:=procstartfilepos;
  236. aktprocsym:=new(pprocsym,init(sp));
  237. end;
  238. end;
  239. end
  240. else
  241. begin
  242. { create a new procsym and set the real filepos }
  243. tokenpos:=procstartfilepos;
  244. aktprocsym:=new(pprocsym,init(sp));
  245. { for operator we have only one definition for each overloaded
  246. operation }
  247. if (options=potype_operator) then
  248. begin
  249. { the only problem is that nextoverloaded might not be in a unit
  250. known for the unit itself }
  251. { not anymore PM }
  252. if assigned(overloaded_operators[optoken]) then
  253. aktprocsym^.definition:=overloaded_operators[optoken]^.definition;
  254. {$ifndef DONOTCHAINOPERATORS}
  255. overloaded_operators[optoken]:=aktprocsym;
  256. {$endif DONOTCHAINOPERATORS}
  257. end;
  258. symtablestack^.insert(aktprocsym);
  259. end;
  260. st:=symtablestack;
  261. pd:=new(pprocdef,init);
  262. pd^.symtablelevel:=symtablestack^.symtablelevel;
  263. if assigned(procinfo^._class) then
  264. pd^._class := procinfo^._class;
  265. { set the options from the caller (podestructor or poconstructor) }
  266. pd^.proctypeoption:=options;
  267. { calculate the offset of the parameters }
  268. paramoffset:=8;
  269. { calculate frame pointer offset }
  270. if lexlevel>normal_function_level then
  271. begin
  272. procinfo^.framepointer_offset:=paramoffset;
  273. inc(paramoffset,target_os.size_of_pointer);
  274. { this is needed to get correct framepointer push for local
  275. forward functions !! }
  276. pd^.parast^.symtablelevel:=lexlevel;
  277. end;
  278. if assigned (procinfo^._Class) and
  279. not(procinfo^._Class^.is_class) and
  280. (pd^.proctypeoption in [potype_constructor,potype_destructor]) then
  281. inc(paramoffset,target_os.size_of_pointer);
  282. { self pointer offset }
  283. { self isn't pushed in nested procedure of methods }
  284. if assigned(procinfo^._class) and (lexlevel=normal_function_level) then
  285. begin
  286. procinfo^.selfpointer_offset:=paramoffset;
  287. if assigned(aktprocsym^.definition) and
  288. not(po_containsself in aktprocsym^.definition^.procoptions) then
  289. inc(paramoffset,target_os.size_of_pointer);
  290. end;
  291. { con/-destructor flag ? }
  292. if assigned (procinfo^._Class) and
  293. procinfo^._class^.is_class and
  294. (pd^.proctypeoption in [potype_destructor,potype_constructor]) then
  295. inc(paramoffset,target_os.size_of_pointer);
  296. procinfo^.para_offset:=paramoffset;
  297. pd^.parast^.datasize:=0;
  298. pd^.nextoverloaded:=aktprocsym^.definition;
  299. aktprocsym^.definition:=pd;
  300. { this is probably obsolete now PM }
  301. aktprocsym^.definition^.fileinfo:=procstartfilepos;
  302. aktprocsym^.definition^.setmangledname(hs);
  303. aktprocsym^.definition^.procsym:=aktprocsym;
  304. if not parse_only then
  305. begin
  306. overloaded_level:=0;
  307. { we need another procprefix !!! }
  308. { count, but only those in the same unit !!}
  309. while assigned(pd) and
  310. (pd^.owner^.symtabletype in [globalsymtable,staticsymtable]) do
  311. begin
  312. { only count already implemented functions }
  313. if not(pd^.forwarddef) then
  314. inc(overloaded_level);
  315. pd:=pd^.nextoverloaded;
  316. end;
  317. if overloaded_level>0 then
  318. procprefix:=hs+'$'+tostr(overloaded_level)+'$'
  319. else
  320. procprefix:=hs+'$';
  321. end;
  322. { this must also be inserted in the right symtable !! PM }
  323. { otherwise we get subbtle problems with
  324. definitions of args defs in staticsymtable for
  325. implementation of a global method }
  326. if token=_LKLAMMER then
  327. parameter_dec(aktprocsym^.definition);
  328. { so we only restore the symtable now }
  329. symtablestack:=st;
  330. if (options=potype_operator) then
  331. overloaded_operators[optoken]:=aktprocsym;
  332. end;
  333. procedure parse_proc_dec;
  334. var
  335. hs : string;
  336. isclassmethod : boolean;
  337. begin
  338. inc(lexlevel);
  339. { read class method }
  340. if token=_CLASS then
  341. begin
  342. consume(_CLASS);
  343. isclassmethod:=true;
  344. end
  345. else
  346. isclassmethod:=false;
  347. case token of
  348. _FUNCTION : begin
  349. consume(_FUNCTION);
  350. parse_proc_head(potype_none);
  351. if token<>_COLON then
  352. begin
  353. if not(aktprocsym^.definition^.forwarddef) or
  354. (m_repeat_forward in aktmodeswitches) then
  355. begin
  356. consume(_COLON);
  357. consume_all_until(_SEMICOLON);
  358. end;
  359. end
  360. else
  361. begin
  362. consume(_COLON);
  363. inc(testcurobject);
  364. single_type(aktprocsym^.definition^.rettype,hs,false);
  365. aktprocsym^.definition^.test_if_fpu_result;
  366. dec(testcurobject);
  367. end;
  368. end;
  369. _PROCEDURE : begin
  370. consume(_PROCEDURE);
  371. parse_proc_head(potype_none);
  372. aktprocsym^.definition^.rettype.def:=voiddef;
  373. end;
  374. _CONSTRUCTOR : begin
  375. consume(_CONSTRUCTOR);
  376. parse_proc_head(potype_constructor);
  377. if assigned(procinfo^._class) and
  378. procinfo^._class^.is_class then
  379. begin
  380. { CLASS constructors return the created instance }
  381. aktprocsym^.definition^.rettype.def:=procinfo^._class;
  382. end
  383. else
  384. begin
  385. { OBJECT constructors return a boolean }
  386. {$IfDef GDB}
  387. { GDB doesn't like unnamed types !}
  388. aktprocsym^.definition^.rettype.def:=globaldef('boolean');
  389. {$else GDB}
  390. aktprocsym^.definition^.rettype.def:=new(porddef,init(bool8bit,0,1));
  391. {$Endif GDB}
  392. end;
  393. end;
  394. _DESTRUCTOR : begin
  395. consume(_DESTRUCTOR);
  396. parse_proc_head(potype_destructor);
  397. aktprocsym^.definition^.rettype.def:=voiddef;
  398. end;
  399. _OPERATOR : begin
  400. if lexlevel>normal_function_level then
  401. Message(parser_e_no_local_operator);
  402. consume(_OPERATOR);
  403. if not(token in [_PLUS..last_overloaded]) then
  404. Message(parser_e_overload_operator_failed);
  405. optoken:=token;
  406. consume(Token);
  407. procinfo^.flags:=procinfo^.flags or pi_operator;
  408. parse_proc_head(potype_operator);
  409. if token<>_ID then
  410. begin
  411. opsym:=nil;
  412. if not(m_result in aktmodeswitches) then
  413. consume(_ID);
  414. end
  415. else
  416. begin
  417. opsym:=new(pvarsym,initdef(pattern,voiddef));
  418. consume(_ID);
  419. end;
  420. if not try_to_consume(_COLON) then
  421. begin
  422. consume(_COLON);
  423. aktprocsym^.definition^.rettype.def:=generrordef;
  424. consume_all_until(_SEMICOLON);
  425. end
  426. else
  427. begin
  428. single_type(aktprocsym^.definition^.rettype,hs,false);
  429. aktprocsym^.definition^.test_if_fpu_result;
  430. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  431. ((aktprocsym^.definition^.rettype.def^.deftype<>
  432. orddef) or (porddef(aktprocsym^.definition^.
  433. rettype.def)^.typ<>bool8bit)) then
  434. Message(parser_e_comparative_operator_return_boolean);
  435. if assigned(opsym) then
  436. opsym^.vartype.def:=aktprocsym^.definition^.rettype.def;
  437. { We need to add the return type in the mangledname
  438. to allow overloading with just different results !! (PM) }
  439. aktprocsym^.definition^.setmangledname(
  440. aktprocsym^.definition^.mangledname+'$$'+hs);
  441. if (optoken=_ASSIGNMENT) and
  442. is_equal(aktprocsym^.definition^.rettype.def,
  443. pvarsym(aktprocsym^.definition^.parast^.symindex^.first)^.vartype.def) then
  444. message(parser_e_no_such_assignment);
  445. end;
  446. end;
  447. end;
  448. if isclassmethod and
  449. assigned(aktprocsym) then
  450. {$ifdef INCLUDEOK}
  451. include(aktprocsym^.definition^.procoptions,po_classmethod);
  452. {$else}
  453. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_classmethod];
  454. {$endif}
  455. consume(_SEMICOLON);
  456. dec(lexlevel);
  457. end;
  458. {****************************************************************************
  459. Procedure directive handlers
  460. ****************************************************************************}
  461. {$ifdef tp}
  462. {$F+}
  463. {$endif}
  464. procedure pd_far(const procnames:Tstringcontainer);
  465. begin
  466. Message(parser_w_proc_far_ignored);
  467. end;
  468. procedure pd_near(const procnames:Tstringcontainer);
  469. begin
  470. Message(parser_w_proc_near_ignored);
  471. end;
  472. procedure pd_export(const procnames:Tstringcontainer);
  473. begin
  474. if assigned(procinfo^._class) then
  475. Message(parser_e_methods_dont_be_export);
  476. if lexlevel<>normal_function_level then
  477. Message(parser_e_dont_nest_export);
  478. { only os/2 needs this }
  479. if target_info.target=target_i386_os2 then
  480. begin
  481. procnames.insert(realname);
  482. procinfo^.exported:=true;
  483. if cs_link_deffile in aktglobalswitches then
  484. deffile.AddExport(aktprocsym^.definition^.mangledname);
  485. end;
  486. end;
  487. procedure pd_inline(const procnames:Tstringcontainer);
  488. begin
  489. if not(cs_support_inline in aktmoduleswitches) then
  490. Message(parser_e_proc_inline_not_supported);
  491. end;
  492. procedure pd_forward(const procnames:Tstringcontainer);
  493. begin
  494. aktprocsym^.definition^.forwarddef:=true;
  495. end;
  496. procedure pd_stdcall(const procnames:Tstringcontainer);
  497. begin
  498. end;
  499. procedure pd_safecall(const procnames:Tstringcontainer);
  500. begin
  501. end;
  502. procedure pd_alias(const procnames:Tstringcontainer);
  503. begin
  504. consume(_COLON);
  505. procnames.insert(get_stringconst);
  506. end;
  507. procedure pd_asmname(const procnames:Tstringcontainer);
  508. begin
  509. aktprocsym^.definition^.setmangledname(target_os.Cprefix+pattern);
  510. if token=_CCHAR then
  511. consume(_CCHAR)
  512. else
  513. consume(_CSTRING);
  514. { we don't need anything else }
  515. aktprocsym^.definition^.forwarddef:=false;
  516. end;
  517. procedure pd_intern(const procnames:Tstringcontainer);
  518. begin
  519. consume(_COLON);
  520. aktprocsym^.definition^.extnumber:=get_intconst;
  521. end;
  522. procedure pd_system(const procnames:Tstringcontainer);
  523. begin
  524. aktprocsym^.definition^.setmangledname(realname);
  525. end;
  526. procedure pd_abstract(const procnames:Tstringcontainer);
  527. begin
  528. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  529. {$ifdef INCLUDEOK}
  530. include(aktprocsym^.definition^.procoptions,po_abstractmethod)
  531. {$else}
  532. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_abstractmethod]
  533. {$endif}
  534. else
  535. Message(parser_e_only_virtual_methods_abstract);
  536. { the method is defined }
  537. aktprocsym^.definition^.forwarddef:=false;
  538. end;
  539. procedure pd_virtual(const procnames:Tstringcontainer);
  540. {$ifdef WITHDMT}
  541. var
  542. pt : ptree;
  543. {$endif WITHDMT}
  544. begin
  545. if (aktprocsym^.definition^.proctypeoption=potype_constructor) and
  546. not(aktprocsym^.definition^._class^.is_class) then
  547. Message(parser_e_constructor_cannot_be_not_virtual);
  548. {$ifdef WITHDMT}
  549. if not(aktprocsym^.definition^._class^.is_class) and
  550. (token<>_SEMICOLON) then
  551. begin
  552. { any type of parameter is allowed here! }
  553. pt:=comp_expr(true);
  554. do_firstpass(pt);
  555. if is_constintnode(pt) then
  556. begin
  557. include(aktprocsym^.definition^.procoptions,po_msgint);
  558. aktprocsym^.definition^.messageinf.i:=pt^.value;
  559. end
  560. else
  561. Message(parser_e_ill_msg_expr);
  562. disposetree(pt);
  563. end;
  564. {$endif WITHDMT}
  565. end;
  566. procedure pd_static(const procnames:Tstringcontainer);
  567. begin
  568. if (cs_static_keyword in aktmoduleswitches) then
  569. begin
  570. {$ifdef INCLUDEOK}
  571. include(aktprocsym^.symoptions,sp_static);
  572. include(aktprocsym^.definition^.procoptions,po_staticmethod);
  573. {$else}
  574. aktprocsym^.symoptions:=aktprocsym^.symoptions+[sp_static];
  575. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_staticmethod];
  576. {$endif}
  577. end;
  578. end;
  579. procedure pd_override(const procnames:Tstringcontainer);
  580. begin
  581. if not(aktprocsym^.definition^._class^.is_class) then
  582. Message(parser_e_no_object_override);
  583. end;
  584. procedure pd_message(const procnames:Tstringcontainer);
  585. var
  586. pt : ptree;
  587. begin
  588. { check parameter type }
  589. if not(po_containsself in aktprocsym^.definition^.procoptions) and
  590. ((aktprocsym^.definition^.para^.count<>1) or
  591. (pparaitem(aktprocsym^.definition^.para^.first)^.paratyp<>vs_var)) then
  592. Message(parser_e_ill_msg_param);
  593. pt:=comp_expr(true);
  594. do_firstpass(pt);
  595. if pt^.treetype=stringconstn then
  596. begin
  597. {$ifdef INCLUDEOK}
  598. include(aktprocsym^.definition^.procoptions,po_msgstr);
  599. {$else}
  600. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgstr];
  601. {$endif}
  602. aktprocsym^.definition^.messageinf.str:=strnew(pt^.value_str);
  603. end
  604. else
  605. if is_constintnode(pt) then
  606. begin
  607. {$ifdef INCLUDEOK}
  608. include(aktprocsym^.definition^.procoptions,po_msgint);
  609. {$else}
  610. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgint];
  611. {$endif}
  612. aktprocsym^.definition^.messageinf.i:=pt^.value;
  613. end
  614. else
  615. Message(parser_e_ill_msg_expr);
  616. disposetree(pt);
  617. end;
  618. procedure resetvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  619. begin
  620. if psym(p)^.typ=varsym then
  621. with pvarsym(p)^ do
  622. if copy(name,1,3)='val' then
  623. aktprocsym^.definition^.parast^.symsearch^.rename(name,copy(name,4,length(name)));
  624. end;
  625. procedure pd_cdecl(const procnames:Tstringcontainer);
  626. begin
  627. if aktprocsym^.definition^.deftype<>procvardef then
  628. aktprocsym^.definition^.setmangledname(target_os.Cprefix+realname);
  629. { do not copy on local !! }
  630. if (aktprocsym^.definition^.deftype=procdef) and
  631. assigned(aktprocsym^.definition^.parast) then
  632. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}resetvaluepara);
  633. end;
  634. procedure pd_pascal(const procnames:Tstringcontainer);
  635. var st,parast : psymtable;
  636. lastps,ps : psym;
  637. begin
  638. new(st,init(parasymtable));
  639. parast:=aktprocsym^.definition^.parast;
  640. lastps:=nil;
  641. while assigned(parast^.symindex^.first) and (lastps<>psym(parast^.symindex^.first)) do
  642. begin
  643. ps:=psym(parast^.symindex^.first);
  644. while assigned(ps^.next) and (psym(ps^.next)<>lastps) do
  645. ps:=psym(ps^.next);
  646. ps^.owner:=st;
  647. { recalculate the corrected offset }
  648. { the really_insert_in_data procedure
  649. for parasymtable should only calculateoffset PM }
  650. ps^.insert_in_data;
  651. { reset the owner correctly }
  652. ps^.owner:=parast;
  653. lastps:=ps;
  654. end;
  655. end;
  656. procedure pd_register(const procnames:Tstringcontainer);
  657. begin
  658. Message(parser_w_proc_register_ignored);
  659. end;
  660. procedure pd_syscall(const procnames:Tstringcontainer);
  661. begin
  662. aktprocsym^.definition^.forwarddef:=false;
  663. aktprocsym^.definition^.extnumber:=get_intconst;
  664. end;
  665. procedure pd_external(const procnames:Tstringcontainer);
  666. {
  667. If import_dll=nil the procedure is assumed to be in another
  668. object file. In that object file it should have the name to
  669. which import_name is pointing to. Otherwise, the procedure is
  670. assumed to be in the DLL to which import_dll is pointing to. In
  671. that case either import_nr<>0 or import_name<>nil is true, so
  672. the procedure is either imported by number or by name. (DM)
  673. }
  674. var
  675. import_dll,
  676. import_name : string;
  677. import_nr : word;
  678. begin
  679. aktprocsym^.definition^.forwarddef:=false;
  680. { If the procedure should be imported from a DLL, a constant string follows.
  681. This isn't really correct, an contant string expression follows
  682. so we check if an semicolon follows, else a string constant have to
  683. follow (FK) }
  684. import_nr:=0;
  685. import_name:='';
  686. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  687. begin
  688. import_dll:=get_stringconst;
  689. if (idtoken=_NAME) then
  690. begin
  691. consume(_NAME);
  692. import_name:=get_stringconst;
  693. end;
  694. if (idtoken=_INDEX) then
  695. begin
  696. {After the word index follows the index number in the DLL.}
  697. consume(_INDEX);
  698. import_nr:=get_intconst;
  699. end;
  700. if (import_nr=0) and (import_name='') then
  701. {if (aktprocsym^.definition^.options and pocdecl)<>0 then
  702. import_name:=aktprocsym^.definition^.mangledname
  703. else
  704. Message(parser_w_empty_import_name);}
  705. { this should work both for win32 and Linux !! PM }
  706. import_name:=realname;
  707. if not(current_module^.uses_imports) then
  708. begin
  709. current_module^.uses_imports:=true;
  710. importlib^.preparelib(current_module^.modulename^);
  711. end;
  712. if not(m_repeat_forward in aktmodeswitches) then
  713. begin
  714. { we can only have one overloaded here ! }
  715. if assigned(aktprocsym^.definition^.nextoverloaded) then
  716. importlib^.importprocedure(aktprocsym^.definition^.nextoverloaded^.mangledname,
  717. import_dll,import_nr,import_name)
  718. else
  719. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  720. end
  721. else
  722. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  723. end
  724. else
  725. begin
  726. if (idtoken=_NAME) then
  727. begin
  728. consume(_NAME);
  729. import_name:=get_stringconst;
  730. aktprocsym^.definition^.setmangledname(import_name);
  731. end
  732. else
  733. begin
  734. { external shouldn't override the cdecl/system name }
  735. if not (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  736. aktprocsym^.definition^.setmangledname(aktprocsym^.name);
  737. end;
  738. end;
  739. end;
  740. {$ifdef TP}
  741. {$F-}
  742. {$endif}
  743. {const
  744. namelength=15;}
  745. type
  746. pd_handler=procedure(const procnames:Tstringcontainer);
  747. proc_dir_rec=record
  748. idtok : ttoken;
  749. pd_flags : longint;
  750. handler : pd_handler;
  751. pocall : tproccalloptions;
  752. pooption : tprocoptions;
  753. mutexclpocall : tproccalloptions;
  754. mutexclpotype : tproctypeoptions;
  755. mutexclpo : tprocoptions;
  756. end;
  757. const
  758. {Should contain the number of procedure directives we support.}
  759. num_proc_directives=29;
  760. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  761. (
  762. (
  763. idtok:_ABSTRACT;
  764. pd_flags : pd_interface+pd_object;
  765. handler : {$ifndef TP}@{$endif}pd_abstract;
  766. pocall : [];
  767. pooption : [po_abstractmethod];
  768. mutexclpocall : [pocall_internproc,pocall_inline];
  769. mutexclpotype : [potype_constructor,potype_destructor];
  770. mutexclpo : [po_exports,po_interrupt,po_external]
  771. ),(
  772. idtok:_ALIAS;
  773. pd_flags : pd_implemen+pd_body;
  774. handler : {$ifndef TP}@{$endif}pd_alias;
  775. pocall : [];
  776. pooption : [];
  777. mutexclpocall : [pocall_inline];
  778. mutexclpotype : [];
  779. mutexclpo : [po_external]
  780. ),(
  781. idtok:_ASMNAME;
  782. pd_flags : pd_interface+pd_implemen;
  783. handler : {$ifndef TP}@{$endif}pd_asmname;
  784. pocall : [pocall_cdecl,pocall_clearstack];
  785. pooption : [po_external];
  786. mutexclpocall : [pocall_internproc];
  787. mutexclpotype : [];
  788. mutexclpo : [po_external]
  789. ),(
  790. idtok:_ASSEMBLER;
  791. pd_flags : pd_implemen+pd_body;
  792. handler : nil;
  793. pocall : [];
  794. pooption : [po_assembler];
  795. mutexclpocall : [];
  796. mutexclpotype : [];
  797. mutexclpo : [po_external]
  798. ),(
  799. idtok:_CDECL;
  800. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  801. handler : {$ifndef TP}@{$endif}pd_cdecl;
  802. pocall : [pocall_cdecl,pocall_clearstack];
  803. pooption : [po_savestdregs];
  804. mutexclpocall : [pocall_internproc,pocall_leftright,pocall_inline];
  805. mutexclpotype : [];
  806. mutexclpo : [po_assembler,po_external]
  807. ),(
  808. idtok:_DYNAMIC;
  809. pd_flags : pd_interface+pd_object;
  810. handler : {$ifndef TP}@{$endif}pd_virtual;
  811. pocall : [];
  812. pooption : [po_virtualmethod];
  813. mutexclpocall : [pocall_internproc,pocall_inline];
  814. mutexclpotype : [];
  815. mutexclpo : [po_exports,po_interrupt,po_external]
  816. ),(
  817. idtok:_EXPORT;
  818. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??};
  819. handler : {$ifndef TP}@{$endif}pd_export;
  820. pocall : [];
  821. pooption : [po_exports];
  822. mutexclpocall : [pocall_internproc,pocall_inline];
  823. mutexclpotype : [];
  824. mutexclpo : [po_external,po_interrupt]
  825. ),(
  826. idtok:_EXTERNAL;
  827. pd_flags : pd_implemen+pd_interface;
  828. handler : {$ifndef TP}@{$endif}pd_external;
  829. pocall : [];
  830. pooption : [po_external];
  831. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  832. mutexclpotype : [];
  833. mutexclpo : [po_exports,po_interrupt,po_assembler]
  834. ),(
  835. idtok:_FAR;
  836. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar;
  837. handler : {$ifndef TP}@{$endif}pd_far;
  838. pocall : [];
  839. pooption : [];
  840. mutexclpocall : [pocall_internproc,pocall_inline];
  841. mutexclpotype : [];
  842. mutexclpo : []
  843. ),(
  844. idtok:_FORWARD;
  845. pd_flags : pd_implemen;
  846. handler : {$ifndef TP}@{$endif}pd_forward;
  847. pocall : [];
  848. pooption : [];
  849. mutexclpocall : [pocall_internproc,pocall_inline];
  850. mutexclpotype : [];
  851. mutexclpo : [po_external]
  852. ),(
  853. idtok:_INLINE;
  854. pd_flags : pd_implemen+pd_body;
  855. handler : {$ifndef TP}@{$endif}pd_inline;
  856. pocall : [pocall_inline];
  857. pooption : [];
  858. mutexclpocall : [pocall_internproc];
  859. mutexclpotype : [potype_constructor,potype_destructor];
  860. mutexclpo : [po_exports,po_external,po_interrupt]
  861. ),(
  862. idtok:_INTERNCONST;
  863. pd_flags : pd_implemen+pd_body;
  864. handler : {$ifndef TP}@{$endif}pd_intern;
  865. pocall : [pocall_internconst];
  866. pooption : [];
  867. mutexclpocall : [];
  868. mutexclpotype : [potype_operator];
  869. mutexclpo : []
  870. ),(
  871. idtok:_INTERNPROC;
  872. pd_flags : pd_implemen;
  873. handler : {$ifndef TP}@{$endif}pd_intern;
  874. pocall : [pocall_internproc];
  875. pooption : [];
  876. mutexclpocall : [pocall_inline,pocall_clearstack,pocall_leftright,pocall_cdecl];
  877. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  878. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck]
  879. ),(
  880. idtok:_INTERRUPT;
  881. pd_flags : pd_implemen+pd_body;
  882. handler : nil;
  883. pocall : [];
  884. pooption : [po_interrupt];
  885. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_clearstack,pocall_leftright,pocall_inline];
  886. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  887. mutexclpo : [po_external]
  888. ),(
  889. idtok:_IOCHECK;
  890. pd_flags : pd_implemen+pd_body;
  891. handler : nil;
  892. pocall : [];
  893. pooption : [po_iocheck];
  894. mutexclpocall : [pocall_internproc];
  895. mutexclpotype : [];
  896. mutexclpo : [po_external]
  897. ),(
  898. idtok:_MESSAGE;
  899. pd_flags : pd_interface+pd_object;
  900. handler : {$ifndef TP}@{$endif}pd_message;
  901. pocall : [];
  902. pooption : []; { can be po_msgstr or po_msgint }
  903. mutexclpocall : [pocall_inline,pocall_internproc];
  904. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  905. mutexclpo : [po_interrupt,po_external]
  906. ),(
  907. idtok:_NEAR;
  908. pd_flags : pd_implemen+pd_body+pd_procvar;
  909. handler : {$ifndef TP}@{$endif}pd_near;
  910. pocall : [];
  911. pooption : [];
  912. mutexclpocall : [pocall_internproc];
  913. mutexclpotype : [];
  914. mutexclpo : []
  915. ),(
  916. idtok:_OVERRIDE;
  917. pd_flags : pd_interface+pd_object;
  918. handler : {$ifndef TP}@{$endif}pd_override;
  919. pocall : [];
  920. pooption : [po_overridingmethod,po_virtualmethod];
  921. mutexclpocall : [pocall_inline,pocall_internproc];
  922. mutexclpotype : [];
  923. mutexclpo : [po_exports,po_external,po_interrupt]
  924. ),(
  925. idtok:_PASCAL;
  926. pd_flags : pd_implemen+pd_body+pd_procvar;
  927. handler : {$ifndef TP}@{$endif}pd_pascal;
  928. pocall : [pocall_leftright];
  929. pooption : [];
  930. mutexclpocall : [pocall_internproc];
  931. mutexclpotype : [];
  932. mutexclpo : [po_external]
  933. ),(
  934. idtok:_POPSTACK;
  935. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  936. handler : nil;
  937. pocall : [pocall_clearstack];
  938. pooption : [];
  939. mutexclpocall : [pocall_inline,pocall_internproc];
  940. mutexclpotype : [];
  941. mutexclpo : [po_assembler,po_external]
  942. ),(
  943. idtok:_PUBLIC;
  944. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject;
  945. handler : nil;
  946. pocall : [];
  947. pooption : [];
  948. mutexclpocall : [pocall_internproc,pocall_inline];
  949. mutexclpotype : [];
  950. mutexclpo : [po_external]
  951. ),(
  952. idtok:_REGISTER;
  953. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  954. handler : {$ifndef TP}@{$endif}pd_register;
  955. pocall : [pocall_register];
  956. pooption : [];
  957. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc];
  958. mutexclpotype : [];
  959. mutexclpo : [po_external]
  960. ),(
  961. idtok:_SAFECALL;
  962. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  963. handler : {$ifndef TP}@{$endif}pd_safecall;
  964. pocall : [pocall_safecall];
  965. pooption : [po_savestdregs];
  966. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc,pocall_inline];
  967. mutexclpotype : [];
  968. mutexclpo : [po_external]
  969. ),(
  970. idtok:_SAVEREGISTERS;
  971. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  972. handler : nil;
  973. pocall : [];
  974. pooption : [po_saveregisters];
  975. mutexclpocall : [pocall_internproc];
  976. mutexclpotype : [];
  977. mutexclpo : [po_external]
  978. ),(
  979. idtok:_STATIC;
  980. pd_flags : pd_interface+pd_object;
  981. handler : {$ifndef TP}@{$endif}pd_static;
  982. pocall : [];
  983. pooption : [po_staticmethod];
  984. mutexclpocall : [pocall_inline,pocall_internproc];
  985. mutexclpotype : [potype_constructor,potype_destructor];
  986. mutexclpo : [po_external,po_interrupt,po_exports]
  987. ),(
  988. idtok:_STDCALL;
  989. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  990. handler : {$ifndef TP}@{$endif}pd_stdcall;
  991. pocall : [pocall_stdcall];
  992. pooption : [po_savestdregs];
  993. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_inline,pocall_internproc];
  994. mutexclpotype : [];
  995. mutexclpo : [po_external]
  996. ),(
  997. idtok:_SYSCALL;
  998. pd_flags : pd_interface;
  999. handler : {$ifndef TP}@{$endif}pd_syscall;
  1000. pocall : [pocall_palmossyscall];
  1001. pooption : [];
  1002. mutexclpocall : [pocall_cdecl,pocall_inline,pocall_internproc];
  1003. mutexclpotype : [];
  1004. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1005. ),(
  1006. idtok:_SYSTEM;
  1007. pd_flags : pd_implemen;
  1008. handler : {$ifndef TP}@{$endif}pd_system;
  1009. pocall : [pocall_clearstack];
  1010. pooption : [];
  1011. mutexclpocall : [pocall_leftright,pocall_inline,pocall_internproc];
  1012. mutexclpotype : [];
  1013. mutexclpo : [po_external,po_assembler,po_interrupt]
  1014. ),(
  1015. idtok:_VIRTUAL;
  1016. pd_flags : pd_interface+pd_object;
  1017. handler : {$ifndef TP}@{$endif}pd_virtual;
  1018. pocall : [];
  1019. pooption : [po_virtualmethod];
  1020. mutexclpocall : [pocall_inline,pocall_internproc];
  1021. mutexclpotype : [];
  1022. mutexclpo : [po_external,po_interrupt,po_exports]
  1023. )
  1024. );
  1025. function is_proc_directive(tok:ttoken):boolean;
  1026. var
  1027. i : longint;
  1028. begin
  1029. is_proc_directive:=false;
  1030. for i:=1 to num_proc_directives do
  1031. if proc_direcdata[i].idtok=idtoken then
  1032. begin
  1033. is_proc_directive:=true;
  1034. exit;
  1035. end;
  1036. end;
  1037. function parse_proc_direc(const proc_names:Tstringcontainer;var pdflags:word):boolean;
  1038. {
  1039. Parse the procedure directive, returns true if a correct directive is found
  1040. }
  1041. var
  1042. p : longint;
  1043. found : boolean;
  1044. name : string;
  1045. begin
  1046. parse_proc_direc:=false;
  1047. name:=pattern;
  1048. found:=false;
  1049. for p:=1 to num_proc_directives do
  1050. if proc_direcdata[p].idtok=idtoken then
  1051. begin
  1052. found:=true;
  1053. break;
  1054. end;
  1055. { Check if the procedure directive is known }
  1056. if not found then
  1057. begin
  1058. { parsing a procvar type the name can be any
  1059. next variable !! }
  1060. if (pdflags and (pd_procvar or pd_object))=0 then
  1061. Message1(parser_w_unknown_proc_directive_ignored,name);
  1062. exit;
  1063. end;
  1064. { static needs a special treatment }
  1065. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1066. exit;
  1067. { Conflicts between directives ? }
  1068. if (aktprocsym^.definition^.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1069. ((aktprocsym^.definition^.proccalloptions*proc_direcdata[p].mutexclpocall)<>[]) or
  1070. ((aktprocsym^.definition^.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1071. begin
  1072. Message1(parser_e_proc_dir_conflict,name);
  1073. exit;
  1074. end;
  1075. { Check if the directive is only for objects }
  1076. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1077. not assigned(aktprocsym^.definition^._class) then
  1078. begin
  1079. exit;
  1080. end;
  1081. { check if method and directive not for object public }
  1082. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1083. assigned(aktprocsym^.definition^._class) then
  1084. begin
  1085. exit;
  1086. end;
  1087. { consume directive, and turn flag on }
  1088. consume(token);
  1089. parse_proc_direc:=true;
  1090. { Check the pd_flags if the directive should be allowed }
  1091. if ((pdflags and pd_interface)<>0) and
  1092. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1093. begin
  1094. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1095. exit;
  1096. end;
  1097. if ((pdflags and pd_implemen)<>0) and
  1098. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1099. begin
  1100. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1101. exit;
  1102. end;
  1103. if ((pdflags and pd_procvar)<>0) and
  1104. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1105. begin
  1106. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1107. exit;
  1108. end;
  1109. { Return the new pd_flags }
  1110. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1111. pdflags:=pdflags and (not pd_body);
  1112. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1113. pdflags:=pdflags or pd_global;
  1114. { Add the correct flag }
  1115. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions+proc_direcdata[p].pocall;
  1116. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+proc_direcdata[p].pooption;
  1117. { Adjust positions of args for cdecl or stdcall }
  1118. if (aktprocsym^.definition^.deftype=procdef) and
  1119. (([pocall_cdecl,pocall_stdcall]*aktprocsym^.definition^.proccalloptions)<>[]) then
  1120. aktprocsym^.definition^.parast^.set_alignment(target_os.size_of_longint);
  1121. { Call the handler }
  1122. if pointer({$ifndef FPC}@{$endif}proc_direcdata[p].handler)<>nil then
  1123. proc_direcdata[p].handler(proc_names);
  1124. end;
  1125. {***************************************************************************}
  1126. function check_identical(var p : pprocdef) : boolean;
  1127. {
  1128. Search for idendical definitions,
  1129. if there is a forward, then kill this.
  1130. Returns the result of the forward check.
  1131. Removed from unter_dec to keep the source readable
  1132. }
  1133. var
  1134. hd,pd : Pprocdef;
  1135. storeparast : psymtable;
  1136. ad,fd : psym;
  1137. s : string;
  1138. begin
  1139. check_identical:=false;
  1140. p:=nil;
  1141. pd:=aktprocsym^.definition;
  1142. if assigned(pd) then
  1143. begin
  1144. { Is there an overload/forward ? }
  1145. if assigned(pd^.nextoverloaded) then
  1146. begin
  1147. { walk the procdef list }
  1148. while (assigned(pd)) and (assigned(pd^.nextoverloaded)) do
  1149. begin
  1150. if (not(m_repeat_forward in aktmodeswitches) and
  1151. (aktprocsym^.definition^.para^.count=0)) or
  1152. (equal_paras(aktprocsym^.definition^.para,pd^.nextoverloaded^.para,false) and
  1153. { for operators equal_paras is not enough !! }
  1154. ((aktprocsym^.definition^.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1155. is_equal(pd^.nextoverloaded^.rettype.def,aktprocsym^.definition^.rettype.def))) then
  1156. begin
  1157. if pd^.nextoverloaded^.forwarddef then
  1158. { remove the forward definition but don't delete it, }
  1159. { the symtable is the owner !! }
  1160. begin
  1161. hd:=pd^.nextoverloaded;
  1162. { Check if the procedure type and return type are correct }
  1163. if (hd^.proctypeoption<>aktprocsym^.definition^.proctypeoption) or
  1164. (not(is_equal(hd^.rettype.def,aktprocsym^.definition^.rettype.def)) and
  1165. (m_repeat_forward in aktmodeswitches)) then
  1166. begin
  1167. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1168. exit;
  1169. end;
  1170. { Check calling convention, no check for internconst,internproc which
  1171. are only defined in interface or implementation }
  1172. if (hd^.proccalloptions-[pocall_internconst,pocall_internproc]<>
  1173. aktprocsym^.definition^.proccalloptions-[pocall_internconst,pocall_internproc]) then
  1174. begin
  1175. { only trigger an error, becuase it doesn't hurt }
  1176. Message(parser_e_call_convention_dont_match_forward);
  1177. { set the mangledname to the interface name so it doesn't trigger
  1178. the Note about different manglednames (PFV) }
  1179. aktprocsym^.definition^.setmangledname(hd^.mangledname);
  1180. end;
  1181. { manglednames are equal? }
  1182. hd^.count:=false;
  1183. if (m_repeat_forward in aktmodeswitches) or
  1184. aktprocsym^.definition^.haspara then
  1185. begin
  1186. if (hd^.mangledname<>aktprocsym^.definition^.mangledname) then
  1187. begin
  1188. { When overloading is not possible then we issue an error }
  1189. { This is not true, tp7/delphi don't give an error when a renamed
  1190. type is used in the other declaration (PFV)
  1191. if not(m_repeat_forward in aktmodeswitches) then
  1192. begin
  1193. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1194. exit;
  1195. end; }
  1196. if not(po_external in aktprocsym^.definition^.procoptions) then
  1197. Message2(parser_n_interface_name_diff_implementation_name,hd^.mangledname,
  1198. aktprocsym^.definition^.mangledname);
  1199. { reset the mangledname of the interface part to be sure }
  1200. { this is wrong because the mangled name might have been used already !! }
  1201. if hd^.is_used then
  1202. renameasmsymbol(hd^.mangledname,aktprocsym^.definition^.mangledname);
  1203. hd^.setmangledname(aktprocsym^.definition^.mangledname);
  1204. { so we need to keep the name of interface !!
  1205. No!!!! The procedure directives can change the mangledname.
  1206. I fixed this by first calling check_identical and then doing
  1207. the proc directives, but this is not a good solution.(DM)}
  1208. { this is also wrong (PM)
  1209. aktprocsym^.definition^.setmangledname(hd^.mangledname);}
  1210. end
  1211. else
  1212. begin
  1213. { If mangled names are equal, therefore }
  1214. { they have the same number of parameters }
  1215. { Therefore we can check the name of these }
  1216. { parameters... }
  1217. if hd^.forwarddef and aktprocsym^.definition^.forwarddef then
  1218. begin
  1219. Message1(parser_e_function_already_declared_public_forward,aktprocsym^.demangledName);
  1220. Check_identical:=true;
  1221. { Remove other forward from the list to reduce errors }
  1222. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1223. exit;
  1224. end;
  1225. ad:=psym(hd^.parast^.symindex^.first);
  1226. fd:=psym(aktprocsym^.definition^.parast^.symindex^.first);
  1227. if assigned(ad) and assigned(fd) then
  1228. begin
  1229. while assigned(ad) and assigned(fd) do
  1230. begin
  1231. s:=ad^.name;
  1232. if s<>fd^.name then
  1233. begin
  1234. Message3(parser_e_header_different_var_names,
  1235. aktprocsym^.name,s,fd^.name);
  1236. break;
  1237. end;
  1238. { it is impossible to have a nil pointer }
  1239. { for only one parameter - since they }
  1240. { have the same number of parameters. }
  1241. { Left = next parameter. }
  1242. ad:=psym(ad^.left);
  1243. fd:=psym(fd^.left);
  1244. end;
  1245. end;
  1246. end;
  1247. end;
  1248. { also the para_offset }
  1249. hd^.parast^.address_fixup:=aktprocsym^.definition^.parast^.address_fixup;
  1250. hd^.count:=true;
  1251. { remove pd^.nextoverloaded from the list }
  1252. { and add aktprocsym^.definition }
  1253. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1254. hd^.nextoverloaded:=aktprocsym^.definition^.nextoverloaded;
  1255. { Alert! All fields of aktprocsym^.definition that are modified
  1256. by the procdir handlers must be copied here!.}
  1257. hd^.forwarddef:=false;
  1258. hd^.proccalloptions:=hd^.proccalloptions + aktprocsym^.definition^.proccalloptions;
  1259. hd^.procoptions:=hd^.procoptions + aktprocsym^.definition^.procoptions;
  1260. if aktprocsym^.definition^.extnumber=-1 then
  1261. aktprocsym^.definition^.extnumber:=hd^.extnumber
  1262. else
  1263. if hd^.extnumber=-1 then
  1264. hd^.extnumber:=aktprocsym^.definition^.extnumber;
  1265. { switch parast for warning in implementation PM }
  1266. if (m_repeat_forward in aktmodeswitches) or
  1267. aktprocsym^.definition^.haspara then
  1268. begin
  1269. storeparast:=hd^.parast;
  1270. hd^.parast:=aktprocsym^.definition^.parast;
  1271. aktprocsym^.definition^.parast:=storeparast;
  1272. end;
  1273. if pd=aktprocsym^.definition then
  1274. p:=nil
  1275. else
  1276. p:=pd;
  1277. aktprocsym^.definition:=hd;
  1278. check_identical:=true;
  1279. end
  1280. else
  1281. { abstract methods aren't forward defined, but this }
  1282. { needs another error message }
  1283. if not(po_abstractmethod in pd^.nextoverloaded^.procoptions) then
  1284. Message(parser_e_overloaded_have_same_parameters)
  1285. else
  1286. Message(parser_e_abstract_no_definition);
  1287. break;
  1288. end;
  1289. pd:=pd^.nextoverloaded;
  1290. end;
  1291. end
  1292. else
  1293. begin
  1294. { there is no overloaded, so its always identical with itself }
  1295. check_identical:=true;
  1296. end;
  1297. end;
  1298. { insert opsym only in the right symtable }
  1299. if ((procinfo^.flags and pi_operator)<>0) and assigned(opsym)
  1300. and not parse_only then
  1301. begin
  1302. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  1303. begin
  1304. pprocdef(aktprocsym^.definition)^.parast^.insert(opsym);
  1305. { this increases the data size }
  1306. { correct this to get the right ret $value }
  1307. dec(pprocdef(aktprocsym^.definition)^.parast^.datasize,opsym^.getpushsize);
  1308. { this allows to read the funcretoffset }
  1309. opsym^.address:=-4;
  1310. opsym^.varspez:=vs_var;
  1311. end
  1312. else
  1313. pprocdef(aktprocsym^.definition)^.localst^.insert(opsym);
  1314. end;
  1315. end;
  1316. procedure compile_proc_body(const proc_names:Tstringcontainer;
  1317. make_global,parent_has_class:boolean);
  1318. {
  1319. Compile the body of a procedure
  1320. }
  1321. var
  1322. oldexitlabel,oldexit2label : pasmlabel;
  1323. oldfaillabel,oldquickexitlabel:Pasmlabel;
  1324. _class,hp:Pobjectdef;
  1325. { switches can change inside the procedure }
  1326. entryswitches, exitswitches : tlocalswitches;
  1327. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  1328. { code for the subroutine as tree }
  1329. {$ifdef newcg}
  1330. code:ptree;
  1331. {$else newcg}
  1332. code:ptree;
  1333. {$endif newcg}
  1334. { size of the local strackframe }
  1335. stackframe:longint;
  1336. { true when no stackframe is required }
  1337. nostackframe:boolean;
  1338. { number of bytes which have to be cleared by RET }
  1339. parasize:longint;
  1340. { filepositions }
  1341. entrypos,
  1342. savepos,
  1343. exitpos : tfileposinfo;
  1344. begin
  1345. { calculate the lexical level }
  1346. inc(lexlevel);
  1347. if lexlevel>32 then
  1348. Message(parser_e_too_much_lexlevel);
  1349. { static is also important for local procedures !! }
  1350. if (po_staticmethod in aktprocsym^.definition^.procoptions) then
  1351. allow_only_static:=true
  1352. else if (lexlevel=normal_function_level) then
  1353. allow_only_static:=false;
  1354. { save old labels }
  1355. oldexitlabel:=aktexitlabel;
  1356. oldexit2label:=aktexit2label;
  1357. oldquickexitlabel:=quickexitlabel;
  1358. oldfaillabel:=faillabel;
  1359. { get new labels }
  1360. getlabel(aktexitlabel);
  1361. getlabel(aktexit2label);
  1362. { exit for fail in constructors }
  1363. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1364. begin
  1365. getlabel(faillabel);
  1366. getlabel(quickexitlabel);
  1367. end;
  1368. { reset break and continue labels }
  1369. block_type:=bt_general;
  1370. aktbreaklabel:=nil;
  1371. aktcontinuelabel:=nil;
  1372. { insert symtables for the class, by only if it is no nested function }
  1373. if assigned(procinfo^._class) and not(parent_has_class) then
  1374. begin
  1375. { insert them in the reverse order ! }
  1376. hp:=nil;
  1377. repeat
  1378. _class:=procinfo^._class;
  1379. while _class^.childof<>hp do
  1380. _class:=_class^.childof;
  1381. hp:=_class;
  1382. _class^.symtable^.next:=symtablestack;
  1383. symtablestack:=_class^.symtable;
  1384. until hp=procinfo^._class;
  1385. end;
  1386. { insert parasymtable in symtablestack}
  1387. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  1388. for checking of same names used in interface and implementation !! }
  1389. if lexlevel>=normal_function_level then
  1390. begin
  1391. aktprocsym^.definition^.parast^.next:=symtablestack;
  1392. symtablestack:=aktprocsym^.definition^.parast;
  1393. symtablestack^.symtablelevel:=lexlevel;
  1394. end;
  1395. { insert localsymtable in symtablestack}
  1396. aktprocsym^.definition^.localst^.next:=symtablestack;
  1397. symtablestack:=aktprocsym^.definition^.localst;
  1398. symtablestack^.symtablelevel:=lexlevel;
  1399. { constant symbols are inserted in this symboltable }
  1400. constsymtable:=symtablestack;
  1401. { reset the temporary memory }
  1402. cleartempgen;
  1403. {$ifdef newcg}
  1404. tg.usedinproc:=[];
  1405. {$else newcg}
  1406. { no registers are used }
  1407. usedinproc:=0;
  1408. {$endif newcg}
  1409. { save entry info }
  1410. entrypos:=aktfilepos;
  1411. entryswitches:=aktlocalswitches;
  1412. localmaxfpuregisters:=aktmaxfpuregisters;
  1413. {$ifdef newcg}
  1414. {$ifdef dummy}
  1415. { parse the code ... }
  1416. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1417. code:=convtree2node(assembler_block)
  1418. else
  1419. code:=convtree2node(block(current_module^.islibrary));
  1420. {$endif dummy}
  1421. { parse the code ... }
  1422. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1423. code:=assembler_block
  1424. else
  1425. code:=block(current_module^.islibrary);
  1426. {$else newcg}
  1427. { parse the code ... }
  1428. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1429. code:=assembler_block
  1430. else
  1431. code:=block(current_module^.islibrary);
  1432. {$endif newcg}
  1433. { get a better entry point }
  1434. if assigned(code) then
  1435. entrypos:=code^.fileinfo;
  1436. { save exit info }
  1437. exitswitches:=aktlocalswitches;
  1438. exitpos:=last_endtoken_filepos;
  1439. { save current filepos }
  1440. savepos:=aktfilepos;
  1441. {When we are called to compile the body of a unit, aktprocsym should
  1442. point to the unit initialization. If the unit has no initialization,
  1443. aktprocsym=nil. But in that case code=nil. hus we should check for
  1444. code=nil, when we use aktprocsym.}
  1445. { set the framepointer to esp for assembler functions }
  1446. { but only if the are no local variables }
  1447. { already done in assembler_block }
  1448. {$ifdef newcg}
  1449. tg.setfirsttemp(procinfo^.firsttemp_offset);
  1450. {$else newcg}
  1451. setfirsttemp(procinfo^.firsttemp_offset);
  1452. {$endif newcg}
  1453. { ... and generate assembler }
  1454. { but set the right switches for entry !! }
  1455. aktlocalswitches:=entryswitches;
  1456. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  1457. aktmaxfpuregisters:=localmaxfpuregisters;
  1458. {$ifndef NOPASS2}
  1459. {$ifdef newcg}
  1460. if assigned(code) then
  1461. generatecode(code);
  1462. {$else newcg}
  1463. if assigned(code) then
  1464. generatecode(code);
  1465. {$endif newcg}
  1466. { set switches to status at end of procedure }
  1467. aktlocalswitches:=exitswitches;
  1468. if assigned(code) then
  1469. begin
  1470. aktprocsym^.definition^.code:=code;
  1471. { the procedure is now defined }
  1472. aktprocsym^.definition^.forwarddef:=false;
  1473. {$ifdef newcg}
  1474. aktprocsym^.definition^.usedregisters:=tg.usedinproc;
  1475. {$else newcg}
  1476. aktprocsym^.definition^.usedregisters:=usedinproc;
  1477. {$endif newcg}
  1478. end;
  1479. {$ifdef newcg}
  1480. stackframe:=tg.gettempsize;
  1481. {$else newcg}
  1482. stackframe:=gettempsize;
  1483. {$endif newcg}
  1484. { first generate entry code with the correct position and switches }
  1485. aktfilepos:=entrypos;
  1486. aktlocalswitches:=entryswitches;
  1487. {$ifdef newcg}
  1488. if assigned(code) then
  1489. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1490. {$else newcg}
  1491. if assigned(code) then
  1492. genentrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1493. {$endif newcg}
  1494. { now generate exit code with the correct position and switches }
  1495. aktfilepos:=exitpos;
  1496. aktlocalswitches:=exitswitches;
  1497. if assigned(code) then
  1498. begin
  1499. {$ifdef newcg}
  1500. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1501. {$else newcg}
  1502. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1503. {$endif newcg}
  1504. procinfo^.aktproccode^.insertlist(procinfo^.aktentrycode);
  1505. procinfo^.aktproccode^.concatlist(procinfo^.aktexitcode);
  1506. {$ifdef i386}
  1507. {$ifndef NoOpt}
  1508. if (cs_optimize in aktglobalswitches) and
  1509. { do not optimize pure assembler procedures }
  1510. ((procinfo^.flags and pi_is_assembler)=0) then
  1511. Optimize(procinfo^.aktproccode);
  1512. {$endif NoOpt}
  1513. {$endif}
  1514. { save local data (casetable) also in the same file }
  1515. if assigned(procinfo^.aktlocaldata) and
  1516. (not procinfo^.aktlocaldata^.empty) then
  1517. begin
  1518. procinfo^.aktproccode^.concat(new(pai_section,init(sec_data)));
  1519. procinfo^.aktproccode^.concatlist(procinfo^.aktlocaldata);
  1520. procinfo^.aktproccode^.concat(new(pai_section,init(sec_code)));
  1521. end;
  1522. { now we can insert a cut }
  1523. if (cs_create_smart in aktmoduleswitches) then
  1524. codesegment^.concat(new(pai_cut,init));
  1525. { add the procedure to the codesegment }
  1526. codesegment^.concatlist(procinfo^.aktproccode);
  1527. end;
  1528. {$else}
  1529. if assigned(code) then
  1530. firstpass(code);
  1531. {$endif NOPASS2}
  1532. { ... remove symbol tables, for the browser leave the static table }
  1533. { if (cs_browser in aktmoduleswitches) and (symtablestack^.symtabletype=staticsymtable) then
  1534. symtablestack^.next:=symtablestack^.next^.next
  1535. else }
  1536. if lexlevel>=normal_function_level then
  1537. symtablestack:=symtablestack^.next^.next
  1538. else
  1539. symtablestack:=symtablestack^.next;
  1540. { ... check for unused symbols }
  1541. { but only if there is no asm block }
  1542. if assigned(code) then
  1543. begin
  1544. if (Errorcount=0) then
  1545. begin
  1546. aktprocsym^.definition^.localst^.check_forwards;
  1547. aktprocsym^.definition^.localst^.checklabels;
  1548. end;
  1549. if (procinfo^.flags and pi_uses_asm)=0 then
  1550. begin
  1551. { not for unit init, becuase the var can be used in finalize,
  1552. it will be done in proc_unit }
  1553. if not(aktprocsym^.definition^.proctypeoption
  1554. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  1555. aktprocsym^.definition^.localst^.allsymbolsused;
  1556. aktprocsym^.definition^.parast^.allsymbolsused;
  1557. end;
  1558. end;
  1559. { the local symtables can be deleted, but the parast }
  1560. { doesn't, (checking definitons when calling a }
  1561. { function }
  1562. { not for a inline procedure !! (PM) }
  1563. { at lexlevel = 1 localst is the staticsymtable itself }
  1564. { so no dispose here !! }
  1565. if assigned(code) and
  1566. not(cs_browser in aktmoduleswitches) and
  1567. not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1568. begin
  1569. if lexlevel>=normal_function_level then
  1570. dispose(aktprocsym^.definition^.localst,done);
  1571. aktprocsym^.definition^.localst:=nil;
  1572. end;
  1573. {$ifdef newcg}
  1574. { all registers can be used again }
  1575. tg.resetusableregisters;
  1576. { only now we can remove the temps }
  1577. tg.resettempgen;
  1578. {$else newcg}
  1579. { all registers can be used again }
  1580. resetusableregisters;
  1581. { only now we can remove the temps }
  1582. resettempgen;
  1583. {$endif newcg}
  1584. { remove code tree, if not inline procedure }
  1585. if assigned(code) and not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1586. {$ifdef newcg}
  1587. {!!!!!!! dispose(code,done); }
  1588. disposetree(code);
  1589. {$else newcg}
  1590. disposetree(code);
  1591. {$endif newcg}
  1592. { remove class member symbol tables }
  1593. while symtablestack^.symtabletype=objectsymtable do
  1594. symtablestack:=symtablestack^.next;
  1595. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  1596. { restore filepos, the switches are already set }
  1597. aktfilepos:=savepos;
  1598. { restore labels }
  1599. aktexitlabel:=oldexitlabel;
  1600. aktexit2label:=oldexit2label;
  1601. quickexitlabel:=oldquickexitlabel;
  1602. faillabel:=oldfaillabel;
  1603. { reset to normal non static function }
  1604. if (lexlevel=normal_function_level) then
  1605. allow_only_static:=false;
  1606. { previous lexlevel }
  1607. dec(lexlevel);
  1608. end;
  1609. procedure parse_proc_directives(Anames:Pstringcontainer;var pdflags:word);
  1610. {
  1611. Parse the procedure directives. It does not matter if procedure directives
  1612. are written using ;procdir; or ['procdir'] syntax.
  1613. }
  1614. var
  1615. res : boolean;
  1616. begin
  1617. while token in [_ID,_LECKKLAMMER] do
  1618. begin
  1619. if try_to_consume(_LECKKLAMMER) then
  1620. begin
  1621. repeat
  1622. parse_proc_direc(Anames^,pdflags);
  1623. until not try_to_consume(_COMMA);
  1624. consume(_RECKKLAMMER);
  1625. { we always expect at least '[];' }
  1626. res:=true;
  1627. end
  1628. else
  1629. res:=parse_proc_direc(Anames^,pdflags);
  1630. { A procedure directive normally followed by a semicolon, but in
  1631. a const section we should stop when _EQUAL is found }
  1632. if res then
  1633. begin
  1634. if (block_type=bt_const) and
  1635. (token=_EQUAL) then
  1636. break;
  1637. consume(_SEMICOLON);
  1638. end
  1639. else
  1640. break;
  1641. end;
  1642. end;
  1643. procedure parse_var_proc_directives(var sym : psym);
  1644. var
  1645. anames : pstringcontainer;
  1646. pdflags : word;
  1647. oldsym : pprocsym;
  1648. pd : pabstractprocdef;
  1649. begin
  1650. oldsym:=aktprocsym;
  1651. anames:=new(pstringcontainer,init);
  1652. pdflags:=pd_procvar;
  1653. { we create a temporary aktprocsym to read the directives }
  1654. aktprocsym:=new(pprocsym,init(sym^.name));
  1655. case sym^.typ of
  1656. varsym :
  1657. pd:=pabstractprocdef(pvarsym(sym)^.vartype.def);
  1658. typedconstsym :
  1659. pd:=pabstractprocdef(ptypedconstsym(sym)^.typedconsttype.def);
  1660. typesym :
  1661. pd:=pabstractprocdef(ptypesym(sym)^.restype.def);
  1662. else
  1663. internalerror(994932432);
  1664. end;
  1665. if pd^.deftype<>procvardef then
  1666. internalerror(994932433);
  1667. pabstractprocdef(aktprocsym^.definition):=pd;
  1668. { names should never be used anyway }
  1669. inc(lexlevel);
  1670. parse_proc_directives(anames,pdflags);
  1671. dec(lexlevel);
  1672. aktprocsym^.definition:=nil;
  1673. dispose(aktprocsym,done);
  1674. dispose(anames,done);
  1675. aktprocsym:=oldsym;
  1676. end;
  1677. procedure parse_object_proc_directives(var sym : pprocsym);
  1678. var
  1679. anames : pstringcontainer;
  1680. pdflags : word;
  1681. begin
  1682. pdflags:=pd_object;
  1683. anames:=new(pstringcontainer,init);
  1684. inc(lexlevel);
  1685. parse_proc_directives(anames,pdflags);
  1686. dec(lexlevel);
  1687. dispose(anames,done);
  1688. if (po_containsself in aktprocsym^.definition^.procoptions) and
  1689. (([po_msgstr,po_msgint]*aktprocsym^.definition^.procoptions)=[]) then
  1690. Message(parser_e_self_in_non_message_handler);
  1691. end;
  1692. procedure checkvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  1693. var
  1694. vs : pvarsym;
  1695. s : string;
  1696. begin
  1697. with pvarsym(p)^ do
  1698. begin
  1699. if copy(name,1,3)='val' then
  1700. begin
  1701. s:=Copy(name,4,255);
  1702. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  1703. begin
  1704. vs:=new(Pvarsym,initdef(s,vartype.def));
  1705. vs^.fileinfo:=fileinfo;
  1706. vs^.varspez:=varspez;
  1707. aktprocsym^.definition^.localst^.insert(vs);
  1708. {$ifdef INCLUDEOK}
  1709. include(vs^.varoptions,vo_is_local_copy);
  1710. {$else}
  1711. vs^.varoptions:=vs^.varoptions+[vo_is_local_copy];
  1712. {$endif}
  1713. vs^.varstate:=vs_assigned;
  1714. localvarsym:=vs;
  1715. inc(refs); { the para was used to set the local copy ! }
  1716. { warnings only on local copy ! }
  1717. varstate:=vs_used;
  1718. end
  1719. else
  1720. begin
  1721. aktprocsym^.definition^.parast^.rename(name,s);
  1722. end;
  1723. end;
  1724. end;
  1725. end;
  1726. procedure read_proc;
  1727. {
  1728. Parses the procedure directives, then parses the procedure body, then
  1729. generates the code for it
  1730. }
  1731. var
  1732. oldprefix : string;
  1733. oldprocsym : Pprocsym;
  1734. oldprocinfo : pprocinfo;
  1735. oldconstsymtable : Psymtable;
  1736. oldfilepos : tfileposinfo;
  1737. names : Pstringcontainer;
  1738. pdflags : word;
  1739. prevdef,stdef : pprocdef;
  1740. begin
  1741. { save old state }
  1742. oldprocsym:=aktprocsym;
  1743. oldprefix:=procprefix;
  1744. oldconstsymtable:=constsymtable;
  1745. oldprocinfo:=procinfo;
  1746. { create a new procedure }
  1747. new(names,init);
  1748. {$ifdef fixLeaksOnError}
  1749. strContStack.push(names);
  1750. {$endif fixLeaksOnError}
  1751. codegen_newprocedure;
  1752. with procinfo^ do
  1753. begin
  1754. parent:=oldprocinfo;
  1755. { clear flags }
  1756. flags:=0;
  1757. { standard frame pointer }
  1758. framepointer:=frame_pointer;
  1759. { funcret_is_valid:=false; }
  1760. funcret_state:=vs_declared;
  1761. { is this a nested function of a method ? }
  1762. if assigned(oldprocinfo) then
  1763. _class:=oldprocinfo^._class;
  1764. end;
  1765. parse_proc_dec;
  1766. procinfo^.sym:=aktprocsym;
  1767. procinfo^.def:=aktprocsym^.definition;
  1768. { set the default function options }
  1769. if parse_only then
  1770. begin
  1771. aktprocsym^.definition^.forwarddef:=true;
  1772. { set also the interface flag, for better error message when the
  1773. implementation doesn't much this header }
  1774. aktprocsym^.definition^.interfacedef:=true;
  1775. pdflags:=pd_interface;
  1776. end
  1777. else
  1778. begin
  1779. pdflags:=pd_body;
  1780. if current_module^.in_implementation then
  1781. pdflags:=pdflags or pd_implemen;
  1782. if (not current_module^.is_unit) or (cs_create_smart in aktmoduleswitches) then
  1783. pdflags:=pdflags or pd_global;
  1784. procinfo^.exported:=false;
  1785. aktprocsym^.definition^.forwarddef:=false;
  1786. end;
  1787. { parse the directives that may follow }
  1788. inc(lexlevel);
  1789. parse_proc_directives(names,pdflags);
  1790. dec(lexlevel);
  1791. { set aktfilepos to the beginning of the function declaration }
  1792. oldfilepos:=aktfilepos;
  1793. aktfilepos:=aktprocsym^.definition^.fileinfo;
  1794. { search for forward declarations }
  1795. if not check_identical(prevdef) then
  1796. begin
  1797. { A method must be forward defined (in the object declaration) }
  1798. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  1799. Message(parser_e_header_dont_match_any_member);
  1800. { Give a better error if there is a forward def in the interface and only
  1801. a single implementation }
  1802. if (not aktprocsym^.definition^.forwarddef) and
  1803. assigned(aktprocsym^.definition^.nextoverloaded) and
  1804. aktprocsym^.definition^.nextoverloaded^.forwarddef and
  1805. aktprocsym^.definition^.nextoverloaded^.interfacedef and
  1806. not(assigned(aktprocsym^.definition^.nextoverloaded^.nextoverloaded)) then
  1807. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName)
  1808. else
  1809. begin
  1810. { check the global flag }
  1811. if (procinfo^.flags and pi_is_global)<>0 then
  1812. Message(parser_e_overloaded_must_be_all_global);
  1813. end
  1814. end;
  1815. { set return type here, becuase the aktprocsym^.definition can be
  1816. changed by check_identical (PFV) }
  1817. procinfo^.returntype.def:=aktprocsym^.definition^.rettype.def;
  1818. { pointer to the return value ? }
  1819. if ret_in_param(procinfo^.returntype.def) then
  1820. begin
  1821. procinfo^.return_offset:=procinfo^.para_offset;
  1822. inc(procinfo^.para_offset,target_os.size_of_pointer);
  1823. end;
  1824. { allows to access the parameters of main functions in nested functions }
  1825. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  1826. { when it is a value para and it needs a local copy then rename
  1827. the parameter and insert a copy in the localst. This is not done
  1828. for assembler procedures }
  1829. if (not parse_only) and (not aktprocsym^.definition^.forwarddef) then
  1830. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}checkvaluepara);
  1831. { restore file pos }
  1832. aktfilepos:=oldfilepos;
  1833. { compile procedure when a body is needed }
  1834. if (pdflags and pd_body)<>0 then
  1835. begin
  1836. Message1(parser_p_procedure_start,aktprocsym^.demangledname);
  1837. names^.insert(aktprocsym^.definition^.mangledname);
  1838. { set _FAIL as keyword if constructor }
  1839. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1840. tokeninfo^[_FAIL].keyword:=m_all;
  1841. if assigned(aktprocsym^.definition^._class) then
  1842. tokeninfo^[_SELF].keyword:=m_all;
  1843. compile_proc_body(names^,((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  1844. { reset _FAIL as normal }
  1845. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1846. tokeninfo^[_FAIL].keyword:=m_none;
  1847. if assigned(aktprocsym^.definition^._class) and (lexlevel=main_program_level) then
  1848. tokeninfo^[_SELF].keyword:=m_none;
  1849. consume(_SEMICOLON);
  1850. end;
  1851. { close }
  1852. {$ifdef fixLeaksOnError}
  1853. if names <> strContStack.pop then
  1854. writeln('problem with strContStack in psub!');
  1855. {$endif fixLeaksOnError}
  1856. dispose(names,done);
  1857. codegen_doneprocedure;
  1858. { Restore old state }
  1859. constsymtable:=oldconstsymtable;
  1860. { from now on all refernece to mangledname means
  1861. that the function is already used }
  1862. aktprocsym^.definition^.count:=true;
  1863. { restore the interface order to maintain CRC values PM }
  1864. if assigned(prevdef) and assigned(aktprocsym^.definition^.nextoverloaded) then
  1865. begin
  1866. stdef:=aktprocsym^.definition;
  1867. aktprocsym^.definition:=stdef^.nextoverloaded;
  1868. stdef^.nextoverloaded:=prevdef^.nextoverloaded;
  1869. prevdef^.nextoverloaded:=stdef;
  1870. end;
  1871. aktprocsym:=oldprocsym;
  1872. procprefix:=oldprefix;
  1873. procinfo:=oldprocinfo;
  1874. opsym:=nil;
  1875. end;
  1876. end.
  1877. {
  1878. $Log$
  1879. Revision 1.59 2000-04-26 08:54:19 pierre
  1880. * More changes for operator bug
  1881. Order_overloaded method removed because it conflicted with
  1882. new implementation where the defs are ordered
  1883. according to the unit loading order !
  1884. Revision 1.58 2000/04/25 23:55:29 pierre
  1885. + Hint about unused unit
  1886. * Testop bug fixed !!
  1887. Now the operators are only applied if the unit is explicitly loaded
  1888. Revision 1.57 2000/04/24 12:48:37 peter
  1889. * removed unused vars
  1890. Revision 1.56 2000/03/31 22:56:47 pierre
  1891. * fix the handling of value parameters in cdecl function
  1892. Revision 1.55 2000/03/27 11:57:22 pierre
  1893. * fix for bug 890
  1894. Revision 1.54 2000/03/23 22:17:51 pierre
  1895. * fix tf000008 bug
  1896. Revision 1.53 2000/03/16 16:41:13 pierre
  1897. * fix for bug 807
  1898. Revision 1.52 2000/03/15 23:10:00 pierre
  1899. * fix for bug 848 (that still genrated wrong code)
  1900. + better testing for variables used in assembler
  1901. (gives an error if variable is not directly reachable !)
  1902. Revision 1.51 2000/02/27 14:44:39 peter
  1903. * if calling convention doesn't match don't print note about
  1904. different manglednames
  1905. Revision 1.50 2000/02/20 20:49:45 florian
  1906. * newcg is compiling
  1907. * fixed the dup id problem reported by Paul Y.
  1908. Revision 1.49 2000/02/17 14:53:42 florian
  1909. * some updates for the newcg
  1910. Revision 1.48 2000/02/09 13:23:00 peter
  1911. * log truncated
  1912. Revision 1.47 2000/02/08 13:55:13 peter
  1913. * reset section back to code after localdata
  1914. Revision 1.46 2000/02/04 20:00:22 florian
  1915. * an exception in a construcor calls now the destructor (this applies only
  1916. to classes)
  1917. Revision 1.45 2000/02/04 14:54:17 jonas
  1918. * moved call to resetusableregs to compile_proc_body (put it right before the
  1919. reset of the temp generator) so the optimizer can know which registers are
  1920. regvars
  1921. Revision 1.44 2000/01/28 23:17:53 florian
  1922. * virtual XXXX; support for objects, only if -dWITHDMT is defined
  1923. Revision 1.43 2000/01/21 22:06:16 florian
  1924. * fixed for the fix of bug 793
  1925. * fpu variables modified by nested subroutines aren't regable anymore
  1926. * $maxfpuregisters doesn't modify anymore the behavior of a procedure before
  1927. Revision 1.42 2000/01/16 22:17:12 peter
  1928. * renamed call_offset to para_offset
  1929. Revision 1.41 2000/01/11 17:16:06 jonas
  1930. * removed a lot of memory leaks when an error is encountered (caused by
  1931. procinfo and pstringcontainers). There are still plenty left though :)
  1932. Revision 1.40 2000/01/07 01:14:31 peter
  1933. * updated copyright to 2000
  1934. Revision 1.39 1999/12/22 01:01:52 peter
  1935. - removed freelabel()
  1936. * added undefined label detection in internal assembler, this prevents
  1937. a lot of ld crashes and wrong .o files
  1938. * .o files aren't written anymore if errors have occured
  1939. * inlining of assembler labels is now correct
  1940. Revision 1.38 1999/12/06 18:17:09 peter
  1941. * newcg compiler compiles again
  1942. Revision 1.37 1999/11/30 10:40:48 peter
  1943. + ttype, tsymlist
  1944. Revision 1.36 1999/11/22 00:23:09 pierre
  1945. * also complain about unused functions in program
  1946. Revision 1.35 1999/11/17 17:05:02 pierre
  1947. * Notes/hints changes
  1948. Revision 1.34 1999/11/10 00:24:02 pierre
  1949. * more browser details
  1950. Revision 1.33 1999/11/09 23:43:08 pierre
  1951. * better browser info
  1952. Revision 1.32 1999/11/09 23:06:45 peter
  1953. * esi_offset -> selfpointer_offset to be newcg compatible
  1954. * hcogegen -> cgbase fixes for newcg
  1955. Revision 1.31 1999/11/06 14:34:23 peter
  1956. * truncated log to 20 revs
  1957. Revision 1.30 1999/10/26 12:30:44 peter
  1958. * const parameter is now checked
  1959. * better and generic check if a node can be used for assigning
  1960. * export fixes
  1961. * procvar equal works now (it never had worked at least from 0.99.8)
  1962. * defcoll changed to linkedlist with pparaitem so it can easily be
  1963. walked both directions
  1964. Revision 1.29 1999/10/22 10:39:35 peter
  1965. * split type reading from pdecl to ptype unit
  1966. * parameter_dec routine is now used for procedure and procvars
  1967. Revision 1.28 1999/10/13 10:37:36 peter
  1968. * moved mangledname creation of normal proc so it also handles a wrong
  1969. method proc
  1970. }