Mechanisms of molecular doping of graphene: A first-principles study