2017년 6월 7일 수요일

Meta-AspectJ

1. Meta-AspectJ
 : a language tool for generating Java and AspectJ programs using code templates
 : meta-programming language
 : built on top of AspectJ (hence Java)
 : guarantees syntactic hygiene
  => use of typed syntax trees
  => SafeGen extension guarantees referential hygiene
http://yanniss.github.io/maj/


2. Meta-programming requires representing programs as data objects
 : A meta-program is a program that represents and manipulates programs as data objects at its run-time
 : A meta-programming language provides language support to represent programs as data objects (reflection)

 - Program representation methods:
  1) text => bad
  2) objects => better but still clumsy: typically as abstract syntax tree(AST)
  3) AST with quote/unquote


3. Quote/unquote representation of programs as data objects
 - Let the compiler build the AST!
  1) use concrete syntax instead of AST constructors
  2) tell the compiler to return AST (instead of compiling): quote program text
  VarDec v = `[ int i; ];

 - Splice meta-level values into AST!
  1) turn meta-language computation results into objects
  2) tell the compiler to copy AST: unquote expressions (vars)
  VarDec v = `[ int i; ];
  Class cl = Foo.class;
  ClassDec = `[ class #[cl.getName()]{ #v } ];

 - Additional operations:
  1) emit: turn data object into source code (program code)
  2) eval: turn data object into object code
  3) run: execute data object (runtime code generation)


4. Quote/unquote in Meta-AspectJ
 - More complicated in languages with richer syntax
  1) quote must parse program text
   infer v = `[ int x = 0 ; ];

  2) unquote must ensure well-formed syntax-trees
   infer c = `[ class C { #v } ];

  3) Meta-AspectJ supports type inference of quotations

https://wiki.hh.se/wg211/images/f/f5/Yannis-smaragdakis-ifip06.pdf


5. Meta-programming requires manipulating programs as data objects
 - quote/unquote is representation mechanism only
 - meta-programming needs support for
  ... parsing existing programs
  ... traversing ASTs
  ... pattern matching over ASTs
  ... manipulating ASTs
 => accidental complexity: not original purpose


6. Meta-AspectJ builds on AspectJ to avoid accidental complexity
 : Generate Aspects!
 - generated aspects implement a generator functionality
 - AspectJ implements generator mechanism

 A Trivial Meta-AspectJ Example
 void generateTrivialLogger(String cn, String mn){
     infer aspectCode = '[
         package MyPackage;
         aspect #[cn + "_" + mn + "Logger"] {
             before : call(* #cn.#mn(..)){
                 out("calling method " + #mn + " in class " + #cn);
             }
         }
     }];
     System.out.println(aspectCode.unparse());
 }


7. Encoding meta-programming as meta-programming language constructors
 - Observation: most meta-programming iterates over lists in AST and checks AST types
   => accidental complexity
 - Solution: new meta-programming language (SafeGen)
 #defgen makeInterface (Class c){
     interface I {
         #foreach(Method m : MethodOf(m, c)) {
             void #[m] ();
         }
     }
 }

댓글 없음:

댓글 쓰기